1.导出无表头excel 文件单个工作表(sheet),【fileName是标题名,titleList是列名,list就是列的内容了】

[java] view plaincopy
  1. /**
  2. * 导出无表头excel 文件单个工作表(sheet)
  3. *
  4. * @param fileName
  5. * @param titleList
  6. * @param list
  7. * @param response
  8. */
  9. public static void exportNoHeadExcel(String fileName, String[] titleList,
  10. List list, HttpServletResponse response) {
  11. SimpleDateFormat df = new java.text.SimpleDateFormat("yyyyMMdd");
  12. String todayStr = df.format(new Date());
  13. OutputStream os = null;
  14. try {
  15. os = response.getOutputStream();
  16. String localFileName = fileName;
  17. fileName = java.net.URLEncoder.encode(fileName, "UTF-8");// 处理中文文件名的问题
  18. fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 处理中文文件名的问题
  19. response.setContentType("application/vnd.ms-excel;");
  20. response.setHeader("Content-disposition", "attachment; filename=\""
  21. + fileName + "_" + todayStr + ".xls\"");
  22. // 开始写入excel
  23. // 字段字体
  24. jxl.write.WritableFont wfc1 = new jxl.write.WritableFont(
  25. WritableFont.COURIER, 10, WritableFont.NO_BOLD, true);
  26. jxl.write.WritableCellFormat wcfFC1 = new jxl.write.WritableCellFormat(
  27. wfc1);
  28. wcfFC1.setAlignment(jxl.format.Alignment.CENTRE);
  29. wcfFC1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
  30. // 结果字体
  31. jxl.write.WritableCellFormat wcfFC2 = new jxl.write.WritableCellFormat();
  32. wcfFC2.setAlignment(jxl.format.Alignment.CENTRE);
  33. wcfFC2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
  34. WritableWorkbook wbook = Workbook.createWorkbook(os);
  35. // 写sheet名称
  36. WritableSheet wsheet = wbook.createSheet(localFileName, 0);
  37. for (int m = 0; m < titleList.length; m++) {
  38. wsheet.setColumnView(m, 30);
  39. }
  40. // 加入字段名
  41. for (int n = 0; n < titleList.length; n++) {
  42. wsheet.addCell(new jxl.write.Label(n, 0, titleList[n], wcfFC1));
  43. }
  44. // 写入流中
  45. int row = 0;
  46. for (int r = 0; r < list.size(); r++) {
  47. Object[] obj = (Object[]) list.get(r);
  48. for (int x = 0; x < titleList.length; x++) {
  49. wsheet.addCell(new jxl.write.Label(x, row + 1,
  50. obj[x] == null ? " " : obj[x].toString(), wcfFC1));
  51. }
  52. row++;
  53. if (row % 60000 == 0) {
  54. row = 0;
  55. // 写sheet名称
  56. wsheet = wbook.createSheet(localFileName, 0);
  57. for (int m = 0; m < titleList.length; m++) {
  58. wsheet.setColumnView(m, 30);
  59. }
  60. // 加入字段名
  61. for (int n = 0; n < titleList.length; n++) {
  62. wsheet.addCell(new jxl.write.Label(n, 0, titleList[n],
  63. wcfFC1));
  64. }
  65. }
  66. }
  67. wbook.write();
  68. wbook.close();
  69. os.flush();
  70. } catch (Exception e) {
  71. e.printStackTrace();
  72. } finally {
  73. if (os == null) {
  74. Log.info("os is null");
  75. } else {
  76. try {
  77. os.close();
  78. os = null;
  79. } catch (IOException e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. }
  84. }

2.首先导出无表头excel 文件带多个工作表(sheet),【sheetList存放的就是Map包含fileName是标题名,titleList是列名,list就是列的内容】

[java] view plaincopy
  1. /**
  2. * 导出无表头excel文件
  3. *
  4. * @param fileName
  5. * @param titleList
  6. * @param list
  7. * @param response
  8. */
  9. public static void exportNoHeadExcel(List<Map<String,Object>> sheetList, HttpServletResponse response) {
  10. SimpleDateFormat df = new java.text.SimpleDateFormat("yyyyMMdd");
  11. String todayStr = df.format(new Date());
  12. OutputStream os = null;
  13. try {
  14. os = response.getOutputStream();
  15. WritableWorkbook wbook = Workbook.createWorkbook(os);
  16. for(int i=0;i<sheetList.size();i++){
  17. Map<String,Object> map=sheetList.get(i);
  18. String fileName=(String) map.get("fileName");
  19. String[] titleList=(String[]) map.get("titleList");
  20. List list=(List) map.get("list");
  21. String localFileName = fileName;
  22. fileName = java.net.URLEncoder.encode(fileName, "UTF-8");// 处理中文文件名的问题
  23. fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 处理中文文件名的问题
  24. response.setContentType("application/vnd.ms-excel;");
  25. response.setHeader("Content-disposition", "attachment; filename=\""
  26. + fileName + "_" + todayStr + ".xls\"");
  27. // 开始写入excel
  28. // 字段字体
  29. jxl.write.WritableFont wfc1 = new jxl.write.WritableFont(
  30. WritableFont.COURIER, 10, WritableFont.NO_BOLD, true);
  31. jxl.write.WritableCellFormat wcfFC1 = new jxl.write.WritableCellFormat(
  32. wfc1);
  33. wcfFC1.setAlignment(jxl.format.Alignment.CENTRE);
  34. wcfFC1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
  35. // 结果字体
  36. jxl.write.WritableCellFormat wcfFC2 = new jxl.write.WritableCellFormat();
  37. wcfFC2.setAlignment(jxl.format.Alignment.CENTRE);
  38. wcfFC2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
  39. // 写sheet名称
  40. WritableSheet wsheet = wbook.createSheet(localFileName,i);
  41. for (int m = 0; m < titleList.length; m++) {
  42. wsheet.setColumnView(m, 30);
  43. }
  44. // 加入字段名
  45. for (int n = 0; n < titleList.length; n++) {
  46. wsheet.addCell(new jxl.write.Label(n, 0, titleList[n], wcfFC1));
  47. }
  48. // 写入流中
  49. int row = 0;
  50. for (int r = 0; r < list.size(); r++) {
  51. Object[] obj = (Object[]) list.get(r);
  52. for (int x = 0; x < titleList.length; x++) {
  53. wsheet.addCell(new jxl.write.Label(x, row + 1,
  54. obj[x] == null ? " " : obj[x].toString(), wcfFC1));
  55. }
  56. row++;
  57. if (row % 60000 == 0) {
  58. row = 0;
  59. // 写sheet名称
  60. wsheet = wbook.createSheet(localFileName, 0);
  61. for (int m = 0; m < titleList.length; m++) {
  62. wsheet.setColumnView(m, 30);
  63. }
  64. // 加入字段名
  65. for (int n = 0; n < titleList.length; n++) {
  66. wsheet.addCell(new jxl.write.Label(n, 0, titleList[n],
  67. wcfFC1));
  68. }
  69. }
  70. }
  71. }
  72. wbook.write();
  73. wbook.close();
  74. os.flush();
  75. } catch (Exception e) {
  76. e.printStackTrace();
  77. } finally {
  78. if (os == null) {
  79. Log.info("os is null");
  80. } else {
  81. try {
  82. os.close();
  83. os = null;
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. }
  89. }

3.导出excel文件带标题【fileName是标题名,titleList是列名,list就是列的内容了】

[java] view plaincopy
  1. /**
  2. * 导出excel 文件  带标题
  3. *
  4. * @param fileName
  5. * @param titleList
  6. * @param list
  7. * @param response
  8. */
  9. public static void exportWithHeadExcel(String fileName, String[] titleList,
  10. List list, HttpServletResponse response) {
  11. Date now = new Date();
  12. SimpleDateFormat dateformat = new java.text.SimpleDateFormat(
  13. "yyyy年MM月dd日HH时mm分");
  14. SimpleDateFormat df = new java.text.SimpleDateFormat("yyyyMMdd");
  15. String todayStr = df.format(new Date());
  16. String today = dateformat.format(now);
  17. OutputStream os = null;
  18. try {
  19. os = response.getOutputStream();
  20. String localFileName = fileName;
  21. fileName = java.net.URLEncoder.encode(fileName, "UTF-8");// 处理中文文件名的问题
  22. fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 处理中文文件名的问题
  23. response.setContentType("application/vnd.ms-excel;");
  24. response.setHeader("Content-disposition", "attachment; filename=\""
  25. + fileName + "_" + todayStr + ".xls\"");
  26. // 开始写入excel
  27. // 加标题
  28. // 标题字体
  29. jxl.write.WritableFont wfc = new jxl.write.WritableFont(
  30. WritableFont.COURIER, 18, WritableFont.NO_BOLD, false);
  31. jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(
  32. wfc);
  33. wcfFC.setAlignment(jxl.format.Alignment.CENTRE);
  34. wcfFC.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
  35. // 字段字体
  36. jxl.write.WritableFont wfc1 = new jxl.write.WritableFont(
  37. WritableFont.COURIER, 10, WritableFont.NO_BOLD, false);
  38. jxl.write.WritableCellFormat wcfFC1 = new jxl.write.WritableCellFormat(
  39. wfc1);
  40. wcfFC1.setAlignment(jxl.format.Alignment.CENTRE);
  41. wcfFC1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
  42. // 结果字体
  43. jxl.write.WritableCellFormat wcfFC2 = new jxl.write.WritableCellFormat();
  44. wcfFC2.setAlignment(jxl.format.Alignment.CENTRE);
  45. wcfFC2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
  46. WritableWorkbook wbook = Workbook.createWorkbook(os);
  47. // 写sheet名称
  48. WritableSheet wsheet = wbook.createSheet(localFileName, 0);
  49. int i = 3;
  50. for (int m = 0; m < titleList.length; m++) {
  51. wsheet.setColumnView(m, 30);
  52. }
  53. // 加入字段名
  54. for (int n = 0; n < titleList.length; n++) {
  55. wsheet.addCell(new jxl.write.Label(n, 3, titleList[n], wcfFC1));
  56. }
  57. // 加入标题
  58. wsheet.mergeCells(0, 0, i - 1, 0);
  59. wsheet.addCell(new Label(0, 0, localFileName, wcfFC));
  60. // 加入打印时间
  61. wsheet.addCell(new Label(i - 2, 1, "打印日期:"));
  62. wsheet.addCell(new Label(i - 1, 1, today));
  63. // 写入流中
  64. int row = 0;
  65. for (int r = 0; r < list.size(); r++) {
  66. Object[] obj = (Object[]) list.get(r);
  67. for (int x = 0; x < titleList.length; x++) {
  68. wsheet.addCell(new jxl.write.Label(x, row + 4,
  69. obj[x] == null ? " " : obj[x].toString(), wcfFC1));
  70. }
  71. row++;
  72. if (row % 60000 == 0) {
  73. row = 0;
  74. // 写sheet名称
  75. wsheet = wbook.createSheet(localFileName, 0);
  76. i = 3;
  77. for (int m = 0; m < titleList.length; m++) {
  78. wsheet.setColumnView(m, 30);
  79. }
  80. // 加入字段名
  81. for (int n = 0; n < titleList.length; n++) {
  82. wsheet.addCell(new jxl.write.Label(n, 3, titleList[n],
  83. wcfFC1));
  84. }
  85. // 加入标题
  86. wsheet.mergeCells(0, 0, i - 1, 0);
  87. wsheet.addCell(new Label(0, 0, localFileName, wcfFC));
  88. // 加入打印时间
  89. wsheet.addCell(new Label(i - 2, 1, "打印日期:"));
  90. wsheet.addCell(new Label(i - 1, 1, today));
  91. }
  92. }
  93. wbook.write();
  94. wbook.close();
  95. os.flush();
  96. } catch (Exception e) {
  97. e.printStackTrace();
  98. } finally {
  99. if (os == null) {
  100. Log.info("os is null");
  101. } else {
  102. try {
  103. os.close();
  104. os = null;
  105. } catch (IOException e) {
  106. e.printStackTrace();
  107. }
  108. }
  109. }
  110. }

转载于:https://www.cnblogs.com/Y-S-X/p/7995741.html

导出excel 文件相关推荐

  1. vue2.0通过Axios导出excel文件(解决乱码问题)

    vue2.0通过Axios导出excel文件(解决乱码问题) 参考文章: (1)vue2.0通过Axios导出excel文件(解决乱码问题) (2)https://www.cnblogs.com/ad ...

  2. php 将数组导出excel,#php 怎样将 数组导出excel文件#前端导出excel表格

    php 怎样将 数组导出excel文件 public function excel() { //在这里你要导出的数据 $data = M('pmproject',"pm_",MYS ...

  3. 【转】 (C#)利用Aspose.Cells组件导入导出excel文件

    Aspose.Cells组件可以不依赖excel来导入导出excel文件: 导入: public static System.Data.DataTable ReadExcel(String strFi ...

  4. Java poi插件导出Excel文件合并多sheet页

    文章目录 一.java导出excel格式文件 二.excel文件多sheet页合并 前言:2020年第一篇文章,就写这两天工作中遇到的这个小需求吧,导出多excel,每个excel有多个sheet页, ...

  5. C#导出Excel文件,过长数值显示为科学计数法解决方法 C#

    C#导出EXCEL文件,身份证号码或某些ID内容长度超过15个数字,这样导出的Excel文件中默认情况下将这个值以科学计数方式显示,下面提供两种解决方式: 1.在转出的内容前,加上一个TAB符号,C# ...

  6. springboot中使用poi导出excel文件(亲测实现了第一个功能)

    1.POI简介 Jakarta POI 是一套用于访问微软格式文档的Java API. 组件HWPF用于操作Word的; 组件HSSF用于操作Excel格式文件. 2.常用组件 HSSFWorkboo ...

  7. java导入、导出Excel文件

    一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...

  8. asp.net中通过html格式导出excel文件

    //通过html格式生成导出excel文件,下载保存.   StreamExport(wgMdcStaff5, fileName, ToDataTable<MdcDrugProcureStock ...

  9. 如何使用JavaScript实现纯前端读取和导出excel文件(转)

    转自小茗同学博客:https://www.cnblogs.com/liuxianan/p/js-excel.html js-xlsx 介绍 由SheetJS出品的js-xlsx是一款非常方便的只需要纯 ...

  10. Vue+iview实现自定义格式导出Excel文件

    背景:项目中要实现一个导出Excel文件模板的功能,原来实现是通过后台生成然后前端请求下载,这样要消耗IO资源,然后看了一下之前项目的导出功能,发现原来Vue+iview可以实现本地数据导出,不过iv ...

最新文章

  1. [转]MFC下关于“建立空文档失败”问题的分析
  2. 开发日记-20190428
  3. 《神秘的程序员们》漫画47:这些年你读过的书
  4. ListView控件的基本使用(方式一:使用ArrayAdapter适配器实现)
  5. 部署Django到云服务器(centos+nginx+mysql+uwsgi+python3)【操作篇(1)】
  6. 太原理工大学c语言课程设计报告,[太原理工大学C语言实验报告.doc
  7. java static 变量,和方法从属于类
  8. 滴滴业务中台构建实践,首次曝光
  9. 基于Vue、vue-i18n实现国际化(多语言)
  10. 定义一个工资变量c语言,《工资管理》c语言程序设计.doc
  11. swfobject参数详解
  12. 计算机培训作息时间安排,985学霸作息时间表“走红”,网友:越努力,越幸运...
  13. ssm酒店预订系统(ssm酒店管理系统民宿预订)ssm酒店客房预订系统宾馆JSP客房预订系统
  14. Y2K Bug and Bill Door
  15. 寄给你全宇宙的爱和自太古至永劫的思念
  16. 三菱FX5U系列PLC控制10轴设备成套资料打包三菱FX5U控制10轴伺服的设备成套电气图纸
  17. 86u 网页服务器,路由器怎么设置DMZ_华硕RT-AC86U路由器开启DMZ方法
  18. 漂亮妹妹~~~~~~`
  19. 文件服务器——NFS
  20. 网校系统是怎样搭建的?

热门文章

  1. C#如何让Listbox支持多选
  2. MaxScale初探
  3. [转]模拟芯片设计的四重境界
  4. 开源组件 Ehcache中被曝严重漏洞,影响多款Jira产品
  5. Apache Struts 修复 OGNL 技术中可能存在的 RCE 缺陷
  6. XP远程桌面连接强制登录
  7. Java日志组件间关系
  8. 知物由学 |“网状世界”下,无处可逃的信息安全
  9. 11月3日云栖精选夜读:《maven实战》读书笔记2——maven安装(windows和eclipse插件)...
  10. 前端开发学习之——dom ready和window onload的区别