1. 程式人生 > >java動態建立excle的簡單示例

java動態建立excle的簡單示例

public  static void excel(){
        FileOutputStream fos = null;
        File excelFile = new File("D:/s.xls");
        try {

        excelFile.delete();

     //判斷檔案路徑是否存在

         if (!excelFile.exists()) {
excelFile.createNewFile();
}
        fos = new FileOutputStream(excelFile);
        // 建立excel和sheet
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet s = wb.createSheet();
        HSSFFont font = wb.createFont();
        font.setFontHeightInPoints((short) 18);
        font.setBoldweight((short) 10);

        // 建立cellStyle

        HSSFCellStyle style = wb.createCellStyle();
        style.setFont(font);


        // 帶上下左右邊界、居中、自動換行
        HSSFCellStyle styleBorderC = wb.createCellStyle();
        styleBorderC.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
        styleBorderC.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
        styleBorderC.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
        styleBorderC.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
        styleBorderC.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        styleBorderC.setWrapText(true);


        // 帶上下左右邊界、居左、自動換行

        HSSFCellStyle styleBorderL = wb.createCellStyle();
        styleBorderL.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
        styleBorderL.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
        styleBorderL.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
        styleBorderL.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
        styleBorderL.setAlignment(HSSFCellStyle.ALIGN_LEFT);
        styleBorderL.setWrapText(true);


        // 帶上下左右邊界、居右、自動換行

        HSSFCellStyle styleBorderR = wb.createCellStyle();
        styleBorderR.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
        styleBorderR.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
        styleBorderR.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
        styleBorderR.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
        styleBorderR.setAlignment(HSSFCellStyle.ALIGN_RIGHT);
        styleBorderR.setWrapText(true);


        // 設定sheet名
        wb.setSheetName(0, "sheet1");


   
        HSSFRow row = null;
        
       


        // 設定報表表頭
        // 表第一行
        // 表名

        HSSFRow row0 = s.createRow(0);
        HSSFCell cell01 = row0.createCell(2);
        cell01.setCellType(HSSFCell.CELL_TYPE_STRING);
        cell01.setCellStyle(style);
        cell01.setCellValue("建立excel");
        // 表第二行
        
        HSSFRow row1 = s.createRow(1);
        HSSFCell cell10 = row1.createCell( 0);
        cell10.setCellType(HSSFCell.CELL_TYPE_STRING);
        cell10.setCellValue("歡迎您");
        // 當前行數
        row = s.createRow(3);//索引值是第四行
        // 填寫報表資料
        for(int i=0;i<3;i++){
        HSSFCell cell = null;
                cell = row.createCell(i);
                cell.setCellType(HSSFCell.CELL_TYPE_STRING);
                cell.setCellValue(i+1);
        }
wb.write(fos);
fos.close();
} catch (IOException e) {
System.out.println(e);
}finally {
//excelFile.delete();對建立的檔案進行刪除
}
    
    }