15. for(int i=0;i<60;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(int j=0;j<25;j++){
23. //设置每个sheet每一行的宽度,自动,根据需求自行确定
24. sheet1.autoSizeColumn(j+1, true); 25. //创建一个基本的样式
26. CellStyle cellStyle = 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.setFont(createFonts(wb)); 34. //把这个样式加到单元格里面
35. cell.setCellStyle(cellStyle);
36. //给单元格设值
37. cell.setCellValue(true); 38. }else if(j==1){
39. //设置文字在单元格里面的位置
40. cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
41. //设置这个样式的格式(Format)
42. cellStyle = SummaryHSSF.setCellFormat(helper,cellStyle, \
43. //先创建字体样式,并把这个样式加到单元格的字体里面
44. cellStyle.setFont(createFonts(wb)); 45. //把这个样式加到单元格里面 46. cell.setCellStyle(cellStyle); 47. //给单元格设值
48. cell.setCellValue(new Double(2008.2008));
49. }else if(j==2){
50. cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
51. cellStyle.setFont(createFonts(wb)); 52. cell.setCellStyle(cellStyle);
53. cell.setCellValue(helper.createRichTextString(\54. }else if(j==3){
55. cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
56. cellStyle = SummaryHSSF.setCellFormat(helper,cellStyle, \
57. cell.setCellStyle(cellStyle); 58. cell.setCellValue(new Date()); 59. }else if(j==24){
60. cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
61. cellStyle.setFont(createFonts(wb)); 62. //设置公式
63. cell.setCellFormula(\)+\
64. }else{
65. cellStyle = SummaryHSSF.setCellStyleAlignment(cellStyle, CellStyle.ALIGN_CENTER, CellStyle.VERTICAL_CENTER);
66. cellStyle = SummaryHSSF.setFillBackgroundColors(cellStyle,IndexedColors.ORANGE.getIndex(),IndexedColors.ORANGE.getIndex(),CellStyle.SOLID_FOREGROUND);
67. cell.setCellStyle(cellStyle); 68. cell.setCellValue(1); 69. } 70. } 71. }
72. //输出
73. OutputStream os = new FileOutputStream(new File(\mmaryHSSF.xls\74. wb.write(os); 75. os.close(); 76. } 77. /** 78. * 边框
79. * @param wb 80. * @return 81. */
82. public static CellStyle createStyleCell(Workbook wb){ 83. CellStyle cellStyle = wb.createCellStyle(); 84. //设置一个单元格边框颜色
85. cellStyle.setBorderBottom(CellStyle.BORDER_THIN); 86. cellStyle.setBorderTop(CellStyle.BORDER_THIN); 87. cellStyle.setBorderLeft(CellStyle.BORDER_THIN); 88. cellStyle.setBorderRight(CellStyle.BORDER_THIN); 89. //设置一个单元格边框颜色
90. cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
91. cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
92. cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
93. cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
94. return cellStyle; 95. } 96. /**
97. * 设置文字在单元格里面的位置 98. * CellStyle.ALIGN_CENTER 99. * CellStyle.VERTICAL_CENTER 100. * @param cellStyle 101. * @param halign 102. * @param valign 103. * @return 104. */
105. public static CellStyle setCellStyleAlignment(CellStyle cellStyle,short halign,short valign){ 106. //设置上下
107. cellStyle.setAlignment(CellStyle.ALIGN_CENTER); 108. //设置左右
109. cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
110. return cellStyle; 111. } 112. /**
113. * 格式化单元格
114. * 如#,##0.00,m/d/yy去HSSFDataFormat或XSSFDataFormat里面找
115. * @param cellStyle
116. * @param fmt 117. * @return 118. */
119. public static CellStyle setCellFormat(CreationHelper helper,CellStyle cellStyle,String fmt){ 120. //还可以用其它方法创建format
121. cellStyle.setDataFormat(helper.createDataFormat().getFormat(fmt));
122. return cellStyle; 123. } 124. /**
125. * 前景和背景填充的着色 126. * @param cellStyle
127. * @param bg IndexedColors.ORANGE.getIndex(); 128. * @param fg IndexedColors.ORANGE.getIndex(); 129. * @param fp CellStyle.SOLID_FOREGROUND 130. * @return 131. */
132. public static CellStyle setFillBackgroundColors(CellStyle cellStyle,short bg,short fg,short fp){
133. //cellStyle.setFillBackgroundColor(bg); 134. cellStyle.setFillForegroundColor(fg); 135. cellStyle.setFillPattern(fp); 136. return cellStyle; 137. } 138. /**
139. * 设置字体 140. * @param wb 141. * @return 142. */
143. public static Font createFonts(Workbook wb){ 144. //创建Font对象
145. Font font = wb.createFont(); 146. //设置字体
147. font.setFontName(\黑体\148. //着色
149. font.setColor(HSSFColor.BLUE.index); 150. //斜体
151. font.setItalic(true); 152. //字体大小
153. font.setFontHeight((short)300); 154. return font; 155. } 156. }
读取Excel文件 Java代码
1. public class ReadExcel {
2. public static void main(String[] args) throws Exception { 3. InputStream is = new FileInputStream(new File(\aryHSSF.xls\
4. //根据输入流创建Workbook对象
5. Workbook wb = WorkbookFactory.create(is); 6. //get到Sheet对象
7. Sheet sheet = wb.getSheetAt(0); 8. //这个必须用接口
9. for(Row row : sheet){ 10. for(Cell cell : row){
11. //cell.getCellType是获得cell里面保存的值的type
12. //如Cell.CELL_TYPE_STRING 13. switch(cell.getCellType()){
14. case Cell.CELL_TYPE_BOOLEAN: 15. //得到Boolean对象的方法
16. System.out.print(cell.getBooleanCellValue()+\
17. break;
18. case Cell.CELL_TYPE_NUMERIC: 19. //先看是否是日期格式
20. if(DateUtil.isCellDateFormatted(cell)){
21. //读取日期格式
22. System.out.print(cell.getDateCellValue()+\
23. }else{
24. //读取数字
25. System.out.print(cell.getNumericCellValue()+\
26. }
27. break;
28. case Cell.CELL_TYPE_FORMULA: 29. //读取公式
30. System.out.print(cell.getCellFormula()+\
31. break;
32. case Cell.CELL_TYPE_STRING: 33. //读取String