ApachePOIHSSF和XSSF读写EXCEL总结.docx

上传人:自*** 文档编号:124738921 上传时间:2020-03-13 格式:DOCX 页数:7 大小:36.81KB
返回 下载 相关 举报
ApachePOIHSSF和XSSF读写EXCEL总结.docx_第1页
第1页 / 共7页
ApachePOIHSSF和XSSF读写EXCEL总结.docx_第2页
第2页 / 共7页
ApachePOIHSSF和XSSF读写EXCEL总结.docx_第3页
第3页 / 共7页
ApachePOIHSSF和XSSF读写EXCEL总结.docx_第4页
第4页 / 共7页
ApachePOIHSSF和XSSF读写EXCEL总结.docx_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《ApachePOIHSSF和XSSF读写EXCEL总结.docx》由会员分享,可在线阅读,更多相关《ApachePOIHSSF和XSSF读写EXCEL总结.docx(7页珍藏版)》请在金锄头文库上搜索。

1、Apache POI HSSF和XSSF读写EXCEL总结 HSSF是指2007年以前的,XSSF是指2007年版本以上的 这个还是比较好用的,这些总结来自Apache的官方向导的点点滴滴 还有好多没有没有写的,详细的请参考http:/poi.apache.org/spreadsheet/quick-guide.html 还有LOG也比较好看 Java代码 1. publicclassSummaryHSSF 2. 3. publicstaticvoidmain(Stringargs)throwsIOException 4. /创建Workbook对象(这一个对象代表着对应的一个Excel文件)

2、 5. /HSSFWorkbook表示以xls为后缀名的文件 6. Workbookwb=newHSSFWorkbook(); 7. /获得CreationHelper对象,这个应该是一个帮助类 8. CreationHelperhelper=wb.getCreationHelper(); 9. /创建Sheet并给名字(表示Excel的一个Sheet) 10. Sheetsheet1=wb.createSheet(HSSF_Sheet_1); 11. Sheetsheet2=wb.createSheet(HSSF_Sheet_2); 12. /Row表示一行Cell表示一列 13. Rowr

3、ow=null; 14. Cellcell=null; 15. for(inti=0;i60;i=i+2) 16. /获得这个sheet的第i行 17. row=sheet1.createRow(i); 18. /设置行长度自动 19. /row.setHeight(short)500); 20. row.setHeightInPoints(20); 21. /row.setZeroHeight(true); 22. for(intj=0;j25;j+) 23. /设置每个sheet每一行的宽度,自动,根据需求自行确定 24. sheet1.autoSizeColumn(j+1,true);

4、25. /创建一个基本的样式 26. CellStylecellStyle=SummaryHSSF.createStyleCell(wb); 27. /获得这一行的每j列 28. cell=row.createCell(j); 29. if(j=0) 30. /设置文字在单元格里面的位置 31. cellStyle=SummaryHSSF.setCellStyleAlignment(cellStyle,CellStyle.ALIGN_CENTER,CellStyle.VERTICAL_CENTER); 32. /先创建字体样式,并把这个样式加到单元格的字体里面 33. cellStyle.se

5、tFont(createFonts(wb); 34. /把这个样式加到单元格里面 35. cell.setCellStyle(cellStyle); 36. /给单元格设值 37. cell.setCellValue(true); 38. elseif(j=1) 39. /设置文字在单元格里面的位置 40. cellStyle=SummaryHSSF.setCellStyleAlignment(cellStyle,CellStyle.ALIGN_CENTER,CellStyle.VERTICAL_CENTER); 41. /设置这个样式的格式(Format) 42. cellStyle=Sum

6、maryHSSF.setCellFormat(helper,cellStyle,#,#0.0000); 43. /先创建字体样式,并把这个样式加到单元格的字体里面 44. cellStyle.setFont(createFonts(wb); 45. /把这个样式加到单元格里面 46. cell.setCellStyle(cellStyle); 47. /给单元格设值 48. cell.setCellValue(newDouble(2008.2008); 49. elseif(j=2) 50. cellStyle=SummaryHSSF.setCellStyleAlignment(cellSty

7、le,CellStyle.ALIGN_CENTER,CellStyle.VERTICAL_CENTER); 51. cellStyle.setFont(createFonts(wb); 52. cell.setCellStyle(cellStyle); 53. cell.setCellValue(helper.createRichTextString(RichString+i+j); 54. elseif(j=3) 55. cellStyle=SummaryHSSF.setCellStyleAlignment(cellStyle,CellStyle.ALIGN_CENTER,CellStyle

8、.VERTICAL_CENTER); 56. cellStyle=SummaryHSSF.setCellFormat(helper,cellStyle,MM-yyyy-dd); 57. cell.setCellStyle(cellStyle); 58. cell.setCellValue(newDate(); 59. elseif(j=24) 60. cellStyle=SummaryHSSF.setCellStyleAlignment(cellStyle,CellStyle.ALIGN_CENTER,CellStyle.VERTICAL_CENTER); 61. cellStyle.setF

9、ont(createFonts(wb); 62. /设置公式 63. cell.setCellFormula(SUM(E+(i+1)+:X+(i+1)+); 64. else 65. cellStyle=SummaryHSSF.setCellStyleAlignment(cellStyle,CellStyle.ALIGN_CENTER,CellStyle.VERTICAL_CENTER); 66. cellStyle=SummaryHSSF.setFillBackgroundColors(cellStyle,IndexedColors.ORANGE.getIndex(),IndexedColo

10、rs.ORANGE.getIndex(),CellStyle.SOLID_FOREGROUND); 67. cell.setCellStyle(cellStyle); 68. cell.setCellValue(1); 69. 70. 71. 72. /输出 73. OutputStreamos=newFileOutputStream(newFile(c:/SummaryHSSF.xls); 74. wb.write(os); 75. os.close(); 76. 77. /* 78. *边框 79. *paramwb 80. *return 81. */82. publicstaticCellStylecreateStyleCell(Workbookwb) 83. CellStylecellStyle=wb.createCellStyle(); 84. /设置一个单元格边框颜色 85. cellStyle.setBorderBottom(CellSty

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 总结/报告

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