首页>代码>spring boot多文件上传、单文件上传、Excel解析>/springboot-upload/src/main/java/com/simon/springbootupload/excel/ExcelDemo.java
package com.simon.springbootupload.excel; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.util.CellRangeAddress; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; /** * @author Simon */ @Controller public class ExcelDemo { /** * 将excel写到本地 * * @param args * @throws IOException */ public static void main(String[] args) throws IOException { //1、创建hssfworkbook文档对象(excel文档对象) HSSFWorkbook wb = new HSSFWorkbook(); //2、创建新的sheet对象(excel表单) HSSFSheet sheet = wb.createSheet("学生信息表"); //还可以设置行高和列宽 sheet.setDefaultRowHeightInPoints(10); sheet.setDefaultColumnWidth(20); //3、在sheet中创建第一行,参数为行索引excel,可以是0~65535之间的任何一个 HSSFRow row1 = sheet.createRow(0); //4、创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个) HSSFCell cell = row1.createCell(0); //5、设置单元格内容 cell.setCellValue("学生信息一览表"); //6、合并单元格CellRangeAddress构造参数依次表示起始行,截止行,起始列,截止列 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3)); //第一行,四列的的区域 //7、在sheet里创建第二行 HSSFRow row2 = sheet.createRow(1); //创建单元格并设置内容 row2.createCell(0).setCellValue("姓名"); row2.createCell(1).setCellValue("身份证号"); row2.createCell(2).setCellValue("身高"); row2.createCell(3).setCellValue("年龄"); //8、在sheet里创建第三行 HSSFRow row3 = sheet.createRow(2); row3.createCell(0).setCellValue("小明"); row3.createCell(1).setCellValue("130629198803041823"); row3.createCell(2).setCellValue("160.8"); row3.createCell(3).setCellValue("19"); //9、在sheet里创建第四行 HSSFRow row4 = sheet.createRow(3); row4.createCell(0).setCellValue("小红"); row4.createCell(1).setCellValue("130982198807129876"); row4.createCell(2).setCellValue("170.3"); row4.createCell(3).setCellValue("20"); //10、省略部分代码 //11、输出Excel文件(到本地) FileOutputStream fos = null; try { fos = new FileOutputStream("D:\\UPLOAD\\student.xls"); wb.write(fos); fos.flush(); } catch (Exception e) { System.out.println("写出失败"); e.printStackTrace(); } finally { if (fos != null) { fos.close(); } } } @RequestMapping("/hello") @ResponseBody public String hello(String username) { return "hello :" + username; } /** * 将生成的excel写出到浏览器供客户下载 * * @param request * @param response * @throws IOException */ @RequestMapping("/excel") public void printExcelToBrowser(HttpServletRequest request, HttpServletResponse response) throws IOException { System.out.println("......................."); //1、创建hssfworkbook文档对象(excel文档对象) HSSFWorkbook wb = new HSSFWorkbook(); //2、创建新的sheet对象(excel表单) HSSFSheet sheet = wb.createSheet("学生信息表"); //3、在sheet中创建第一行,参数为行索引excel,可以是0~65535之间的任何一个 HSSFRow row1 = sheet.createRow(0); //4、创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个) HSSFCell cell = row1.createCell(0); //5、设置单元格内容 cell.setCellValue("学生信息一览表"); //6、合并单元格CellRangeAddress构造参数依次表示起始行,截止行,起始列,截止列 sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3)); //第一行,四列的的区域 //7、在sheet里创建第二行 HSSFRow row2 = sheet.createRow(1); //创建单元格并设置内容 row2.createCell(0).setCellValue("姓名"); row2.createCell(1).setCellValue("身份证号"); row2.createCell(2).setCellValue("身高"); row2.createCell(3).setCellValue("年龄"); //8、在sheet里创建第三行 HSSFRow row3 = sheet.createRow(2); row3.createCell(0).setCellValue("小明"); row3.createCell(1).setCellValue("130629198803041823"); row3.createCell(2).setCellValue("160.8"); row3.createCell(3).setCellValue("19"); //9、在sheet里创建第四行 HSSFRow row4 = sheet.createRow(3); row4.createCell(0).setCellValue("小红"); row4.createCell(1).setCellValue("130982198807129876"); row4.createCell(2).setCellValue("170.3"); row4.createCell(3).setCellValue("20"); //10、省略部分代码 //11、输出Excel文件(到浏览器) OutputStream outputStream = response.getOutputStream(); response.reset(); response.setHeader("Content-disposition", "attachement;filename=details.xls"); response.setContentType("application/msexcel"); wb.write(outputStream); // outputStream.flush(); outputStream.close(); } }

微信网友_7134912998903808 LV15
2024年9月13日
Seaskye LV14
2023年11月4日
lyh1989 LV34
2023年10月24日
镜影 LV3
2023年9月14日
做你的景天 LV7
2023年4月12日
lironggang LV38
2023年3月31日
jk-mack LV5
2023年2月19日
15908475644 LV4
2023年2月4日
快乐的程序员 LV25
2023年1月25日
最代码官方 LV168
2023年1月15日

rexefar
4月16日
暂无贡献等级
ma406805131 LV19
2024年12月18日
短巷拥猫 LV12
2024年12月4日
yimaoermao LV1
2024年11月28日
3993zby LV2
2024年11月11日
best2018 LV46
2024年9月13日
微信网友_7134912998903808 LV15
2024年8月29日
赵鑫cdsaljkdfsa LV11
2024年6月18日
TY0165 LV20
2024年6月17日
ShiBa Lover
2024年6月11日
暂无贡献等级