POI操作EXCEL技巧

上传人:lcm****801 文档编号:89280671 上传时间:2019-05-22 格式:DOC 页数:29 大小:191KB
返回 下载 相关 举报
POI操作EXCEL技巧_第1页
第1页 / 共29页
POI操作EXCEL技巧_第2页
第2页 / 共29页
POI操作EXCEL技巧_第3页
第3页 / 共29页
POI操作EXCEL技巧_第4页
第4页 / 共29页
POI操作EXCEL技巧_第5页
第5页 / 共29页
点击查看更多>>
资源描述

《POI操作EXCEL技巧》由会员分享,可在线阅读,更多相关《POI操作EXCEL技巧(29页珍藏版)》请在金锄头文库上搜索。

1、POI操作EXCEL技巧Java代码 1. 1.创建工作簿(WORKBOOK) 2. 3. HSSFWorkbookwb=newHSSFWorkbook(); 4. 5. FileOutputStreamfileOut=newFileOutputStream(workbook.xls); 6. 7. wb.write(fileOut); 8. 9. fileOut.close(); 10. 11. 2.创建工作表(SHEET) 12. 13. HSSFWorkbookwb=newHSSFWorkbook(); 14. 15. HSSFSheetsheet1=wb.createSheet(new

2、sheet); 16. 17. HSSFSheetsheet2=wb.createSheet(secondsheet); 18. 19. FileOutputStreamfileOut=newFileOutputStream(workbook.xls); 20. 21. wb.write(fileOut); 22. 23. fileOut.close(); 24. 25. 3.创建单元格(CELL) 26. 27. HSSFWorkbookwb=newHSSFWorkbook(); 28. 29. HSSFSheetsheet=wb.createSheet(newsheet); 30. 31.

3、 /Createarowandputsomecellsinit.Rowsare0based. 32. 33. HSSFRowrow=sheet.createRow(short)0); 34. 35. /Createacellandputavalueinit. 36. 37. HSSFCellcell=row.createCell(short)0); 38. 39. cell.setCellValue(1); 40. 41. /Ordoitononeline. 42. 43. row.createCell(short)1).setCellValue(1.2); 44. 45. row.creat

4、eCell(short)2).setCellValue(Thisisastring); 46. 47. row.createCell(short)3).setCellValue(true); 48. 49. /Writetheoutputtoafile 50. 51. FileOutputStreamfileOut=newFileOutputStream(workbook.xls); 52. 53. wb.write(fileOut); 54. 55. fileOut.close(); 56. 57. 4.创建指定单元格式的单元格 58. 59. HSSFWorkbookwb=newHSSFW

5、orkbook(); 60. 61. HSSFSheetsheet=wb.createSheet(newsheet); 62. 63. /Createarowandputsomecellsinit.Rowsare0based. 64. 65. HSSFRowrow=sheet.createRow(short)0); 66. 67. /Createacellandputadatevalueinit.Thefirstcellisnotstyled 68. 69. /asadate. 70. 71. HSSFCellcell=row.createCell(short)0); 72. 73. cell

6、.setCellValue(newDate(); 74. 75. /westylethesecondcellasadate(andtime).Itisimportantto 76. 77. /createanewcellstylefromtheworkbookotherwiseyoucanendup 78. 79. /modifyingthebuiltinstyleandeffectingnotonlythiscellbutothercells. 80. 81. HSSFCellStylecellStyle=wb.createCellStyle(); 82. 83. cellStyle.set

7、DataFormat(HSSFDataFormat.getBuiltinFormat(m/d/yyh:mm); 84. 85. cell=row.createCell(short)1); 86. 87. cell.setCellValue(newDate(); 88. 89. cell.setCellStyle(cellStyle); 90. 91. /Writetheoutputtoafile 92. 93. FileOutputStreamfileOut=newFileOutputStream(workbook.xls); 94. 95. wb.write(fileOut); 96. 97

8、. fileOut.close(); 98. 99. 5.单元格的不同格式 100. 101. HSSFWorkbookwb=newHSSFWorkbook(); 102. 103. HSSFSheetsheet=wb.createSheet(newsheet); 104. 105. HSSFRowrow=sheet.createRow(short)2); 106. 107. row.createCell(short)0).setCellValue(1.1); 108. 109. row.createCell(short)1).setCellValue(newDate(); 110. 111.

9、 row.createCell(short)2).setCellValue(astring); 112. 113. row.createCell(short)3).setCellValue(true); 114. 115. row.createCell(short)4).setCellType(HSSFCell.CELL_TYPE_ERROR); 116. 117. /Writetheoutputtoafile 118. 119. FileOutputStreamfileOut=newFileOutputStream(workbook.xls); 120. 121. wb.write(file

10、Out); 122. 123. fileOut.close(); 124. 125. 6.单元格的不通对齐方式 126. 127. publicstaticvoidmain(Stringargs) 128. 129. throwsIOException 130. 131. 132. 133. HSSFWorkbookwb=newHSSFWorkbook(); 134. 135. HSSFSheetsheet=wb.createSheet(newsheet); 136. 137. HSSFRowrow=sheet.createRow(short)2); 138. 139. createCell(

11、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.

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号