Java生成excel文件-本地生成excel文件-poi

男娘i 2022-06-04 06:15 422阅读 0赞
  1. package cn.itcast.poi;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. import org.apache.poi.hssf.usermodel.HSSFCell;
  8. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  9. import org.apache.poi.hssf.usermodel.HSSFFont;
  10. import org.apache.poi.hssf.usermodel.HSSFRow;
  11. import org.apache.poi.hssf.usermodel.HSSFSheet;
  12. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  13. import org.apache.poi.hssf.util.HSSFColor;
  14. import org.apache.poi.ss.util.CellRangeAddress;
  15. public class POIDemo {
  16. public static void poiExcel(String[] propertyName, List<Worker> value)
  17. throws FileNotFoundException, IOException {
  18. // 创建一个Excel文件
  19. HSSFWorkbook wb = new HSSFWorkbook();
  20. // 创建一个Excel的Sheet
  21. HSSFSheet sheet = wb.createSheet("first sheet");
  22. // 获取表需要的列数
  23. int length = propertyName.length;
  24. // ---------------下面设置表的第一行也就是通常的title----------------------
  25. // 设置行-下面为第一行
  26. HSSFRow row0 = sheet.createRow(0);
  27. // 设置列-下面为第一行的第一列
  28. HSSFCell cell00 = row0.createCell(0);
  29. // 设置字体
  30. HSSFFont headfont = wb.createFont();
  31. headfont.setFontName("黑体");
  32. // 字体大小
  33. headfont.setFontHeightInPoints((short) 22);
  34. // 加粗
  35. headfont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  36. // 设置样式
  37. HSSFCellStyle headstyle = wb.createCellStyle();
  38. // 使用了上面设置的字体样式
  39. headstyle.setFont(headfont);
  40. // 左右居中
  41. headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  42. // 上下居中
  43. headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  44. headstyle.setLocked(true);
  45. // 自动换行
  46. headstyle.setWrapText(true);
  47. // 合并单元格:参数说明:1:开始行 2:结束行 3:开始列 4:结束列
  48. // 注意,在这里使用表字段名-1来作结束列,因为我们是从0开始数的,所以要减去一个
  49. CellRangeAddress range = new CellRangeAddress(0, 0, 0, length - 1);
  50. // 将表的合并单元格样式设置进去
  51. sheet.addMergedRegion(range);
  52. // 设置表的第一行的第一列的样式
  53. cell00.setCellStyle(headstyle);
  54. // 设置表的第一行的第一列的value
  55. cell00.setCellValue("POI导出EXCEL表TITLE");
  56. // ---------------下面开始设置表的第二行,通常为字段名----------------------
  57. HSSFRow row1 = sheet.createRow(1);
  58. // 字段名使用的字体
  59. HSSFFont columnHeadFont = wb.createFont();
  60. columnHeadFont.setFontName("宋体");
  61. // 字体大小
  62. columnHeadFont.setFontHeightInPoints((short) 10);
  63. // 加粗
  64. columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
  65. // 列头的样式
  66. HSSFCellStyle columnHeadStyle = wb.createCellStyle();
  67. // 设置上面已经设置好的字体样式
  68. columnHeadStyle.setFont(columnHeadFont);
  69. // 左右居中
  70. columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  71. // 上下居中
  72. columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  73. columnHeadStyle.setLocked(true);
  74. columnHeadStyle.setWrapText(true);
  75. // 上边框的颜色
  76. columnHeadStyle.setTopBorderColor(HSSFColor.BLACK.index);
  77. // 边框的大小
  78. columnHeadStyle.setBorderTop((short) 1);
  79. // 左边框的颜色
  80. columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);
  81. // 边框的大小
  82. columnHeadStyle.setBorderLeft((short) 1);
  83. // 右边框的颜色
  84. columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);
  85. // 边框的大小
  86. columnHeadStyle.setBorderRight((short) 1);
  87. // 设置单元格的边框为粗体
  88. columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  89. // 设置单元格的边框颜色
  90. columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index);
  91. // 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)
  92. columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);
  93. // 设置第二行的行高
  94. row1.setHeight((short) 750);
  95. // 创建在这行中的列
  96. HSSFCell cell1 = null;
  97. for (int i = 0; i < length; i++) {
  98. cell1 = row1.createCell(i);
  99. // 获取数组中的表头字段名
  100. cell1.setCellValue(propertyName[i]);
  101. // 给它设置风格
  102. cell1.setCellStyle(columnHeadStyle);
  103. }
  104. // ---------------下面开始设置表里面的内容-----------------------------
  105. // 设置字体
  106. HSSFFont font = wb.createFont();
  107. font.setFontName("宋体");
  108. font.setFontHeightInPoints((short) 10);
  109. // 设置其风格
  110. HSSFCellStyle style = wb.createCellStyle();
  111. style.setFont(font);
  112. // 左右居中
  113. style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  114. // 上下居中
  115. style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
  116. style.setWrapText(true);
  117. style.setLeftBorderColor(HSSFColor.BLACK.index);
  118. style.setBorderLeft((short) 1);
  119. style.setRightBorderColor(HSSFColor.BLACK.index);
  120. style.setBorderRight((short) 1);
  121. // 设置单元格的边框为粗体
  122. style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  123. // 设置单元格的边框颜色.
  124. style.setBottomBorderColor(HSSFColor.BLACK.index);
  125. // 设置单元格的背景颜色.
  126. style.setFillForegroundColor(HSSFColor.WHITE.index);
  127. HSSFRow row = null;
  128. HSSFCell cell = null;
  129. int valueStartRow = 2;
  130. // 赋值
  131. for (Worker worker : value) {
  132. row = sheet.createRow(valueStartRow);
  133. cell = row.createCell(0);
  134. cell.setCellValue(worker.getId());
  135. cell.setCellStyle(style);
  136. cell = row.createCell(1);
  137. cell.setCellValue(worker.getName());
  138. cell.setCellStyle(style);
  139. cell = row.createCell(2);
  140. cell.setCellValue(worker.getGender());
  141. cell.setCellStyle(style);
  142. cell = row.createCell(3);
  143. cell.setCellValue(worker.getAge());
  144. cell.setCellStyle(style);
  145. cell = row.createCell(4);
  146. cell.setCellValue(worker.getAddress());
  147. cell.setCellStyle(style);
  148. cell = row.createCell(5);
  149. cell.setCellValue(worker.getWage());
  150. cell.setCellStyle(style);
  151. valueStartRow++;
  152. }
  153. wb.write(new FileOutputStream("G:/excel.xls"));
  154. }
  155. public static void main(String[] args) throws Exception {
  156. String[] name = { "ID", "姓名", "性别", "年龄", "地址", "工资" };
  157. List<Worker> list = new ArrayList<Worker>();
  158. list.add(new Worker(1, "张三", "男", 23, "北京市", "3500"));
  159. list.add(new Worker(2, "李四", "男", 22, "上海市", "3300"));
  160. list.add(new Worker(3, "王五", "男", 21, "深圳市", "3100"));
  161. list.add(new Worker(4, "赵六", "男", 25, "杭州市", "3100"));
  162. list.add(new Worker(5, "田七", "男", 26, "天津市", "3200"));
  163. poiExcel(name, list);
  164. }
  165. }

项目源码:http://download.csdn.net/download/baidu\_32492845/10147840

发表评论

表情:
评论列表 (有 0 条评论,422人围观)

还没有评论,来说两句吧...

相关阅读