web123456

Java creates Excel file templates and implements browser downloads

  • public void exportExcel(HttpServletResponse response) {
  • String fileName = "Test template_"+() + ".xlsx";
  • List<String> awardPrinciple = ("Title 1", "Title 2", "Title 3");
  • Create a workbook
  • XSSFWorkbook work = new XSSFWorkbook();
  • XSSFSheet sheet1 = ("Test template");
  • //Set the column width
  • (0, 25 * 256);
  • (1, 25 * 256);
  • (2, 25 * 256);
  • //Create cell style
  • CellStyle cellStyle = ();
  • //Set the background color to gray
  • (IndexedColors.GREY_25_PERCENT.getIndex());
  • (FillPatternType.SOLID_FOREGROUND);
  • //Set font style
  • Font font = ();
  • (true);
  • (());
  • (font);
  • //Set the table center
  • ();
  • ();
  • //Set border
  • ();
  • ();
  • ();
  • ();
  • //Create a row
  • XSSFRow row1 = (0);
  • ((short) 500);
  • for (int i = 0; i < (); i++) {
  • XSSFCell cell1 = (i);
  • ((i));
  • (cellStyle);
  • }
  • //Export using 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) {
  • ();
  • }
  • }