Java导出Excel给每一列设置不同样式示例

打印 上一主题 下一主题

主题 1014|帖子 1014|积分 3042

Excel导出这里不讲,方法很多,原生的POI可以参照 Java原生POI实现的Excel导入导出(简单易懂)
这里只说怎么给Excel每一列设置不同的样式,好比下面如许的

直接上代码
  1. @Override
  2.     public void exportTemplate(HttpServletRequest request, HttpServletResponse response) throws Exception {
  3.         try (Workbook wb = new SXSSFWorkbook(1000)) {
  4.             // 构建一个导出页
  5.             Sheet sheet = wb.createSheet("用户信息表");
  6.             CellStyle style = wb.createCellStyle();
  7.             style.setAlignment(HorizontalAlignment.CENTER);
  8.             style.setVerticalAlignment(VerticalAlignment.CENTER);
  9.             style.setBorderBottom(BorderStyle.THIN);
  10.             style.setBorderLeft(BorderStyle.THIN);
  11.             style.setBorderTop(BorderStyle.THIN);
  12.             style.setBorderRight(BorderStyle.THIN);
  13.             style.setWrapText(true);// 自动换行
  14.             Font font = wb.createFont();
  15.             font.setFontName("宋体");
  16.             font.setFontHeightInPoints((short) 11);
  17.             style.setFont(font);
  18.             DataFormat format = wb.createDataFormat();
  19.             style.setDataFormat(format.getFormat("@")); // 设置为文本格式
  20.             sheet.setDefaultRowHeight((short) 500);
  21.             // 构建excel抬头
  22.             Row row0 = sheet.createRow((int) 0); // 第一行
  23.             Cell cell0_0 = row0.createCell(0);
  24.             cell0_0.setCellValue("序号");
  25.             cell0_0.setCellStyle(style);
  26.             sheet.setColumnWidth(0, 15 * 256);// 列宽
  27.             sheet.setDefaultColumnStyle(0, style);
  28.             Cell cell0_1 = row0.createCell(1);
  29.             cell0_1.setCellValue("姓名");
  30.             cell0_1.setCellStyle(style);
  31.             sheet.setColumnWidth(1, 15 * 256);// 列宽
  32.             sheet.setDefaultColumnStyle(1, style);
  33.             Cell cell0_2 = row0.createCell(2);
  34.             cell0_2.setCellValue("国家标识码");
  35.             cell0_2.setCellStyle(style);
  36.             sheet.setColumnWidth(2, 15 * 256);// 列宽
  37.             sheet.setDefaultColumnStyle(2, style);
  38.             Cell cell0_3 = row0.createCell(3);
  39.             cell0_3.setCellValue("手机号");
  40.             cell0_3.setCellStyle(style);
  41.             sheet.setColumnWidth(3, 15 * 256);// 列宽
  42.             sheet.setDefaultColumnStyle(3, style);
  43.             Cell cell0_4 = row0.createCell(4);
  44.             cell0_4.setCellValue("身份证号");
  45.             cell0_4.setCellStyle(style);
  46.             sheet.setColumnWidth(4, 20 * 256);// 列宽
  47.             sheet.setDefaultColumnStyle(4, style);
  48.             Cell cell0_5 = row0.createCell(5);
  49.             cell0_5.setCellValue("部门名称(选填)");
  50.             cell0_5.setCellStyle(style);
  51.             sheet.setColumnWidth(5, 20 * 256);// 列宽
  52.             sheet.setDefaultColumnStyle(5, style);
  53.             Cell cell0_6 = row0.createCell(6);
  54.             cell0_6.setCellValue("职位(选填,多个用英文/分隔)");
  55.             cell0_6.setCellStyle(style);
  56.             sheet.setColumnWidth(6, 35 * 256);// 列宽
  57.             sheet.setDefaultColumnStyle(6, style);
  58.             // 构建示例
  59.             Row row1 = sheet.createRow((int) 1); // 第二行
  60.             CellStyle style1 = wb.createCellStyle();
  61.             style1.setAlignment(HorizontalAlignment.CENTER);
  62.             style1.setVerticalAlignment(VerticalAlignment.CENTER);
  63.             style1.setBorderBottom(BorderStyle.THIN);
  64.             style1.setBorderLeft(BorderStyle.THIN);
  65.             style1.setBorderTop(BorderStyle.THIN);
  66.             style1.setBorderRight(BorderStyle.THIN);
  67.             style1.setWrapText(true);// 自动换行
  68.             style1.setFont(font);
  69.             DataFormat format1 = wb.createDataFormat();
  70.             style1.setDataFormat(format1.getFormat("@")); // 设置为文本格式
  71.             style1.setFillForegroundColor((short) 13);// 设置背景色
  72.             style1.setFillPattern(FillPatternType.SOLID_FOREGROUND);
  73.             Cell cell1_0 = row1.createCell(0);
  74.             cell1_0.setCellValue("示例(保留)");
  75.             cell1_0.setCellStyle(style1);
  76.             Cell cell1_1 = row1.createCell(1);
  77.             cell1_1.setCellValue("张三");
  78.             cell1_1.setCellStyle(style1);
  79.             Cell cell1_2 = row1.createCell(2);
  80.             cell1_2.setCellValue("+86");
  81.             cell1_2.setCellStyle(style1);
  82.             Cell cell1_3 = row1.createCell(3);
  83.             cell1_3.setCellValue("13700002222");
  84.             cell1_3.setCellStyle(style1);
  85.             Cell cell1_4 = row1.createCell(4);
  86.             cell1_4.setCellValue("452186200001010001");
  87.             cell1_4.setCellStyle(style1);
  88.             Cell cell1_5 = row1.createCell(5);
  89.             cell1_5.setCellValue("技术部");
  90.             cell1_5.setCellStyle(style1);
  91.             Cell cell1_6 = row1.createCell(6);
  92.             cell1_6.setCellValue("科室主任/细胞生物学家/博士生导师");
  93.             cell1_6.setCellStyle(style1);
  94.             // 构建第二列
  95.             Row row2 = sheet.createRow((int) 2); // 第一行
  96.             Cell cell2_0 = row2.createCell(0);
  97.             cell2_0.setCellValue("1");
  98.             cell2_0.setCellStyle(style);
  99.             Cell cell2_1 = row2.createCell(1);
  100.             cell2_1.setCellValue("");
  101.             cell2_1.setCellStyle(style);
  102.             Cell cell2_2 = row2.createCell(2);
  103.             cell2_2.setCellValue("");
  104.             cell2_2.setCellStyle(style);
  105.             Cell cell2_3 = row2.createCell(3);
  106.             cell2_3.setCellValue("");
  107.             cell2_3.setCellStyle(style);
  108.             Cell cell2_4 = row2.createCell(4);
  109.             cell2_4.setCellValue("");
  110.             cell2_4.setCellStyle(style);
  111.             Cell cell2_5 = row2.createCell(5);
  112.             cell2_5.setCellValue("");
  113.             cell2_5.setCellStyle(style);
  114.             Cell cell2_6 = row2.createCell(6);
  115.             cell2_6.setCellValue("");
  116.             cell2_6.setCellStyle(style);
  117.             String fileName = "用户信息表.xlsx";
  118.             if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) {
  119.                 fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1"); // firefox浏览器
  120.             } else if (request.getHeader("User-Agent").toUpperCase().indexOf("MSIE") > 0) {
  121.                 fileName = URLEncoder.encode(fileName, "UTF-8");// IE浏览器
  122.             } else if (request.getHeader("User-Agent").toUpperCase().indexOf("CHROME") > 0) {
  123.                 fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1");// 谷歌
  124.             }
  125.             response.reset();
  126.             response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");  
  127.             response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
  128.             wb.write(response.getOutputStream());
  129.             response.getOutputStream().close();
  130.         }
  131.     }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

知者何南

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表