Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9175073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:51:19+00:00 2026-06-17T16:51:19+00:00

I am a newbie in POI and my code is not giving me suitable

  • 0

I am a newbie in POI and my code is not giving me suitable result. When excel sheet is populating it is not applying style in Date completed column as well as it is not changing the format of the Date completed column to Date (format should be dd-mmm) . Here is my code.

package oup.excel.report;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;

import oup.dbconnection.ConnectionObject;
import util.Mail;

public class CreateExcelReport {
    public static void main(String[] args) {
        try {
            File reportPath = new File("/a1/reports/");
            reportPath.mkdirs();
            String filename = "/a1/reports/OUP_Delivery_Report_" + getTodaysDate() + ".xls";
            HSSFWorkbook hwb = new HSSFWorkbook();
            HSSFSheet sheet = hwb.createSheet("OUP Delivery Report");           
            HSSFRow rowhead = sheet.createRow((short) 0);

            HSSFFont font = hwb.createFont();
            font.setFontName("Trebuchet MS");
            font.setFontHeightInPoints((short) 8);
            font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

            HSSFCellStyle style = hwb.createCellStyle();

            HSSFColor CYAN = setColor(hwb, (byte) 204, (byte) 255, (byte) 255);

            style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            style.setBorderTop(HSSFCellStyle.BORDER_THIN);
            style.setBorderRight(HSSFCellStyle.BORDER_THIN);
            style.setBorderLeft(HSSFCellStyle.BORDER_THIN);

            style.setFillForegroundColor(CYAN.getIndex());
            //style.setFillBackgroundColor(new HSSFColor.BLACK().getIndex());
            style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND );

            style.setFont(font);
            style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

            HSSFCell jrnlcell = rowhead.createCell(0);
            HSSFCell uidcell = rowhead.createCell(1);
            HSSFCell stepcell = rowhead.createCell(2);
            HSSFCell ptcell = rowhead.createCell(3);
            HSSFCell dccell = rowhead.createCell(4);
            HSSFCell cmtscell = rowhead.createCell(5);

            jrnlcell.setCellValue("Journal");
            uidcell.setCellValue("Unique ID");
            stepcell.setCellValue("Step");
            ptcell.setCellValue("Proof typeset pages");
            dccell.setCellValue("Date completed");
            cmtscell.setCellValue("Comments");

            jrnlcell.setCellStyle(style);
            uidcell.setCellStyle(style);
            stepcell.setCellStyle(style);
            ptcell.setCellStyle(style);
            dccell.setCellStyle(style);
            cmtscell.setCellStyle(style);

            Connection con = ConnectionObject.getConnection();
            String sql = "SELECT getjrabbr (journalbookid) journal, clientreference UniqueID, (CASE WHEN currentstageid = 11 THEN 'GALLEY' WHEN currentstageid = 1 THEN 'PROOF' WHEN currentstageid = 54 THEN 'FROM T/S' WHEN currentstageid = 2 THEN 'REV1' WHEN currentstageid = 3 THEN 'REV2' WHEN currentstageid = 4 THEN 'REV3' WHEN currentstageid = 14 THEN 'REV4' WHEN currentstageid = 18 THEN 'REV5' WHEN currentstageid = 19 THEN 'REV6' WHEN currentstageid = 20 THEN 'REV7' WHEN currentstageid = 21 THEN 'REV8' WHEN currentstageid = 70 THEN 'REV9' WHEN currentstageid = 71 THEN 'REV10' WHEN currentstageid = 6 THEN 'Supply Files for QA' WHEN currentstageid = 93 THEN 'Resupp QA' WHEN currentstageid = 39 THEN 'PAP' WHEN currentstageid = 99 THEN 'PAP1' WHEN currentstageid = 100 THEN 'PAP2' END) step, tsp Prooftypesetpages, TO_CHAR (CLIENTUPLOADDATE, 'MM/DD/YYYY') Datecompleted FROM v_jobitems_history ji WHERE ji.clientid = 1487 AND CLIENTUPLOADDATE IS NOT NULL AND CLIENTUPLOADDATE BETWEEN TRUNC (SYSDATE - 1) + 3 / 24 AND TRUNC (SYSDATE) + 3 / 24 order by 5";
            PreparedStatement pstmt = con.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();

            int i=1;

            HSSFCellStyle style1 = hwb.createCellStyle();
            HSSFFont font1 = hwb.createFont();
            font1.setFontName("Trebuchet MS");
            font1.setFontHeightInPoints((short) 8);

            style1.setFont(font1);
            style1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            style1.setBorderTop(HSSFCellStyle.BORDER_THIN);
            style1.setBorderRight(HSSFCellStyle.BORDER_THIN);
            style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            style1.setAlignment(HSSFCellStyle.ALIGN_CENTER);

            CellStyle cellStyle = hwb.createCellStyle();
            CreationHelper createHelper = hwb.getCreationHelper();
            // Set the date format of date
            cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("d-mmmm"));
            cellStyle.setFont(font1);
            cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
            cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
            cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
            cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);

            CellStyle cellStyleInt = hwb.createCellStyle();
            cellStyleInt.setDataFormat(createHelper.createDataFormat().getFormat("#"));
            cellStyleInt.setFont(font1);
            cellStyleInt.setBorderBottom(HSSFCellStyle.BORDER_THIN);
            cellStyleInt.setBorderTop(HSSFCellStyle.BORDER_THIN);
            cellStyleInt.setBorderRight(HSSFCellStyle.BORDER_THIN);
            cellStyleInt.setBorderLeft(HSSFCellStyle.BORDER_THIN);          
            cellStyleInt.setAlignment(HSSFCellStyle.ALIGN_CENTER);

            while(rs.next()){
                String journal = rs.getString("journal");
                String UniqueID = rs.getString("UniqueID");
                String step = rs.getString("step");
                String tsp = rs.getString("Prooftypesetpages");
                String Datecompleted = rs.getString("Datecompleted");

                System.out.println("journal : " + journal);
                System.out.println("UniqueID : " + UniqueID);
                System.out.println("step : " + step);
                System.out.println("tsp : " + tsp);
                System.out.println("Datecompleted : " + Datecompleted);
                System.out.println("================================================");

                HSSFRow row = sheet.createRow((short) i);

                HSSFCell jcell = row.createCell(0);
                HSSFCell ucell = row.createCell(1);
                HSSFCell scell = row.createCell(2);
                HSSFCell tcell = row.createCell(3);
                HSSFCell dcell = row.createCell(4);
                HSSFCell ccell = row.createCell(5);

                tcell.setCellType(Cell.CELL_TYPE_NUMERIC);

                jcell.setCellValue(journal);
                ucell.setCellValue(UniqueID);
                scell.setCellValue(step);
                if(tsp!=null && !tsp.equals("")){
                    tcell.setCellValue(Integer.parseInt(tsp.trim()));
                }else{
                    tcell.setCellValue(0);
                }

                SimpleDateFormat oldFormat = new SimpleDateFormat("dd/MM/yyyy");
                SimpleDateFormat newFormat = new SimpleDateFormat("dd-MMM");

                String reformattedDt = null;
                Date inputDt = null;
                try {
                    reformattedDt = newFormat.format(oldFormat.parse(Datecompleted));
                    inputDt = newFormat.parse(reformattedDt);                   
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                dcell.setCellValue(inputDt);
                ccell.setCellValue("");

                jcell.setCellStyle(style1);
                ucell.setCellStyle(style1);
                scell.setCellStyle(style1);
                tcell.setCellStyle(cellStyleInt);
                dccell.setCellStyle(cellStyle);
                ccell.setCellStyle(style1);

                i++;
            }

            try{
                if(rs!=null){
                    rs.close();
                }
                if(pstmt!=null){
                    pstmt.close();
                }
                if(con!=null){
                    con.close();
                }
            }catch (Exception e) {
                e.printStackTrace();
            }
            for(int j=0;j<6;j++){
                sheet.autoSizeColumn((short) j);
            }
            FileOutputStream fileOut = new FileOutputStream(filename);
            hwb.write(fileOut);
            fileOut.close();
            Mail mail = new Mail();
            boolean sendmail = mail.sendMailWithAttachHtmlFormat("dbadmin@aptaracorp.com", "abc@abc.com", "abc@aabc.com", "OUP daily delivery report", "<p style='color:blue'>Hi, <br/>Please find the daily delivery report for the articles.</p><br/><p style='color:blue'>Thanks,<br/>Aptara Automailer</p>", filename, "", "DBADMIN");
            if(sendmail){
                System.out.println("Mail sent successfully.");
            }
            System.out.println("Your excel file has been generated!");
        } 
        catch(FileNotFoundException fnf){
            if(fnf.getMessage().indexOf("The process cannot access the file because it is being used by another process")>=0){
                System.out.println("The file is already being used by another process. Please close the file and try again.");
            }else{
                System.out.println("Exception occured : " + fnf.getMessage());
            }           
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static String getTodaysDate(){
        Date date = new Date();
        SimpleDateFormat sdf;               
        sdf = new SimpleDateFormat("ddMMMyyyy_hh_mm_ss");
        String dtString = sdf.format(date);
        return dtString;
    }

    private static HSSFColor setColor(HSSFWorkbook workbook, byte r,byte g, byte b){
        HSSFPalette palette = workbook.getCustomPalette();
        HSSFColor hssfColor = null;
        try {
        hssfColor= palette.findColor(r, g, b); 
        if (hssfColor == null ){
            palette.setColorAtIndex(HSSFColor.LAVENDER.index, r, g,b);
            hssfColor = palette.getColor(HSSFColor.LAVENDER.index);
        }
         } catch (Exception e) {
             e.printStackTrace();
        }

         return hssfColor;
        }
}

Please give me a proper solution.

Thanks

Saikat

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:51:20+00:00Added an answer on June 17, 2026 at 4:51 pm

    You are creating your HSSFCell dcell to store your date values inside your while loop. However, this cell’s style is never set.

    Your line

    dccell.setCellStyle(cellStyle);
    

    inside your while loop is setting your header cell’s cell style (“dccell”), not your actual date value cell’s style (“dcell”).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Newbie question since I'm not up to speed using maven at all. I'm trying
Newbie here. I am trying to store the result of my search onto a
newbie question in css: I have the following style defined: TABLE.tabulardata th { font:
Newbie here. I am looking at company code. It appears that there are NO
newbie question,I have the following code where I need to match the data in
Newbie Question. I have a UIWebView that I push HTML code into. In the
* Newbie Alert * I have an AdvancedDataGrid with 5 columns. The second column,
Newbie IIS question: I want to setup HTTPS access for my XHTML page (not
Newbie in testing. I generated a test case using Selenium, and then exported it
newbie here, so thanks in advance for help! I have a Wordpress site with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.