当前位置:   article > 正文

POI导出excel单元格宽度自适应_hssfcellstyle 设置单元格宽度

hssfcellstyle 设置单元格宽度
  1. package com.tgpms.util;
  2. import cn.hutool.core.util.StrUtil;
  3. import org.apache.poi.hssf.usermodel.*;
  4. import org.apache.poi.ss.usermodel.Cell;
  5. import org.apache.poi.ss.usermodel.Font;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.io.OutputStream;
  10. import java.util.HashMap;
  11. import java.util.LinkedList;
  12. import java.util.List;
  13. import java.util.Map;
  14. /**
  15. * @author jiangli
  16. * @since 2021/1/26 17:42
  17. */
  18. public class ExportExcelUtil {
  19. public static void downloadExcel(String excelName, List<String> tableHead, List<LinkedList<String>> tableBody, HttpServletResponse response) throws IOException {
  20. // 1:创建一个workbook
  21. HSSFWorkbook workbook = new HSSFWorkbook();
  22. // 创建样式
  23. HSSFCellStyle style = workbook.createCellStyle();
  24. Font font = workbook.createFont();
  25. font.setBoldweight(Font.BOLDWEIGHT_BOLD); // 粗体
  26. style.setFont(font);
  27. style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
  28. style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中
  29. style.setBorderTop((short) 1);
  30. style.setBorderBottom((short) 1);
  31. style.setBorderLeft((short) 1);
  32. style.setBorderRight((short) 1);
  33. style.setWrapText(true);
  34. // 设置合计样式
  35. HSSFCellStyle style1 = workbook.createCellStyle();
  36. style1.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
  37. style1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 垂直居中
  38. style1.setBorderTop((short) 1);
  39. style1.setBorderBottom((short) 1);
  40. style1.setBorderLeft((short) 1);
  41. style1.setBorderRight((short) 1);
  42. style.setWrapText(true);
  43. HSSFSheet sheet = (HSSFSheet) workbook.createSheet(excelName);
  44. // // 2:合并单元格,表头。并设置值
  45. // CellRangeAddress cra = new CellRangeAddress(0, 0, 0, tableHead.size() - 1);
  46. // sheet.addMergedRegion(cra);
  47. // HSSFRow row = sheet.createRow(0);
  48. // HSSFCell tableName = row.createCell(0);
  49. // tableName.setCellStyle(style);
  50. // tableName.setCellValue(excelName);
  51. //存储最大列宽
  52. Map<Integer,Integer> maxWidth = new HashMap<>();
  53. // 3:设置表head
  54. HSSFRow row1 = sheet.createRow(0);
  55. for (int i = 0; i < tableHead.size(); i++) {
  56. Cell createCell = row1.createCell(i);
  57. createCell.setCellValue(tableHead.get(i));
  58. createCell.setCellStyle(style);
  59. maxWidth.put(i,createCell.getStringCellValue().getBytes().length * 256 + 200);
  60. }
  61. // 4:表格内容
  62. for (int i = 0; i < tableBody.size(); i++) {
  63. HSSFRow rows = sheet.createRow(i + 1);
  64. int j = 0;
  65. LinkedList<String> linkedList = tableBody.get(i);
  66. for (String s : linkedList) {
  67. HSSFCell createCell = rows.createCell(j);
  68. if(StrUtil.isNotBlank(s)){
  69. createCell.setCellValue(s);
  70. }else{
  71. createCell.setCellValue("");
  72. }
  73. int length = createCell.getStringCellValue().getBytes().length * 256 + 200;
  74. //这里把宽度最大限制到15000
  75. if (length>15000){
  76. length = 15000;
  77. }
  78. maxWidth.put(j,Math.max(length,maxWidth.get(j)));
  79. j++;
  80. createCell.setCellStyle(style1);
  81. }
  82. }
  83. // 列宽自适应
  84. for (int i = 0; i < tableHead.size(); i++) {
  85. sheet.setColumnWidth(i,maxWidth.get(i));
  86. }
  87. // 5:设置头
  88. response.setHeader("Content-disposition",
  89. "attachment; filename=" + new String(excelName.getBytes("GB2312"), "ISO8859-1") + ".xls");
  90. // 6:设置头类型
  91. response.setContentType("application/vnd.ms-excel");
  92. // 7:写出
  93. OutputStream toClient = response.getOutputStream();
  94. workbook.write(toClient);
  95. toClient.flush();
  96. toClient.close();
  97. }
  98. }
  1. public void exportSummaryInfo(HealthQueryParam healthQueryParam, HttpServletResponse response) {
  2. ManagementSysuser managementSysuser = LoginInterceptor.loginUser.get();
  3. if (managementSysuser == null) {
  4. throw new ProjectException("请先登录");
  5. }
  6. String deptId = managementSysuser.getDeptId();
  7. ManagementDept dept = deptMapper.selectById(deptId);
  8. if (healthQueryParam.getPageNo() != null && healthQueryParam.getSize() != null) {
  9. PageHelper.startPage(healthQueryParam.getPageNo(), healthQueryParam.getSize());
  10. }
  11. List<HealthSummary> list = mapper.selectList(new LambdaQueryWrapper<HealthSummary>().eq(StrUtil.isNotBlank(healthQueryParam.getOpenId()), HealthSummary::getOpenId, healthQueryParam.getOpenId())
  12. .like(StrUtil.isNotBlank(healthQueryParam.getIdCard()), HealthSummary::getIdCard, healthQueryParam.getIdCard())
  13. .like(StrUtil.isNotBlank(healthQueryParam.getPhone()), HealthSummary::getPhone, healthQueryParam.getPhone())
  14. .like(StrUtil.isNotBlank(healthQueryParam.getPeopleName()), HealthSummary::getPeopleName, healthQueryParam.getPeopleName())
  15. .like(StrUtil.isNotBlank(healthQueryParam.getMainContractor()), HealthSummary::getMainContractor, healthQueryParam.getMainContractor())
  16. .eq(healthQueryParam.getProfileAuditing() != null, HealthSummary::getProfileAuditing, healthQueryParam.getProfileAuditing())
  17. .eq(healthQueryParam.getHealthAuditing() != null, HealthSummary::getHealthAuditing, healthQueryParam.getHealthAuditing())
  18. .eq(!"-1".equals(dept.getPId()), HealthSummary::getMainContractor, dept.getId())
  19. .ge(healthQueryParam.getInTimeStart() != null, HealthSummary::getBhtTime, healthQueryParam.getInTimeStart())
  20. .le(healthQueryParam.getInTimeEnd() != null, HealthSummary::getBhtTime, healthQueryParam.getInTimeEnd())
  21. .ge(healthQueryParam.getOutTimeStart() != null, HealthSummary::getOutTime, healthQueryParam.getOutTimeStart())
  22. .le(healthQueryParam.getOutTimeEnd() != null, HealthSummary::getOutTime, healthQueryParam.getOutTimeEnd())
  23. .eq(HealthSummary::getDeleted, 0)
  24. .orderByDesc(HealthSummary::getInsertDate));
  25. list.forEach(e -> {
  26. // 部门
  27. ManagementDept managementDept = deptMapper.selectById(e.getMainContractor());
  28. e.setMainContractorCN(managementDept.getDName());
  29. e.setProfileAuditingCN(e.getProfileAuditing() == 0 ? "审核中" : (e.getProfileAuditing() == 1 ? "审核已通过" : "驳回"));
  30. e.setHealthAuditingCN(e.getHealthAuditing() == 0 ? "审核中" : (e.getHealthAuditing() == 1 ? "审核已通过" : "驳回"));
  31. });
  32. List<String> tableHead = Arrays.asList("序号", "单位", "部门/分包单位", "姓名", "性别", "年龄", "身份证号码", "返岗前住址", "联系电话", "返岗前核酸检测时间", "返岗前核酸检测结果",
  33. "返岗前健康码状态", "返岗前体温", "返岗时间", "返岗乘坐交通工具", "交通工具时间班次车牌号", "到达白鹤滩工地时间", "白鹤滩工地居住地", "到达后核酸检测时间", "到达后核酸检测结果", "到达后健康码状态",
  34. "到达后体温", "是否乘坐非正规交通工具", "是否独自返乡,同行人员姓名", "同行人员住址", "同行人员联系电话", "是否有发热、咳嗽", "公司责任人", "项目责任人", "劳务责任人", "备注",
  35. "员工个人信息审核状态","员工返岗信息审核状态");
  36. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  37. List<LinkedList<String>> tableBody = new LinkedList<>();
  38. for (int i = 0; i < list.size(); i++) {
  39. LinkedList<String> linkedList = new LinkedList<>();
  40. linkedList.add(String.valueOf(i+1));
  41. linkedList.add(list.get(i).getMainContractorCN());
  42. linkedList.add(list.get(i).getSubContractor());
  43. linkedList.add(list.get(i).getPeopleName());
  44. linkedList.add(list.get(i).getSex() == 0 ? "男" : "女");
  45. linkedList.add(String.valueOf(list.get(i).getAge()));
  46. linkedList.add(list.get(i).getIdCard());
  47. linkedList.add(list.get(i).getReturnAddress());
  48. linkedList.add(list.get(i).getPhone());
  49. linkedList.add(list.get(i).getReturnCheckTime() != null ? format.format(list.get(i).getReturnCheckTime()) : "");
  50. linkedList.add(list.get(i).getReturnCheckResult());
  51. linkedList.add(list.get(i).getReturnHealthCode());
  52. linkedList.add(list.get(i).getReturnBodyTemperature());
  53. linkedList.add(list.get(i).getReturnTime() != null ? format.format(list.get(i).getReturnTime()) : "");
  54. linkedList.add(list.get(i).getReturnTransportation());
  55. linkedList.add(list.get(i).getReturnTransportationNo());
  56. linkedList.add(list.get(i).getBhtTime() != null ? format.format(list.get(i).getBhtTime()) : "");
  57. linkedList.add(list.get(i).getBhtAddress());
  58. linkedList.add(list.get(i).getBhtCheckTime() != null ? format.format(list.get(i).getBhtCheckTime()) : "");
  59. linkedList.add(list.get(i).getBhtCheckResult());
  60. linkedList.add(list.get(i).getHealthCode());
  61. linkedList.add(list.get(i).getBodyTemperature());
  62. linkedList.add(list.get(i).getInformalTransportation());
  63. linkedList.add("是");
  64. linkedList.add("无");
  65. linkedList.add("无");
  66. linkedList.add(list.get(i).getFever() == 0 ? "否" : "是");
  67. linkedList.add(list.get(i).getCompanyOwner());
  68. linkedList.add(list.get(i).getProjectOwner());
  69. linkedList.add(list.get(i).getServiceOwner());
  70. linkedList.add(list.get(i).getRemark());
  71. linkedList.add(list.get(i).getProfileAuditingCN());
  72. linkedList.add(list.get(i).getHealthAuditingCN());
  73. tableBody.add(linkedList);
  74. }
  75. try {
  76. ExportExcelUtil.downloadExcel("员工返岗健康信息", tableHead, tableBody, response);
  77. } catch (IOException e) {
  78. e.printStackTrace();
  79. }
  80. }

效果

 

第二种方式

  1. // 设置自动列宽
  2. for (int i = 0; i < headers.length; i++) {
  3. sheet.autoSizeColumn(i);
  4. sheet.setColumnWidth(i, sheet.getColumnWidth(i) * 2);
  5. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/812269
推荐阅读
相关标签
  

闽ICP备14008679号