POI操作EXCEL技巧
Java代码
1. 1.创建工作簿 (WORKBOOK) 2.
3. HSSFWorkbook wb = new HSSFWorkbook(); 4.
5. FileOutputStream fileOut = new FileOutputStream(\ 6.
7. wb.write(fileOut); 8.
9. fileOut.close(); 10.
11. 2.创建工作表(SHEET) 12.
13. HSSFWorkbook wb = new HSSFWorkbook(); 14.
15. HSSFSheet sheet1 = wb.createSheet(\ 16.
17. HSSFSheet sheet2 = wb.createSheet(\ 18.
19. FileOutputStream fileOut = new FileOutputStream(\ 20.
21. wb.write(fileOut); 22.
23. fileOut.close(); 24.
25. 3.创建单元格(CELL) 26.
27. HSSFWorkbook wb = new HSSFWorkbook(); 28.
29. HSSFSheet sheet = wb.createSheet(\ 30.
31. // Create a row and put some cells in it. Rows are 0 based. 32.
33. HSSFRow row = sheet.createRow((short)0); 34.
35. // Create a cell and put a value in it. 36.
37. HSSFCell cell = row.createCell((short)0); 38.
39. cell.setCellValue(1); 40.
41. // Or do it on one line. 42.
43. row.createCell((short)1).setCellValue(1.2); 44.
45. row.createCell((short)2).setCellValue(\ 46.
47. row.createCell((short)3).setCellValue(true); 48.
49. // Write the output to a file 50.
51. FileOutputStream fileOut = new FileOutputStream(\ 52.
53. wb.write(fileOut); 54.
55. fileOut.close(); 56.
57. 4.创建指定单元格式的单元格 58.
59. HSSFWorkbook wb = new HSSFWorkbook(); 60.
61. HSSFSheet sheet = wb.createSheet(\ 62.
63. // Create a row and put some cells in it. Rows are 0 based. 64.
65. HSSFRow row = sheet.createRow((short)0); 66.
67. // Create a cell and put a date value in it. The first cell is not styled 68.
69. // as a date. 70.
71. HSSFCell cell = row.createCell((short)0); 72.
73. cell.setCellValue(new Date()); 74.
75. // we style the second cell as a date (and time). It is important to 76.
77. // create a new cell style from the workbook otherwise you can end up 78.
79. // modifying the built in style and effecting not only this cell but other cells. 80.
81. HSSFCellStyle cellStyle = wb.createCellStyle(); 82.
83. cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat(\ 84.
85. cell = row.createCell((short)1); 86.
87. cell.setCellValue(new Date()); 88.
89. cell.setCellStyle(cellStyle); 90.
91. // Write the output to a file 92.
93. FileOutputStream fileOut = new FileOutputStream(\ 94.
95. wb.write(fileOut); 96.
97. fileOut.close(); 98.
99. 5. 单元格的不同格式 100.
101. HSSFWorkbook wb = new HSSFWorkbook(); 102.
103. HSSFSheet sheet = wb.createSheet(\ 104.
105. HSSFRow row = sheet.createRow((short)2); 106.
107. row.createCell((short) 0).setCellValue(1.1); 108.
109. row.createCell((short) 1).setCellValue(new Date()); 110.
111. row.createCell((short) 2).setCellValue(\ 112.
113. row.createCell((short) 3).setCellValue(true); 114.
115. row.createCell((short) 4).setCellType(HSSFCell.CELL_TYPE_ERROR); 116.
117. // Write the output to a file 118.
119. FileOutputStream fileOut = new FileOutputStream(\ 120.
121. wb.write(fileOut); 122.
123. fileOut.close(); 124.
125. 6.单元格的不通对齐方式 126.
127. public static void main(String[] args) 128.
129. throws IOException 130. 131. { 132.
133. HSSFWorkbook wb = new HSSFWorkbook(); 134.
135. HSSFSheet sheet = wb.createSheet(\ 136.
137. HSSFRow row = sheet.createRow((short) 2); 138.
139. createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_CENTER); 140.
141. createCell(wb, row, (short) 1, HSSFCellStyle.ALIGN_CENTER_SELECTION);
142.
143. createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_FILL); 144.
145. createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_GENERAL); 146.
147. createCell(wb, row, (short) 4, HSSFCellStyle.ALIGN_JUSTIFY); 148.
149. createCell(wb, row, (short) 5, HSSFCellStyle.ALIGN_LEFT); 150.
151. createCell(wb, row, (short) 6, HSSFCellStyle.ALIGN_RIGHT); 152.
153. // Write the output to a file 154.
155. FileOutputStream fileOut = new FileOutputStream(\ 156.
157. wb.write(fileOut); 158.
159. fileOut.close(); 160. 161. } 162.
163. /** 164.
165. * Creates a cell and aligns it a certain way. 166. 167. * 168.
169. * @param wb the workbook
170.
171. * @param row the row to create the cell in 172.
173. * @param column the column number to create the cell in 174.
175. * @param align the alignment for the cell. 176.
177. */ 178.
179. private static void createCell(HSSFWorkbook wb, HSSFRow row, short column, shor
t align) 180. 181. { 182.
183. HSSFCell cell = row.createCell(column); 184.
185. cell.setCellValue(\ 186.
187. HSSFCellStyle cellStyle = wb.createCellStyle(); 188.
189. cellStyle.setAlignment(align); 190.
191. cell.setCellStyle(cellStyle); 192. 193. } 194.
195. 7.单元格的边框设置 196.
197. Working with borders 198.
199. HSSFWorkbook wb = new HSSFWorkbook(); 200.
201. HSSFSheet sheet = wb.createSheet(\ 202.
203. // Create a row and put some cells in it. Rows are 0 based. 204.
205. HSSFRow row = sheet.createRow((short) 1); 206.
207. // Create a cell and put a value in it. 208.
209. HSSFCell cell = row.createCell((short) 1); 210.
211. cell.setCellValue(4); 212.