文档章节

Java SpringMVC项目导出excel多种类对应工具类整理(util)

Yemon
 Yemon
发布于 2016/05/17 10:32
字数 1239
阅读 25
收藏 1


续上一章【Java SpringMVC项目导入excel2003以及2007多版本自动识别对应工具类(util)



上一章介绍如何导入excel进系统处理成MAP集合(详情请看上提供的链接),这次我又整理了一下如何快速导出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();
				}
			}
		}
	}

目前大概整理了这3个,以后用到还会整理更新,今天就先到在这里吧..待续...



....谢谢阅读!


本文转载自:http://blog.csdn.net/hj7jay/article/details/51260523

共有 人打赏支持
Yemon
粉丝 10
博文 321
码字总数 23076
作品 0
广州
架构师
私信 提问
jxl导入/导出Excel(Maven)

About jxl is a pure java 5 super-project that includes Enclosure, Peermi, Enshell, and Xlp. jxl && POI jxl是一个开源的Java Excel API项目,通过Jxl,Java可以很方便的操作微软的Exc......

coderzs
2017/08/31
0
0
java Excel导入导出,基于XML的实现,easy-excel使用

项目地址:http://git.oschina.net/lis1314/easy-excel 使用easy-excel 完成Excel导入导出功能 下面有如下的几个模型 学生模型,图书模型,作者模型 有如下的Excel文件格式,需要映射成学生实体...

lis1314
2016/06/19
0
18
Spring Boot 自定义日志详解

本节内容基于 Spring Boot 2.0. 你所需具备的基础 什么是 Spring Boot? Spring Boot 核心配置文件详解 Spring Boot 开启的 2 种方式 Spring Boot 自动配置原理、实战 Spring Boot 2.x 启动全...

Java技术栈
11/16
0
0
Spring MVC-集成(Integration)-生成Excel示例(转载实践)

以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_excel.htm 说明:示例基于Spring MVC 4.1.6。 以下示例显示如何使用Spring Web MVC框架生成Excel。首先,让我们使用E...

easonjim
2017/09/10
0
0
Java高级程序员面试大纲——错过了金三,你还要错过银四吗

跳槽时时刻刻都在发生,但是我建议大家跳槽之前,先想清楚为什么要跳槽。切不可跟风,看到同事一个个都走了,自己也盲目的开始面试起来(期间也没有准备充分),到底是因为技术原因(影响自己...

Java高级架构
04/27
0
0

没有更多内容

加载失败,请刷新页面

加载更多

开发十年,就只剩下这套架构体系了!

蓦然回首自己做开发已经十年了,这十年中我获得了很多,技术能力、培训、出国、大公司的经历,还有很多很好的朋友。但再仔细一想,这十年中我至少浪费了五年时间,这五年可以足够让自己成长为...

Java架构资源分享
7分钟前
0
0
spring切面编程中,如何获取request和response

HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();//获取request HttpServletResponse response = ((ServletRequest......

重城重楼
7分钟前
1
0
Numpy的广播功能

当我学习logitic函数,想用Python画出曲线。第一个版本这么实现: import numpy as np import matplotlib.pyplot as plt def logistic(x): return 1 /( 1 + np.exp(-x)) x = np.arange(-10,......

shengjuntu
8分钟前
0
0
Java堆:Shallow Size和Retained Size

Android Profiler分析的时候有如下两个值 Shallow Size: Total amount of Java memory used by this object type (in bytes). Retained Size: Total size of memory being retained due to a......

SuShine
14分钟前
0
0
建模常用的pandas语句

  pandas对象是Python常用的数据分析模块,它主要包括series对象,dataframe对象和index对象。每种对象都有自己所特有的方法和属性。今天小编更新下建模中常用的pandas语句。   额外提一...

数据星河
17分钟前
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部