日期:2014-05-16  浏览次数:20368 次

jsp页面导出保存word或Excel几种方法及个人总结
以下是本人在做EXCEL/WORLD中在网上找的方法及个人总结:

方法1:

<%@ page contentType="application/msword; charset=GBK" %>  //或者vnd.ms-word
<%@ page contentType="application/msexcel; charset=GBK" %> //或者vnd.ms-excel
通过设置可以使原来页面的内容在word或者excel中表现出来。

如果需要把word或者excel文档下载下来,只需在jsp页面上面加上如下代码:
<%
//线上浏览的方式:
response.setHeader("Content-disposition","inline; filename=test1.xls");
//下载的方式:
response.setHeader("Content-disposition","attachment; filename=test2.xls");
//以上这行设定传送到前端浏览器时的档名为test1.xls
//就是靠这一行,让前端浏览器以为接收到一个excel档
%>


方法2:
通过jxl等第三方来操作excel,这种网上资料丰富,
但这种又分两个方法:下面会介绍
1)在服务器端生成文件
String download_path = request.getSession().getServletContext().getRealPath("/") + "***.excel";
	    File file = new File(download_path);  //生成个应的文件

	    WritableWorkbook book = Workbook.createWorkbook(file);
	    WritableSheet sheet = book.createSheet("sheet", 0);
//配置第一行的头	    
  Label cardNoLabel = new Label(0, 0, "卡号");
	    Label cardTypeLabel = new Label(1, 0, "ICCID");
	    Label iccIdLabel = new Label(2, 0, "卡类型");
	    Label version = new Label(3, 0, "版本号");
	    Label m1NoLabel = new Label(4,0,"M1编号");
	    Label errorLabel = new Label(5, 0, "错误原因");
	    sheet.addCell(cardTypeLabel);
	    sheet.addCell(cardNoLabel);
	    sheet.addCell(iccIdLabel);
	    sheet.addCell(errorLabel);
	    sheet.addCell(version);
	    sheet.addCell(m1NoLabel);

//遍历生成相应数据
  for (int i = 0; i < size; i++) {
		String[] objects = cardList.get(i);
		String cardType = objects[0];
		String cardNo = objects[1].startsWith("0000") ? objects[1].substring(4) : objects[1];
		String iccId = objects[2];
		String m1No = objects[3];
		Object cardNoExists = existsQuery.setString(0, cardNo).uniqueResult();
		Object iccIdExists = iccIdQuery.setString(0, iccId).uniqueResult();
		if (cardNoExists != null) {
		    sheet.addCell(new Label(0, i - successNum + 1, cardNo));
		    sheet.addCell(new Label(1, i - successNum + 1, iccId));
		    sheet.addCell(new Label(2, i - successNum + 1, cardType));
		    sheet.addCell(new Label(3, i - successNum + 1, apVersion));
		    sheet.addCell(new Label(4, i - successNum + 1, m1No));
		    sheet.addCell(new Label(5, i - successNum + 1, "卡号已存在"));
		} else if (iccIdExists != null) {
		    sheet.addCell(new Label(0, i - successNum + 1, cardNo));
		    sheet.addCell(new Label(1, i - successNum + 1, iccId));
		    sheet.addCell(new Label(2, i - successNum + 1, cardType));
		    sheet.addCell(new Label(3, i - successNum + 1, apVersion));
		    sheet.addCell(new Label(4, i - successNum + 1, m1No));
		    sheet.addCell(new Label(5, i - successNum + 1, "iccid已存在"));
		} 
	    }
	    book.write();
	    book.close();


2)服务端不生成文件
 // 1.设定好response的相关属性:
	    response.setContentType("application/vnd.ms-excel");
	    // URLEncoder.encode(qList.get("FUNCTION_NAME").toString(), "gbk")
	    String downloadFileName = new String(qList.get("FUNCTION_NAME").toString().getBytes("GBK"), "ISO-8859-1");
	    response.setHeader("Content-Disposition", "attachment;   filename=\"" + downloadFileName + ".xls" + "\"");

	    // 2.取到response的OutputStream实例,并用这个实例化一个WritableWorkbook对象
	    OutputStream os = response.getOutputStream();
	    WritableWorkbook wwb = Workbook.createWorkbook(os);

	    int ncout = resultList.size();
	    int maxnum = 50000; // 一次最多写入量
	    int times = (ncout + maxnum - 1) / maxnum;

	    // 大循环
	    for (int t = 0; t < times; t++) {

		// 新建一张表
		WritableSheet wsheet = wwb.createSheet("members_" + (t + 1), t);
		// 设置表头
		Map<String, Object> tableNameMap = null;
		Label label;
		for (int i = 0; i < tableNameList.size(); i++) {
		    tableNameMap = tableNameList.get(i);
		    label = new Label(i, 0, tableNameMap.get("COLUMN_CHIN").toString());
		    wsheet.addCell(label);
		}