导出excel 文件
1.导出无表头excel 文件单个工作表(sheet),【fileName是标题名,titleList是列名,list就是列的内容了】
- /**
- * 导出无表头excel 文件单个工作表(sheet)
- *
- * @param fileName
- * @param titleList
- * @param list
- * @param response
- */
- public static void exportNoHeadExcel(String fileName, String[] titleList,
- List list, HttpServletResponse response) {
- SimpleDateFormat df = new java.text.SimpleDateFormat("yyyyMMdd");
- String todayStr = df.format(new Date());
- OutputStream os = null;
- try {
- os = response.getOutputStream();
- String localFileName = fileName;
- fileName = java.net.URLEncoder.encode(fileName, "UTF-8");// 处理中文文件名的问题
- fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 处理中文文件名的问题
- response.setContentType("application/vnd.ms-excel;");
- response.setHeader("Content-disposition", "attachment; filename=\""
- + fileName + "_" + todayStr + ".xls\"");
- // 开始写入excel
- // 字段字体
- jxl.write.WritableFont wfc1 = new jxl.write.WritableFont(
- WritableFont.COURIER, 10, WritableFont.NO_BOLD, true);
- jxl.write.WritableCellFormat wcfFC1 = new jxl.write.WritableCellFormat(
- wfc1);
- wcfFC1.setAlignment(jxl.format.Alignment.CENTRE);
- wcfFC1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
- // 结果字体
- jxl.write.WritableCellFormat wcfFC2 = new jxl.write.WritableCellFormat();
- wcfFC2.setAlignment(jxl.format.Alignment.CENTRE);
- wcfFC2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
- WritableWorkbook wbook = Workbook.createWorkbook(os);
- // 写sheet名称
- WritableSheet wsheet = wbook.createSheet(localFileName, 0);
- for (int m = 0; m < titleList.length; m++) {
- wsheet.setColumnView(m, 30);
- }
- // 加入字段名
- for (int n = 0; n < titleList.length; n++) {
- wsheet.addCell(new jxl.write.Label(n, 0, titleList[n], wcfFC1));
- }
- // 写入流中
- int row = 0;
- for (int r = 0; r < list.size(); r++) {
- Object[] obj = (Object[]) list.get(r);
- for (int x = 0; x < titleList.length; x++) {
- wsheet.addCell(new jxl.write.Label(x, row + 1,
- obj[x] == null ? " " : obj[x].toString(), wcfFC1));
- }
- row++;
- if (row % 60000 == 0) {
- row = 0;
- // 写sheet名称
- wsheet = wbook.createSheet(localFileName, 0);
- for (int m = 0; m < titleList.length; m++) {
- wsheet.setColumnView(m, 30);
- }
- // 加入字段名
- for (int n = 0; n < titleList.length; n++) {
- wsheet.addCell(new jxl.write.Label(n, 0, titleList[n],
- wcfFC1));
- }
- }
- }
- wbook.write();
- wbook.close();
- os.flush();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (os == null) {
- Log.info("os is null");
- } else {
- try {
- os.close();
- os = null;
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
2.首先导出无表头excel 文件带多个工作表(sheet),【sheetList存放的就是Map包含fileName是标题名,titleList是列名,list就是列的内容】
- /**
- * 导出无表头excel文件
- *
- * @param fileName
- * @param titleList
- * @param list
- * @param response
- */
- public static void exportNoHeadExcel(List<Map<String,Object>> sheetList, HttpServletResponse response) {
- SimpleDateFormat df = new java.text.SimpleDateFormat("yyyyMMdd");
- String todayStr = df.format(new Date());
- OutputStream os = null;
- try {
- os = response.getOutputStream();
- WritableWorkbook wbook = Workbook.createWorkbook(os);
- for(int i=0;i<sheetList.size();i++){
- Map<String,Object> map=sheetList.get(i);
- String fileName=(String) map.get("fileName");
- String[] titleList=(String[]) map.get("titleList");
- List list=(List) map.get("list");
- String localFileName = fileName;
- fileName = java.net.URLEncoder.encode(fileName, "UTF-8");// 处理中文文件名的问题
- fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 处理中文文件名的问题
- response.setContentType("application/vnd.ms-excel;");
- response.setHeader("Content-disposition", "attachment; filename=\""
- + fileName + "_" + todayStr + ".xls\"");
- // 开始写入excel
- // 字段字体
- jxl.write.WritableFont wfc1 = new jxl.write.WritableFont(
- WritableFont.COURIER, 10, WritableFont.NO_BOLD, true);
- jxl.write.WritableCellFormat wcfFC1 = new jxl.write.WritableCellFormat(
- wfc1);
- wcfFC1.setAlignment(jxl.format.Alignment.CENTRE);
- wcfFC1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
- // 结果字体
- jxl.write.WritableCellFormat wcfFC2 = new jxl.write.WritableCellFormat();
- wcfFC2.setAlignment(jxl.format.Alignment.CENTRE);
- wcfFC2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
- // 写sheet名称
- WritableSheet wsheet = wbook.createSheet(localFileName,i);
- for (int m = 0; m < titleList.length; m++) {
- wsheet.setColumnView(m, 30);
- }
- // 加入字段名
- for (int n = 0; n < titleList.length; n++) {
- wsheet.addCell(new jxl.write.Label(n, 0, titleList[n], wcfFC1));
- }
- // 写入流中
- int row = 0;
- for (int r = 0; r < list.size(); r++) {
- Object[] obj = (Object[]) list.get(r);
- for (int x = 0; x < titleList.length; x++) {
- wsheet.addCell(new jxl.write.Label(x, row + 1,
- obj[x] == null ? " " : obj[x].toString(), wcfFC1));
- }
- row++;
- if (row % 60000 == 0) {
- row = 0;
- // 写sheet名称
- wsheet = wbook.createSheet(localFileName, 0);
- for (int m = 0; m < titleList.length; m++) {
- wsheet.setColumnView(m, 30);
- }
- // 加入字段名
- for (int n = 0; n < titleList.length; n++) {
- wsheet.addCell(new jxl.write.Label(n, 0, titleList[n],
- wcfFC1));
- }
- }
- }
- }
- wbook.write();
- wbook.close();
- os.flush();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (os == null) {
- Log.info("os is null");
- } else {
- try {
- os.close();
- os = null;
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
3.导出excel文件带标题【fileName是标题名,titleList是列名,list就是列的内容了】
- /**
- * 导出excel 文件 带标题
- *
- * @param fileName
- * @param titleList
- * @param list
- * @param response
- */
- public static void exportWithHeadExcel(String fileName, String[] titleList,
- List list, HttpServletResponse response) {
- Date now = new Date();
- SimpleDateFormat dateformat = new java.text.SimpleDateFormat(
- "yyyy年MM月dd日HH时mm分");
- SimpleDateFormat df = new java.text.SimpleDateFormat("yyyyMMdd");
- String todayStr = df.format(new Date());
- String today = dateformat.format(now);
- OutputStream os = null;
- try {
- os = response.getOutputStream();
- String localFileName = fileName;
- fileName = java.net.URLEncoder.encode(fileName, "UTF-8");// 处理中文文件名的问题
- fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 处理中文文件名的问题
- response.setContentType("application/vnd.ms-excel;");
- response.setHeader("Content-disposition", "attachment; filename=\""
- + fileName + "_" + todayStr + ".xls\"");
- // 开始写入excel
- // 加标题
- // 标题字体
- jxl.write.WritableFont wfc = new jxl.write.WritableFont(
- WritableFont.COURIER, 18, WritableFont.NO_BOLD, false);
- jxl.write.WritableCellFormat wcfFC = new jxl.write.WritableCellFormat(
- wfc);
- wcfFC.setAlignment(jxl.format.Alignment.CENTRE);
- wcfFC.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
- // 字段字体
- jxl.write.WritableFont wfc1 = new jxl.write.WritableFont(
- WritableFont.COURIER, 10, WritableFont.NO_BOLD, false);
- jxl.write.WritableCellFormat wcfFC1 = new jxl.write.WritableCellFormat(
- wfc1);
- wcfFC1.setAlignment(jxl.format.Alignment.CENTRE);
- wcfFC1.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
- // 结果字体
- jxl.write.WritableCellFormat wcfFC2 = new jxl.write.WritableCellFormat();
- wcfFC2.setAlignment(jxl.format.Alignment.CENTRE);
- wcfFC2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
- WritableWorkbook wbook = Workbook.createWorkbook(os);
- // 写sheet名称
- WritableSheet wsheet = wbook.createSheet(localFileName, 0);
- int i = 3;
- for (int m = 0; m < titleList.length; m++) {
- wsheet.setColumnView(m, 30);
- }
- // 加入字段名
- for (int n = 0; n < titleList.length; n++) {
- wsheet.addCell(new jxl.write.Label(n, 3, titleList[n], wcfFC1));
- }
- // 加入标题
- wsheet.mergeCells(0, 0, i - 1, 0);
- wsheet.addCell(new Label(0, 0, localFileName, wcfFC));
- // 加入打印时间
- wsheet.addCell(new Label(i - 2, 1, "打印日期:"));
- wsheet.addCell(new Label(i - 1, 1, today));
- // 写入流中
- int row = 0;
- for (int r = 0; r < list.size(); r++) {
- Object[] obj = (Object[]) list.get(r);
- for (int x = 0; x < titleList.length; x++) {
- wsheet.addCell(new jxl.write.Label(x, row + 4,
- obj[x] == null ? " " : obj[x].toString(), wcfFC1));
- }
- row++;
- if (row % 60000 == 0) {
- row = 0;
- // 写sheet名称
- wsheet = wbook.createSheet(localFileName, 0);
- i = 3;
- for (int m = 0; m < titleList.length; m++) {
- wsheet.setColumnView(m, 30);
- }
- // 加入字段名
- for (int n = 0; n < titleList.length; n++) {
- wsheet.addCell(new jxl.write.Label(n, 3, titleList[n],
- wcfFC1));
- }
- // 加入标题
- wsheet.mergeCells(0, 0, i - 1, 0);
- wsheet.addCell(new Label(0, 0, localFileName, wcfFC));
- // 加入打印时间
- wsheet.addCell(new Label(i - 2, 1, "打印日期:"));
- wsheet.addCell(new Label(i - 1, 1, today));
- }
- }
- wbook.write();
- wbook.close();
- os.flush();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (os == null) {
- Log.info("os is null");
- } else {
- try {
- os.close();
- os = null;
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
转载于:https://www.cnblogs.com/Y-S-X/p/7995741.html
导出excel 文件相关推荐
- vue2.0通过Axios导出excel文件(解决乱码问题)
vue2.0通过Axios导出excel文件(解决乱码问题) 参考文章: (1)vue2.0通过Axios导出excel文件(解决乱码问题) (2)https://www.cnblogs.com/ad ...
- php 将数组导出excel,#php 怎样将 数组导出excel文件#前端导出excel表格
php 怎样将 数组导出excel文件 public function excel() { //在这里你要导出的数据 $data = M('pmproject',"pm_",MYS ...
- 【转】 (C#)利用Aspose.Cells组件导入导出excel文件
Aspose.Cells组件可以不依赖excel来导入导出excel文件: 导入: public static System.Data.DataTable ReadExcel(String strFi ...
- Java poi插件导出Excel文件合并多sheet页
文章目录 一.java导出excel格式文件 二.excel文件多sheet页合并 前言:2020年第一篇文章,就写这两天工作中遇到的这个小需求吧,导出多excel,每个excel有多个sheet页, ...
- C#导出Excel文件,过长数值显示为科学计数法解决方法 C#
C#导出EXCEL文件,身份证号码或某些ID内容长度超过15个数字,这样导出的Excel文件中默认情况下将这个值以科学计数方式显示,下面提供两种解决方式: 1.在转出的内容前,加上一个TAB符号,C# ...
- springboot中使用poi导出excel文件(亲测实现了第一个功能)
1.POI简介 Jakarta POI 是一套用于访问微软格式文档的Java API. 组件HWPF用于操作Word的; 组件HSSF用于操作Excel格式文件. 2.常用组件 HSSFWorkboo ...
- java导入、导出Excel文件
一.介绍 当前B/S模式已成为应用开发的主流,而在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统.银行系统).或者是:我们已经习惯用Excel打印.这样在我们实际 ...
- asp.net中通过html格式导出excel文件
//通过html格式生成导出excel文件,下载保存. StreamExport(wgMdcStaff5, fileName, ToDataTable<MdcDrugProcureStock ...
- 如何使用JavaScript实现纯前端读取和导出excel文件(转)
转自小茗同学博客:https://www.cnblogs.com/liuxianan/p/js-excel.html js-xlsx 介绍 由SheetJS出品的js-xlsx是一款非常方便的只需要纯 ...
- Vue+iview实现自定义格式导出Excel文件
背景:项目中要实现一个导出Excel文件模板的功能,原来实现是通过后台生成然后前端请求下载,这样要消耗IO资源,然后看了一下之前项目的导出功能,发现原来Vue+iview可以实现本地数据导出,不过iv ...
最新文章
- [转]MFC下关于“建立空文档失败”问题的分析
- 开发日记-20190428
- 《神秘的程序员们》漫画47:这些年你读过的书
- ListView控件的基本使用(方式一:使用ArrayAdapter适配器实现)
- 部署Django到云服务器(centos+nginx+mysql+uwsgi+python3)【操作篇(1)】
- 太原理工大学c语言课程设计报告,[太原理工大学C语言实验报告.doc
- java static 变量,和方法从属于类
- 滴滴业务中台构建实践,首次曝光
- 基于Vue、vue-i18n实现国际化(多语言)
- 定义一个工资变量c语言,《工资管理》c语言程序设计.doc
- swfobject参数详解
- 计算机培训作息时间安排,985学霸作息时间表“走红”,网友:越努力,越幸运...
- ssm酒店预订系统(ssm酒店管理系统民宿预订)ssm酒店客房预订系统宾馆JSP客房预订系统
- Y2K Bug and Bill Door
- 寄给你全宇宙的爱和自太古至永劫的思念
- 三菱FX5U系列PLC控制10轴设备成套资料打包三菱FX5U控制10轴伺服的设备成套电气图纸
- 86u 网页服务器,路由器怎么设置DMZ_华硕RT-AC86U路由器开启DMZ方法
- 漂亮妹妹~~~~~~`
- 文件服务器——NFS
- 网校系统是怎样搭建的?
热门文章
- C#如何让Listbox支持多选
- MaxScale初探
- [转]模拟芯片设计的四重境界
- 开源组件 Ehcache中被曝严重漏洞,影响多款Jira产品
- Apache Struts 修复 OGNL 技术中可能存在的 RCE 缺陷
- XP远程桌面连接强制登录
- Java日志组件间关系
- 知物由学 |“网状世界”下,无处可逃的信息安全
- 11月3日云栖精选夜读:《maven实战》读书笔记2——maven安装(windows和eclipse插件)...
- 前端开发学习之——dom ready和window onload的区别