Total Pageviews

2013/11/12

POI 常用API筆記

參考資料來源:http://become.wei-ting.net/2011/11/poiexcel.html

1:  File tempFile = new File(filePath,filename);//建立儲存檔案  
2:  Workbook workbook = new HSSFWorkbook();//建立Excel物件  
3:  String safeName = WorkbookUtil.createSafeSheetName(SHEETNAME);   
4:  Sheet sheet = workbook.createSheet(safeName);//建立工作表  
5:  Row row1 = sheet.createRow((short)0);//建立工作列  
6:  //字型設定  
7:  Font font = workbook.createFont();  
8:  font.setColor(HSSFColor.WHITE.index);//顏色  
9:  font.setBoldweight(Font.BOLDWEIGHT_BOLD); //粗體  
10:  //設定儲存格格式  
11:  CellStyle styleRow1 = workbook.createCellStyle();  
12:  styleRow1.setFillForegroundColor(HSSFColor.GREEN.index);//填滿顏色  
13:  styleRow1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);  
14:  styleRow1.setFont(font);//設定字體  
15:  styleRow1.setAlignment(HSSFCellStyle.ALIGN_CENTER);//水平置中  
16:  styleRow1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//垂直置中  
17:  //設定框線  
18:  styleRow1.setBorderBottom((short)1);  
19:  styleRow1.setBorderTop((short)1);  
20:  styleRow1.setBorderLeft((short)1);  
21:  styleRow1.setBorderRight((short)1);  
22:  styleRow1.setWrapText(true);//自動換行  
23:  Cell cell = row1.createCell(0);//建立儲存格  
24:  cell.setCellStyle(styleRow1);//套用格式  
25:  cell.setCellValue(CELLTEXT);//設定內容  
26:  sheet.autoSizeColumn(0);//自動調整欄位寬度  
27:  //儲存檔案  
28:  FileOutputStream fileOut = new FileOutputStream(tempFile);  
29:  workbook.write(fileOut);  
30:  fileOut.close();  

No comments: