1. 程式人生 > 其它 >SPI協議總結

SPI協議總結

apache.poi 可用於 word/ppt/excel等檔案進行處理

詳情見於 -> https://poi.apache.org/components/index.html 導航欄中Component APIs 應用指南

一、Excel (HSSF/XSSF)

 1 public static void creatWorkbook()throws Exception{
 2         // 建立工作薄
 3         Workbook wb = new HSSFWorkbook();
 4         // 建立表
 5         CreationHelper createHelper = wb.getCreationHelper();
6 Sheet sheet = wb.createSheet("new sheet"); 7 // 建立第一行 *************************** 8 Row row = sheet.createRow(0); 9 // 建立單元格 10 Cell cell = row.createCell(0); 11 // 插入單元格數值 (整數、小數、字串、布林型) 12 cell.setCellValue(1); 13 row.createCell(1).setCellValue(1.2);
14 row.createCell(2).setCellValue(createHelper.createRichTextString("This is a string")); 15 row.createCell(3).setCellValue(true); 16 // 建立第二行 * 時間 ************************* 17 Row row1 = sheet.createRow(1); 18 Cell cell1 = row1.createCell(0); 19 String localData = LocalDate.now().toString();
20 cell1.setCellValue(createHelper.createRichTextString(localData)); 21 22 23 CellStyle cellStyle = wb.createCellStyle(); 24 cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("m/d/yy h:mm")); 25 cell1 = row1.createCell(1); 26 cell1.setCellValue(new Date()); 27 cell1.setCellStyle(cellStyle); 28 29 //you can also set date as java.util.Calendar 30 cell1 = row1.createCell(2); 31 cell1.setCellValue(Calendar.getInstance()); 32 cell1.setCellStyle(cellStyle); 33 row1.createCell(3).setCellType(CellType.ERROR); 34 35 // 輸出檔案 36 try (OutputStream fileOut = new FileOutputStream("D:\\資料\\exl\\workbook.xls")) { 37 wb.write(fileOut); 38 } 39 }