Date apache格式中的POI问题

Date apache格式中的POI问题,date,format,apache-poi,Date,Format,Apache Poi,我是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

我是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;
        }
}
包oup.excel.report;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.sql.Connection;
导入java.sql.PreparedStatement;
导入java.sql.ResultSet;
导入java.text.ParseException;
导入java.text.simpleDataFormat;
导入java.util.Date;
导入org.apache.poi.hssf.usermodel.HSSFCell;
导入org.apache.poi.hssf.usermodel.HSSFCellStyle;
导入org.apache.poi.hssf.usermodel.hssfont;
导入org.apache.poi.hssf.usermodel.hssfpalete;
导入org.apache.poi.hssf.usermodel.HSSFRow;
导入org.apache.poi.hssf.usermodel.HSSFSheet;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.hssf.util.HSSFColor;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.CellStyle;
导入org.apache.poi.ss.usermodel.CreationHelper;
导入oup.dbconnection.ConnectionObject;
导入util.Mail;
公共类报告{
公共静态void main(字符串[]args){
试一试{
文件报告路径=新文件(“/a1/reports/”);
reportPath.mkdirs();
字符串filename=“/a1/reports/OUP_Delivery_Report_”+getTodaysDate()+“.xls”;
HSSFWorkbook hwb=新的HSSFWorkbook();
HSSFSheet sheet=hwb.createSheet(“OUP交付报告”);
HSSFRow rowhead=sheet.createRow((短)0);
hssfont font=hwb.createFont();
font.setFontName(“投石机MS”);
font.setFontHeightInPoints((短)8);
字体设置权重(hssfont.BOLDWEIGHT\u BOLD);
HSSFCellStyle=hwb.createCellStyle();
HSSFColor青色=设置颜色(hwb,(字节)204,(字节)255,(字节)255);
样式.底部(HSSFCellStyle.边框薄);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setFillForegroundColor(CYAN.getIndex());
//style.setFillBackgroundColor(新的HSSFColor.BLACK().getIndex());
style.setFillPattern(HSSFCellStyle.SOLID\u前景);
style.setFont(字体);
样式设置对齐(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(“日记账”);
uidcell.setCellValue(“唯一ID”);
stepcell.setCellValue(“步骤”);
ptcell.setCellValue(“校对排版页”);
dccell.setCellValue(“完成日期”);
cmtscell.setCellValue(“注释”);
jrnlcell.setCellStyle(style);
uidcell.setCellStyle(style);
stepcell.setCellStyle(style);
ptcell.setCellStyle(style);
dccell.setCellStyle(style);
cmtscell.setCellStyle(style);
Connection con=ConnectionObject.getConnection();
String sql=“选择getjrabbr(journalbookid)journal,clientreference UniqueID,(当currentstageid=11时为“厨房”,当currentstageid=1时为“厨房”,当currentstageid=54时为“证明”,当currentstageid=2时为“从T/S”,当currentstageid=3时为“REV1”,当currentstageid=4时为“REV2”,当currentstageid=14时为“REV3”,当currentstageid=18时为“REV4”,当currentstageid=19时为“REV6rrentstageid=20,当currentstageid=21时为'REV7',当currentstageid=70时为'REV8',当currentstageid=71时为'REV9',当currentstageid=6时为'REV10',当currentstageid=93时为'Supply Files for QA',当currentstageid=39时为'REPP',当currentstageid=99时为'PAP',当currentstageid=100时为'PAP1',然后为'PAP2'结束)步骤,tsp Prooftypesetpages,TO_CHAR(CLIENTUPLOADDATE,'MM/DD/yyyyy')Date从v_jobitems_history ji完成,其中ji.clientid=1487,CLIENTUPLOADDATE不为空,CLIENTUPLOADDATE在TRUNC(SYSDATE-1)+3/24和TRUNC(SYSDATE)+3/24之间的顺序为5”;
PreparedStatement pstmt=con.prepareStatement(sql);
结果集rs=pstmt.executeQuery();
int i=1;
HSSFCellStyle1=hwb.createCellStyle();
hssfont font1=hwb.createFont();
font1.setFontName(“投石机MS”);
font1.设置FontHeightInPoints((短)8);
style1.setFont(font1);
样式1.底部(HSSFCellStyle.边框薄);
style1.setBorderTop(HSSFCellStyle.BORDER_THIN);
style1.setBorderRight(HSSFCellStyle.BORDER_THIN);
style1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
样式1.设置对齐(HSSFCellStyle.对齐\u中心);
CellStyle CellStyle=hwb.createCellStyle();
CreationHelper createHelper=hwb.getCreationHelper();
//设置日期的日期格式
setDataFormat(createHelper.createDataFormat().getFormat(“d-mmmm”));
cellStyle.setFont(font1);
cellStyle.SetboordBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cellStyle.setAlignment(HSSFCellStyle.Alignment_CENTER);
CellStyle cellStyleInt=hwb.createCellStyle();
cellStyleInt.setDataFormat(createHelper.createDataFormat().getFormat(“#”);
dccell.setCellStyle(cellStyle);