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

12 篇文章 0 订阅


续上一章【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个,以后用到还会整理更新,今天就先到在这里吧..待续...



....谢谢阅读!


  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值