`
sunjing21
  • 浏览: 157701 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
文章分类
社区版块
存档分类
最新评论

jsp导出excel

 
阅读更多

jsp导出excel有两种不同的方式 一种时网页直接导出 另一种是通过开源jar直接操作excel


一.网页导出格式

导出excel按钮直接请求此页面,然后将要导出的值放到request里面即可

//此处必须添加 指定excel格式

<%@ page contentType="application/vnd.ms-excel; charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
response.addHeader("Content-Disposition", "filename=test.xls");
%>
<html>
<head>
<meta name="Generator" content="Microsoft Excel 11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>

<body>
<center>
<b>表格名</b>
</center>
<br>
<table border="1" align="center" cellpadding="0" cellspacing="1">
<tr>
<td>
姓名
</td>
<td>
年龄
</td>
<td>
生日
</td>
</tr>
<c:forEach var="user" items="${userInfo}}">
<tr>
<td>
${user.userName}
</td>
<td>
${user.age}
</td>
<td>
${user.date}
</td>
</tr>
</c:forEach>
</table>
</body>
</html>

二。利用开源jar POI


Servlet代码如下:直接请求Servelt,处理完成不需要跳转,这个请求会事另外一个页面跳转

response.setContentType("application/vnd.ms-excel;charset=UTF-8");// // 指定文件的保存类型。
POIExcel we=new POIExcel();
we.getExcel("111.xls",response.getOutputStream(),list);


POIExcel代码:一看就知道什么意思


package com.sunjing.jquery;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class POIExcel {

/**
* 导出excel
* @param sheetName sheet名称
* @param output 响应输出流
* @param list 导出结果集
*/
public void getExcel(String sheetName, OutputStream output, List list) {
HSSFWorkbook wb = new HSSFWorkbook();

HSSFSheet sheet1 = wb.createSheet("sheet1");
//创建题头行
HSSFRow titleRow = sheet1.createRow((short) 0);
//创建题头列0
HSSFCell cell0= titleRow.createCell((short) 0);
cell0.setEncoding(HSSFCell.ENCODING_UTF_16);
cell0.setCellValue("姓名");

//创建题头列1
HSSFCell cell1 = titleRow.createCell((short) 1);
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("年龄");

//创建题头列2
HSSFCell cell12 = titleRow.createCell((short) 2);
cell12.setEncoding(HSSFCell.ENCODING_UTF_16);
cell12.setCellValue("出生日期");

//创建内容行
int rowBegin = 1;
for (int i = 0; i < list.size(); i++) {
UserInfo info = (UserInfo) list.get(i);
HSSFRow contentRow = sheet1.createRow((short) rowBegin);

HSSFCell contentCell0 = contentRow.createCell((short) 0);
contentCell0.setEncoding(HSSFCell.ENCODING_UTF_16);
//注意此处要判断是否为空 下面属性判断省略
if(null != info.getUserName() && !"".equals(info.getUserName())){
contentCell0.setCellValue(info.getUserName());
}
HSSFCell contentCell1 = contentRow.createCell((short) 1);
contentCell1.setEncoding(HSSFCell.ENCODING_UTF_16);
contentCell1.setCellValue(info.getAge().intValue());

HSSFCell contentCell2 = contentRow.createCell((short) 2);
contentCell2.setEncoding(HSSFCell.ENCODING_UTF_16);
contentCell2.setCellValue(info.getDate());

rowBegin++;
}
//FileOutputStream fileout=new FileOutputStream("workbook.xls");
try {
output.flush();
wb.write(output);
output.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Output is closed ");
}
}
}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics