1

我是 POI 的新手,我的代码没有给我合适的结果。填充 excel 表时,它没有在完成日期列中应用样式,也没有将完成日期列的格式更改为日期(格式应为 dd-mmm)。这是我的代码。

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;
        }
}

请给我一个适当的解决方案。

谢谢

赛卡特

4

1 回答 1

2

您正在创建 HSSFCelldcell以将日期值存储在 while 循环中。但是,从未设置此单元格的样式。

你的线

dccell.setCellStyle(cellStyle);

在您的 while 循环中设置标题单元格的单元格样式(“dccell”),而不是您的实际日期值单元格的样式(“dcell”)。

于 2013-01-14T19:20:24.627 回答