POI导出excel(Java完整版代码)

news/2025/2/22 17:43:07/

1.pom.xml引入依赖

   <dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.6</version></dependency>

2.页面发送请求

window.location.href = “/exportInfoList?”+param;

3.后台Controller接收请求

//导出列表@RequestMapping(value = "exportInfoList")public void exportInfoList(String param, HttpServletResponse response) {OutputStream outputStream=null;try {HSSFWorkbook workbook=  service.exportExcel(param);outputStream = response.getOutputStream();response.reset();response.setContentType("application/msexcel");//response.setCharacterEncoding("utf-8");//设置浏览器响应头对应的Content-disposition 解决中文名字乱码String filename = "XX管理";response.setHeader("Content-disposition", "attachment;filename="+new String(filename.getBytes("gbk"), "iso8859-1")+".xls");workbook.write(outputStream);} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();} finally {try {outputStream.flush();outputStream.close();} catch (IOException e) {e.printStackTrace();}}}

4.Service方法

  //XX管理导出excelpublic HSSFWorkbook exportExcel(String param){HSSFWorkbook workbook=new HSSFWorkbook();try {OutputStream outputStream = null;List<JpCustomerInfo> list = mapper.selectListByPage(param);HSSFSheet sheet = workbook.createSheet("XX管理");Row row = sheet.createRow(0);// excle标题String[] ar = new String[]{"姓名", "身份证号", "渠道来源", "手机号", "省份", "app是否登录",  "注册时间"};for (int i = 0; i < ar.length; i++) {Cell cell = row.createCell(i);cell.setCellValue(ar[i]);sheet.setColumnWidth(i,ar[i].getBytes().length*350);}//excel 内容for (int s = 0; s < list.size(); s++) {JpCustomerInfo info = list.get(s);row = sheet.createRow(s + 1);row.createCell(0).setCellValue(info.getName());row.createCell(1).setCellValue(info.getIdNumber());row.createCell(2).setCellValue(info.getChannelName());row.createCell(3).setCellValue(info.getPhone());row.createCell(4).setCellValue(info.getProvince()); //省份row.createCell(5).setCellValue(info.getAppIsLogin() == 0 ? "是" : "否"); //app是否登录Date date =info.getRegisterTime();if (date==null){row.createCell(6).setCellValue(""); //注册时间}else{String time=DateFormatUtils.format(date,"yyyy-MM-dd HH-mm-ss");row.createCell(6).setCellValue(time); //注册时间}}} catch (Exception e) {e.printStackTrace();}return workbook;}

http://www.ppmy.cn/news/249268.html

相关文章

java代码读取excel文件,同时兼容2003和2007

后台服务器需要一个读取excel文件的工具&#xff0c;查了些资料&#xff0c;很多不能同时兼容2003和2007&#xff0c;整理了一下&#xff0c;加了一个判断&#xff0c;现在能同时兼容2003和2007&#xff0c;并且可以选择从表格的第几行开始读取。主要用到了apache的jar包&#…

html5 excel网页版,一种网页版EXCEL的实现方法与流程

本发明涉及计算机网页开发前端技术领域,特别是一种网页版EXCEL的实现方法。 背景技术: 目前对表格的功能依赖除了微软的OFFICE软件和金山的WPS之外,其他的在线版都需要高额的收费,有些能下载的都是试用的版本,并不能开源;难以满足用户的需求。 技术实现要素: 本发明解决…

Spring Boot整合EasyExcel(完整版包含上传解析excel和下载模板)

Spring Boot整合EasyExcel&#xff08;完整版包含上传解析excel和下载模板&#xff09; 1. 加入依赖2. 对读取excel内容&#xff08;批量添加&#xff09;3. 模板下载&#xff1a; 1. 加入依赖 <dependency><groupId>com.alibaba</groupId><artifactId>…

Vue+SpringBoot实现Excel模版上传、下载、Excel数据导出

环境准备&#xff1a;导入Maven依赖 <!-- hutool --><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>${hutool.version}</version></dependency><!--******** 处理excel文件…

Excel中文转拼音(完整版)-转

Excel中文转拼音&#xff08;完整版&#xff09; 打开Excel->工具->宏->Viaual Basic编辑器 在弹出来的窗口中对着VBAproject点右键->插入->模块 下面会出现一个名为"模块1"&#xff0c;点击 在右边的空白栏中粘贴以下内容&#xff1a; Function pi…

python生成矩阵导出excel_Python xlwt导出excel完整版

有一组任务数据,要把它excel下载下来,如果仅仅导出,用csv导出就很方便。 但是要导出漂亮的样式,重复的地方要合并单元格,设置背影颜色,字体,边框等。而CSV是纯文本格式,不支持设置各种样式。 研究了一天,把代码写了出来。 完整代码如下: import xlwt book = xlwt.Wor…

Excel下载(easyExcel)

​ 目录 一 框架及版本二 数据准备三 基础下载​四 单元格样式设置五 单元格行合并1 合并统计2 合并策略类3 注册合并策略 六 行高设置1 非动态设置行高2 动态设置行高 一 框架及版本 springboot: 2.1.3.RELEASEhutool工具easyExcel <dependency><groupId>com.al…

Python + Excel 实现图片批量下载

本文由荒原之梦原创&#xff0c;原文链接&#xff1a;http://zhaokaifeng.com/?p1214 操作环境 操作系统&#xff1a;Windows 10 家庭版 64 位 Python 版本&#xff1a;Python 3.7.0 操作步骤 首先&#xff0c;我们有下面这样一个 Excel 表格&#xff0c;其中第三列是图片…