poi导出excel设置样式

朱雀 2020-10-01 02:19 1188阅读 0赞

示例代码

  1. /**
  2. *
  3. * @Date 2018年9月11日 下午3:53:26
  4. * @Fcunction exportExcel
  5. * @param sheetName
  6. * @param value_columns1
  7. * @param value_columns2
  8. * @param value_columns3
  9. * @param list
  10. * @return HSSFWorkbook
  11. *
  12. */
  13. public static HSSFWorkbook exportExcel(String sheetName, String[] value_columns1,String[] value_columns2,String[] value_columns3, List<List<String>> list,String name){
  14. //创建一个Excel文件
  15. HSSFWorkbook workbook = new HSSFWorkbook();
  16. //创建一个工作表
  17. HSSFSheet sheet = workbook.createSheet(sheetName);
  18. //设置宽度
  19. for (int i = 0; i < value_columns1.length; i++) {
  20. sheet.setColumnWidth(i, 3000);
  21. }
  22. //设置单元格格式居中
  23. HSSFCellStyle titleStyle = workbook.createCellStyle(); //标题
  24. HSSFCellStyle headStyle = workbook.createCellStyle(); //表头
  25. HSSFCellStyle cellStyle = workbook.createCellStyle(); //表格内容
  26. HSSFCellStyle twoStyle = workbook.createCellStyle(); //第二行不居中显示
  27. HSSFCellStyle remarkStyle = workbook.createCellStyle(); //备注,第二行不居中显示
  28. HSSFCellStyle recordStyle = workbook.createCellStyle(); //记录内容
  29. HSSFCellStyle recordHumanLeftStyle = workbook.createCellStyle(); //记录人左边
  30. HSSFCellStyle recordHumanRightStyle = workbook.createCellStyle(); //记录人右边
  31. //设置单元格样式
  32. HSSFFont titleFont = workbook.createFont(); //标题字体
  33. titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //宽度
  34. titleFont.setFontHeightInPoints((short)16); //字号
  35. titleStyle.setFont(titleFont);
  36. titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  37. HSSFFont headFont = workbook.createFont(); //表头字体
  38. headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //宽度
  39. headFont.setFontHeightInPoints((short)16); //字号
  40. headStyle.setFont(headFont);
  41. headStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
  42. headStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
  43. headStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  44. headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平居中
  45. headStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //垂直居中
  46. headStyle.setWrapText(true); //自动换行
  47. cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  48. cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
  49. cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
  50. cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  51. cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER); //水平居中
  52. cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //垂直居中
  53. remarkStyle.setWrapText(true); //自动换行
  54. remarkStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); //垂直居中
  55. remarkStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  56. remarkStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
  57. remarkStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
  58. remarkStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  59. remarkStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
  60. twoStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
  61. recordStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_TOP); //垂直居上
  62. recordStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
  63. recordStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
  64. recordStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  65. recordStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
  66. recordHumanLeftStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
  67. recordHumanLeftStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  68. recordHumanRightStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
  69. recordHumanRightStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
  70. recordHumanRightStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);
  71. //添加表头行
  72. HSSFRow hssfRow = sheet.createRow(0);
  73. //第一行标题
  74. HSSFCell titleCell = hssfRow.createCell(0);
  75. titleCell.setCellValue(sheetName);
  76. titleCell.setCellStyle(titleStyle);
  77. //合并第一行所有列
  78. sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, value_columns1.length-1));
  79. //第二行
  80. hssfRow = sheet.createRow(1);
  81. for (int i = 0; i < value_columns1.length; i++) {
  82. HSSFCell twoCell = hssfRow.createCell(i);
  83. if(i==0){
  84. twoCell.setCellValue("日期:"+new SimpleDateFormat("yyyy年M月d日").format(new Date()));
  85. twoCell.setCellStyle(twoStyle);
  86. }else if(i==value_columns1.length-3){
  87. twoCell.setCellValue("交班领导:"+name);
  88. twoCell.setCellStyle(twoStyle);
  89. }
  90. }
  91. //合并第二行
  92. sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, value_columns1.length-4));
  93. sheet.addMergedRegion(new CellRangeAddress(1, 1, value_columns1.length-3, value_columns1.length-1));
  94. //第三行开始表格
  95. hssfRow = sheet.createRow(2);
  96. for (int i = 0; i < value_columns1.length; i++) {
  97. //添加表头内容
  98. HSSFCell headCell = hssfRow.createCell(i);
  99. headCell.setCellValue(value_columns1[i]);
  100. headCell.setCellStyle(cellStyle);
  101. }
  102. hssfRow = sheet.createRow(3);
  103. for (int i = 0; i < value_columns2.length; i++) {
  104. //添加表头内容
  105. HSSFCell headCell = hssfRow.createCell(i);
  106. headCell.setCellValue(value_columns2[i]);
  107. headCell.setCellStyle(cellStyle);
  108. }
  109. hssfRow = sheet.createRow(4);
  110. for (int i = 0; i < value_columns3.length; i++) {
  111. //添加表头内容
  112. HSSFCell headCell = hssfRow.createCell(i);
  113. headCell.setCellValue(value_columns3[i]);
  114. headCell.setCellStyle(cellStyle);
  115. }
  116. //合并表头(3、4、5行)
  117. sheet.addMergedRegion(new CellRangeAddress(2, 4, 0, 0));
  118. sheet.addMergedRegion(new CellRangeAddress(2, 3, 1, 2));
  119. sheet.addMergedRegion(new CellRangeAddress(2, 3, 3, 4));
  120. sheet.addMergedRegion(new CellRangeAddress(2, 2, 5, 8));
  121. sheet.addMergedRegion(new CellRangeAddress(3, 3, 5, 6));
  122. sheet.addMergedRegion(new CellRangeAddress(3, 3, 7, 8));
  123. sheet.addMergedRegion(new CellRangeAddress(2, 3, 9, 10));
  124. //把数据添加到excel
  125. for (int i = 0; i < list.size(); i++) {
  126. hssfRow = sheet.createRow(i + 5);
  127. for (int j = 0; j < list.get(i).size(); j++) {
  128. //创建单元格,并设置值
  129. HSSFCell cell = hssfRow.createCell(j);
  130. cell.setCellValue(list.get(i).get(j));
  131. if(i==list.size()-1&&j!=0){
  132. cell.setCellStyle(remarkStyle);
  133. }else {
  134. cell.setCellStyle(cellStyle);
  135. }
  136. }
  137. }
  138. for (int i = 5+list.size(); i < 5+list.size()+2; i++) {
  139. hssfRow = sheet.createRow(i);
  140. for (int j = 0; j < value_columns1.length; j++) {
  141. HSSFCell cell = hssfRow.createCell(j);
  142. cell.setCellValue("");
  143. cell.setCellStyle(remarkStyle);
  144. }
  145. }
  146. sheet.addMergedRegion(new CellRangeAddress(4+list.size(), 4+list.size()+2, 0, 0));
  147. sheet.addMergedRegion(new CellRangeAddress(4+list.size(), 4+list.size()+2, 1, 10));
  148. //会议记录
  149. for (int i = 7+list.size(); i < 7+list.size()+5; i++) {
  150. hssfRow = sheet.createRow(i);
  151. for (int j = 0; j < value_columns1.length; j++) {
  152. HSSFCell cell = hssfRow.createCell(j);
  153. if(i == 7+list.size()&&j==0) {
  154. cell.setCellValue("会议记录:");
  155. }else {
  156. cell.setCellValue("");
  157. }
  158. cell.setCellStyle(recordStyle);
  159. }
  160. }
  161. sheet.addMergedRegion(new CellRangeAddress(7+list.size(), 7+list.size()+4, 0, 10));
  162. //记录人
  163. hssfRow = sheet.createRow(7+list.size()+5);
  164. for (int j = 0; j < value_columns1.length; j++) {
  165. HSSFCell cell = hssfRow.createCell(j);
  166. if(j>=value_columns1.length-3) {
  167. cell.setCellValue("记录人:");
  168. cell.setCellStyle(recordHumanRightStyle);
  169. }else {
  170. cell.setCellValue("");
  171. cell.setCellStyle(recordHumanLeftStyle);
  172. }
  173. }
  174. sheet.addMergedRegion(new CellRangeAddress(7+list.size()+5, 7+list.size()+5, 0, value_columns1.length-4));
  175. sheet.addMergedRegion(new CellRangeAddress(7+list.size()+5, 7+list.size()+5, value_columns1.length-3, value_columns1.length-1));
  176. return workbook;
  177. }

setColumnWidth方法设置具体某一列的宽度(第一个参数是第几列从0开始,第二个参数是宽度值)

  1. HSSFSheet sheet = workbook.createSheet(date+sheetName);
  2. sheet.setColumnWidth((short) 6, (short) 1600);

设置不同字体,如下,titleStyle是加粗的,默认字号大小为10;

  1. HSSFCellStyle titleStyle = workbook.createCellStyle();
  2. //设置字体
  3. HSSFFont font =workbook.createFont();
  4. font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); //加粗
  5. font.setFontHeightInPoints((short)16); //字号
  6. titleStyle.setFont(font);
  7. HSSFCellStyle cellStyle = workbook.createCellStyle();

设置自动换行,setWrapText(true);内容里加\r\n,在对应地方自动换行;不加\r\n按照单元格宽度自动换行;不调用setWrapText(true)方法,只在内容加\r\n,只有点开单元格时才换行

  1. remarkStyle.setWrapText(true); //自动换行
  2. cell.setCellValue("hello\r\n word");

发表评论

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

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

相关阅读