HSSFWorkbook wb = new HSSFWorkbook();
//創建一個sheet及把sheet名字中文化
HSSFSheet sheet = wb.createSheet("TEST表格"); //建立新的sheet物件
wb.setSheetName(0, "TEST表格", (short)1);
//定義cell內字體樣式
HSSFFont font = wb.createFont();
//設置字體色彩
font.setColor(HSSFFont.COLOR_RED);
font.setFontHeightInPoints( (short)24);
font.setFontName("宋體");
//創建一個樣式對象
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setWrapText(true);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_BOTTOM);
cellStyle.setFont(font);
//創建新的row
HSSFRow row = sheet.createRow((short)0);
//創建新的cell
HSSFCell cell = row.createCell((short)3);
//把cell內容中文化,這個一定要寫在setCellValue前面,不然的話,就還是會出現亂碼
cell.setEncoding((short)1);
cell.setCellValue("這是第一個生成的完整的EXCEL檔");
//把cell樣式增加進來
cell.setCellStyle(cellStyle);
//把CELL合並起來
sheet.addMergedRegion(new Region(0, (short) 3, 2, (short)11));
//畫出新的行
HSSFRow rowA = sheet.createRow((short)3);
for(int i = 0; i < 10; i ++){
HSSFCell cellA = rowA.createCell( (short) i);
cellA.setEncoding( (short) 1);
cellA.setCellValue("POI+"+i);
}
//畫出新的列
for(int k = 4; k < 10; k++){
HSSFRow rowB = sheet.createRow((short)k);
for (int m = 0; m < 15; m++) {
HSSFCell cells = rowB.createCell( (short) m);
cells.setEncoding( (short) 1);
cells.setCellValue("test"+k+m);
}
}