Java创建Excel文件模板并实现浏览器下载

时间:2025-04-10 07:59:17
  • public void exportExcel(HttpServletResponse response) {
  • String fileName = "测试模板_"+() + ".xlsx";
  • List<String> awardPrinciple = ("标题1", "标题2", "标题3");
  • 创建工作薄
  • XSSFWorkbook work = new XSSFWorkbook();
  • XSSFSheet sheet1 = ("测试模板");
  • //设置列宽
  • (0, 25 * 256);
  • (1, 25 * 256);
  • (2, 25 * 256);
  • //创建单元格样式
  • CellStyle cellStyle = ();
  • //设置背景色为灰色
  • (IndexedColors.GREY_25_PERCENT.getIndex());
  • (FillPatternType.SOLID_FOREGROUND);
  • //设置字体样式
  • Font font = ();
  • (true);
  • (());
  • (font);
  • //设置表格居中
  • ();
  • ();
  • //设置边框
  • ();
  • ();
  • ();
  • ();
  • //创建行
  • XSSFRow row1 = (0);
  • ((short) 500);
  • for (int i = 0; i < (); i++) {
  • XSSFCell cell1 = (i);
  • ((i));
  • (cellStyle);
  • }
  • //使用response导出
  • OutputStream output;
  • try {
  • output = ();
  • ();
  • ("application/octet-stream;charset=UTF-8");
  • ("UTF-8");
  • ("Content-Disposition", "attachment;filename=" + (fileName, "utf8"));
  • ("Pragma", "no-cache");
  • ("Cache-Control", "no-cache");
  • (output);
  • ();
  • } catch (IOException e) {
  • ();
  • }
  • }
  • 相关文章