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 ");
}
}
}
分享到:
相关推荐
jsp导出excel一般性问题及解决办法
jsp导出Excel;简单实用!导出Excel;导出Word jsp导出Excel;简单实用!导出Excel;导出Word jsp导出Excel;简单实用!导出Excel;导出Word
JSP导出excel和pdf格式JSP导出excel和pdf格式
JSP导出Excel
jsp导出excel并支持分sheet导出的方法.docx
关于jsp导出excel用javascript实现
jsp页面导出excel的实现相关代码,是从整个项目中把相关的几个页面拿出来分享一下
java及jsp下导出excel表格,真心好用
jsp导入导出excel文件用的是poi和学习心得
本文实例讲述了jsp导出excel并支持分sheet导出的方法。分享给大家供大家参考,具体如下: public DownloadFile exportToExcel() throws Exception{ String config_value = systemConfigService....
下面小编就为大家带来一篇JSP 导出Excel表格的实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本人试过了可以用,做jsp程序导出excel功能的可以参考下,下载后按照模版进行修改
查询数据跳转到当前的jsp,通过获取后台的接口数据导出excel文件,最后再自动跳转到上一个的页面。
本人验证过的jsp代码,ireport导出pdf excel报表
。。。
一种很简单的将JSP页面的table导出excel的方法
我在开发一个项目时,遇到jsp生成EXCEL有问题,在网上找了很多资料。现在终于解决了。共享出来,对大家可能能带来希望!