//返回浏览器下载

public int ExportWithResponse(String sheetName, String titleName,String fileName, int columnNumber, int[] columnWidth,String[] columnName, String[][] dataList,HttpServletResponse response) throws Exception {if (columnNumber == columnWidth.length&& columnWidth.length == columnName.length) {// 第一步,创建一个webbook,对应一个Excel文件HSSFWorkbook wb = new HSSFWorkbook();// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheetHSSFSheet sheet = wb.createSheet(sheetName);// sheet.setDefaultColumnWidth(15); //统一设置列宽for (int i = 0; i < columnNumber; i++) {for (int j = 0; j <= i; j++) {if (i == j) {sheet.setColumnWidth(i, columnWidth[j] * 256); // 单独设置每列的宽}}}// 创建第0行 也就是标题HSSFRow row1 = sheet.createRow((int) 0);row1.setHeightInPoints(50);// 设备标题的高度// 第三步创建标题的单元格样式style2以及字体样式headerFont1HSSFCellStyle style2 = wb.createCellStyle();style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//style2.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);背景色style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);HSSFFont headerFont1 = (HSSFFont) wb.createFont(); // 创建字体样式// headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗headerFont1.setFontName("黑体"); // 设置字体类型headerFont1.setFontHeightInPoints((short) 14); // 设置字体大小style2.setFont(headerFont1); // 为标题样式设置字体样式HSSFCell cell1 = row1.createCell(0);// 创建标题第一列sheet.addMergedRegion(new CellRangeAddress(0, 0, 0,columnNumber - 1)); // 合并列标题cell1.setCellValue(titleName); // 设置值标题cell1.setCellStyle(style2); // 设置标题样式// 创建第1行 也就是表头HSSFRow row = sheet.createRow((int) 1);row.setHeightInPoints(40);// 设置表头高度// 第四步,创建表头单元格样式 以及表头的字体样式HSSFCellStyle style = wb.createCellStyle();style.setWrapText(true);// 设置自动换行style.setAlignment(HSSFCellStyle.ALIGN_CENTER);style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式style.setBottomBorderColor(HSSFColor.BLACK.index);style.setBorderBottom(HSSFCellStyle.BORDER_THIN);style.setBorderLeft(HSSFCellStyle.BORDER_THIN);style.setBorderRight(HSSFCellStyle.BORDER_THIN);style.setBorderTop(HSSFCellStyle.BORDER_THIN);HSSFFont headerFont = (HSSFFont) wb.createFont(); // 创建字体样式headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗headerFont.setFontName("宋体"); // 设置字体类型headerFont.setFontHeightInPoints((short) 12); // 设置字体大小HSSFFont headerFont2 = (HSSFFont) wb.createFont(); // 创建字体样式headerFont2.setFontName("宋体"); // 设置字体类型headerFont2.setFontHeightInPoints((short) 12); // 设置字体大小style.setFont(headerFont); // 为标题样式设置字体样式// 第四.一步,创建表头的列for (int i = 0; i < columnNumber; i++) {HSSFCell cell = row.createCell(i);cell.setCellValue(columnName[i]);cell.setCellStyle(style);}// 第五步,创建单元格,并设置值for (int i = 0; i < dataList.length; i++) {row = sheet.createRow((int) i + 2);// 为数据内容设置特点新单元格样式1 自动换行 上下居中HSSFCellStyle zidonghuanhang = wb.createCellStyle();zidonghuanhang.setWrapText(true);// 设置自动换行zidonghuanhang.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式// 设置边框zidonghuanhang.setBottomBorderColor(HSSFColor.BLACK.index);zidonghuanhang.setBorderBottom(HSSFCellStyle.BORDER_THIN);zidonghuanhang.setBorderLeft(HSSFCellStyle.BORDER_THIN);zidonghuanhang.setBorderRight(HSSFCellStyle.BORDER_THIN);zidonghuanhang.setBorderTop(HSSFCellStyle.BORDER_THIN);// 为数据内容设置特点新单元格样式2 自动换行 上下居中左右也居中HSSFCellStyle zidonghuanhang2 = wb.createCellStyle();zidonghuanhang2.setFont(headerFont2);zidonghuanhang2.setWrapText(true);// 设置自动换行zidonghuanhang2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个上下居中格式zidonghuanhang2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中// 设置边框zidonghuanhang2.setBottomBorderColor(HSSFColor.BLACK.index);zidonghuanhang2.setBorderBottom(HSSFCellStyle.BORDER_THIN);zidonghuanhang2.setBorderLeft(HSSFCellStyle.BORDER_THIN);zidonghuanhang2.setBorderRight(HSSFCellStyle.BORDER_THIN);zidonghuanhang2.setBorderTop(HSSFCellStyle.BORDER_THIN);HSSFCell datacell = null;for (int j = 0; j < columnNumber; j++) {datacell = row.createCell(j);datacell.setCellValue(dataList[i][j]);datacell.setCellStyle(zidonghuanhang2);}}// 第六步,将文件存到浏览器设置的下载位置String filename = fileName + ".xls";response.setContentType("application/ms-excel;charset=UTF-8");response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(filename, "UTF-8"))));OutputStream out = response.getOutputStream();try {wb.write(out);// 将数据写出去//  String str = "导出" + fileName + "成功!";//   System.out.println(str);return 1;} catch (Exception e) {e.printStackTrace();//  String str1 = "导出" + fileName + "失败!";//  System.out.println(str1);return 2;} finally {out.close();}} else {//    System.out.println("列数目长度名称三个数组长度要一致");return 2;}}

//直接在本地路径下生成

public void ExportNoResponse(String sheetName, String titleName,String fileName, int columnNumber, int[] columnWidth,String[] columnName, String[][] dataList) throws Exception {if (columnNumber == columnWidth.length&& columnWidth.length == columnName.length) {// 第一步,创建一个webbook,对应一个Excel文件HSSFWorkbook wb = new HSSFWorkbook();// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheetHSSFSheet sheet = wb.createSheet(sheetName);// sheet.setDefaultColumnWidth(15); //统一设置列宽for (int i = 0; i < columnNumber; i++) {for (int j = 0; j <= i; j++) {if (i == j) {sheet.setColumnWidth(i, columnWidth[j] * 256); // 单独设置每列的宽}}}// 创建第0行 也就是标题HSSFRow row1 = sheet.createRow((int) 0);row1.setHeightInPoints(50);// 设备标题的高度// 第三步创建标题的单元格样式style2以及字体样式headerFont1HSSFCellStyle style2 = wb.createCellStyle();style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);style2.setFillForegroundColor(HSSFColor.LIGHT_TURQUOISE.index);style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);HSSFFont headerFont1 = (HSSFFont) wb.createFont(); // 创建字体样式headerFont1.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗headerFont1.setFontName("黑体"); // 设置字体类型headerFont1.setFontHeightInPoints((short) 15); // 设置字体大小style2.setFont(headerFont1); // 为标题样式设置字体样式HSSFCell cell1 = row1.createCell(0);// 创建标题第一列sheet.addMergedRegion(new CellRangeAddress(0, 0, 0,columnNumber - 1)); // 合并第0到第17列cell1.setCellValue(titleName); // 设置值标题cell1.setCellStyle(style2); // 设置标题样式// 创建第1行 也就是表头HSSFRow row = sheet.createRow((int) 1);row.setHeightInPoints(37);// 设置表头高度// 第四步,创建表头单元格样式 以及表头的字体样式HSSFCellStyle style = wb.createCellStyle();style.setWrapText(true);// 设置自动换行style.setAlignment(HSSFCellStyle.ALIGN_CENTER);style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式style.setBottomBorderColor(HSSFColor.BLACK.index);style.setBorderBottom(HSSFCellStyle.BORDER_THIN);style.setBorderLeft(HSSFCellStyle.BORDER_THIN);style.setBorderRight(HSSFCellStyle.BORDER_THIN);style.setBorderTop(HSSFCellStyle.BORDER_THIN);HSSFFont headerFont = (HSSFFont) wb.createFont(); // 创建字体样式headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 字体加粗headerFont.setFontName("黑体"); // 设置字体类型headerFont.setFontHeightInPoints((short) 10); // 设置字体大小style.setFont(headerFont); // 为标题样式设置字体样式// 第四.一步,创建表头的列for (int i = 0; i < columnNumber; i++) {HSSFCell cell = row.createCell(i);cell.setCellValue(columnName[i]);cell.setCellStyle(style);}// 第五步,创建单元格,并设置值for (int i = 0; i < dataList.length; i++){row = sheet.createRow((int) i + 2);// 为数据内容设置特点新单元格样式1 自动换行 上下居中HSSFCellStyle zidonghuanhang = wb.createCellStyle();zidonghuanhang.setWrapText(true);// 设置自动换行zidonghuanhang.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个居中格式// 设置边框zidonghuanhang.setBottomBorderColor(HSSFColor.BLACK.index);zidonghuanhang.setBorderBottom(HSSFCellStyle.BORDER_THIN);zidonghuanhang.setBorderLeft(HSSFCellStyle.BORDER_THIN);zidonghuanhang.setBorderRight(HSSFCellStyle.BORDER_THIN);zidonghuanhang.setBorderTop(HSSFCellStyle.BORDER_THIN);// 为数据内容设置特点新单元格样式2 自动换行 上下居中左右也居中HSSFCellStyle zidonghuanhang2 = wb.createCellStyle();zidonghuanhang2.setWrapText(true);// 设置自动换行zidonghuanhang2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 创建一个上下居中格式zidonghuanhang2.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中// 设置边框zidonghuanhang2.setBottomBorderColor(HSSFColor.BLACK.index);zidonghuanhang2.setBorderBottom(HSSFCellStyle.BORDER_THIN);zidonghuanhang2.setBorderLeft(HSSFCellStyle.BORDER_THIN);zidonghuanhang2.setBorderRight(HSSFCellStyle.BORDER_THIN);zidonghuanhang2.setBorderTop(HSSFCellStyle.BORDER_THIN);HSSFCell datacell = null;for (int j = 0; j < columnNumber; j++) {datacell = row.createCell(j);datacell.setCellValue(dataList[i][j]);datacell.setCellStyle(zidonghuanhang2);}}// 第六步,将文件存到指定位置try {FileOutputStream fout = new FileOutputStream("D:/666/students.xls");wb.write(fout);String str = "导出" + fileName + "成功!";System.out.println(str);fout.close();} catch (Exception e) {e.printStackTrace();String str1 = "导出" + fileName + "失败!";System.out.println(str1);}} else {System.out.println("列数目长度名称三个数组长度要一致");}}

//测试

public static void main(String[] args) throws Exception {String sheetName = "天津xxx公司政策兑现申请情况";String titleName = "天津xxx公司政策兑现申请情况";String fileName = "天津xxx公司政策兑现申请情况";int columnNumber = 8;//列数int[] columnWidth = { 10, 40, 100, 50, 50, 60, 60, 60  }; //列宽//需要插入Excel的数据String[][] dataList = {  { "1", "111", "建立博士后工作站和博士后创新实践基地资助", "初审通过", "2015-01-01", "5000", "5000", "5000" }};String[] columnName = { "序号", "申请编号", "申请事项", "办理进度", "办理日期", "申请金额(万元)", "财政核准金额(万元)", "拨付金额(万元)" };new EnpExcelTest().ExportNoResponse(sheetName, titleName, fileName,columnNumber, columnWidth, columnName, dataList);}

所需jar包下载地址:https://download.csdn.net/download/qq_41032995/10662034

poi导出Excel并在浏览器下载相关推荐

  1. poi导出Excel直接在浏览器下载

    需求:导出成Excel格式,要在浏览器看到类似于下载的效果. 导出的Excel和下载在同一个目录下. xxController.java // 导出 @RequestMapping(value = & ...

  2. 关于poi导出excel浏览器不下载的问题

    问题: 最近使用poi导出excel表格,但是发现请求成功,但是没有下载框,在浏览器中按F12查看,在调用的成功的response中查看到一堆乱码数据,考虑是不是返回的数据类型有问题,百度了一波,才发 ...

  3. Spring Boot poi 导出Excel表格、Txt到浏览器下载

    Spring Boot & poi 导出Excel表格.Txt到浏览器下载 原文链接:小回博客 文章目录 Spring Boot & poi 导出Excel表格.Txt到浏览器下载 一 ...

  4. POI导出Excel,浏览器不下载的问题解决

    在做POI导出Excel的时候,遇到了浏览器不弹出下载框的问题 问题发现 debug跟进发现输出流已经成功写入,而且程序没有报错,可就是没有下载提示 在前台控制器的XHR-response中查看返回数 ...

  5. Java POI 导出EXCEL经典实现 Java导出Excel弹出下载框

    原文转载:http://blog.csdn.net/evangel_z/article/details/7332535 目录(?)[+] 在web开发中,有一个经典的功能,就是数据的导入导出.特别是数 ...

  6. 项目总结23:POI生成Excel文件并浏览器导出

    项目总结23:POI生成Excel文件并浏览器导出 具体的逻辑可以参考Controller层的注释 代码1-前端html <button onclick="downLoad()&quo ...

  7. Springboot利用poi导出excel下载

    Springboot利用poi导出excel下载 因为项目中之前的做法是用反射获取属性,所以demo中也是用的反射,我看网上很多文章都是存入一个List中,不知道这两种哪种更何合适一点,或者有什么更好 ...

  8. java导出excel 客户端_Java poi导出Excel下载到客户端

    Java poi 导出Excel并下载到客户端,具体内容如下 Maven配置,包含了其他文件格式的依赖,就全贴出来了 org.apache.poi poi-excelant 3.12 org.apac ...

  9. struts2 poi导出excel实例代码下载

    原文:struts2 poi导出excel实例代码下载 代码下载地址:http://www.zuidaima.com/share/1550463233526784.htm 页面展现成表格形式,添加ex ...

最新文章

  1. 结对编程--基于android平台的黄金点游戏
  2. Kerberos验证过程
  3. 谷歌大脑Wasserstein自编码器:新一代生成模型算法
  4. 图像目标分割_5 DeepLab V2 V3 V3+
  5. LeetCode MySQL 570. 至少有5名直接下属的经理
  6. 用C#实现对Oracle 存储过程/函数/包的调试(附源代码)
  7. 几个病毒代码(c++)
  8. java 数独 gui,GitHub - fagen/sudoku: 数独终局生成器和GUI
  9. canvas动画之三 -- 黑客帝国文字掉落效果
  10. java上传图片压缩大小
  11. 图片怎么识别文字?这几个方法很实用
  12. 请帮忙扩写韩翃“寒食”这首诗
  13. 语音数据集 | Speech datasets
  14. 回顾丨2022隐私计算融合区块链技术论坛(附视频+演讲PPT)
  15. 基于HBuilder 开发 项目之微信支付
  16. 示波器探头校准-补偿电容
  17. vue中根据窗口的大小调整echarts图表的尺寸
  18. 2019年第一场雪中所思
  19. 山武阀门配件AVP300RSD3A
  20. 网络编程(学习整理)---3--(Udp)FeiQ实现广播消息群发

热门文章

  1. 【蓝桥杯省赛真题02】python正方形内切圆 青少年组蓝桥杯python编程省赛真题解析
  2. python中的键可以是列表吗_Python 字典中的“键”可以是列表。
  3. 2021年4月11日度小满笔试
  4. 优化切尔诺贝利灾难模型——附matlab代码
  5. python数据类型定义为_一Python 数据类型
  6. 欢乐地球NFT游戏火爆开启,平台积分OFE暴涨100倍
  7. [电脑故障]ntoskrnl.exe导致DRIVER_POWER_STATE_FAILURE
  8. Python 画图程序
  9. 双城记:京城走进海关、电力讲解决方案,泉城展开“X+Y”渠道招募
  10. 高考考了657分想重新复读一年,又害怕白费一年