使用POI进行Excel文件下载的示例工程
程序员文章站
2022-05-19 20:26:44
...
说明:
1.下载http://www.apache.org/dyn/closer.cgi/poi/release/bin/poi-bin-3.7-20101029.zip后,将poi-3.7目录下的jar包放入lib目录, 再将此工程载入Eclipse/MyEclipse即可。
2.界面中文本框供输出数据量用,在我的T410上测试数据量在3.5W~3.6W之间,再多就报java.lang.OutOfMemoryError错误。
3.主要代码如下:
Sevlet代码:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->package com.heyang.action;
import java.io.BufferedOutputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.heyang.service.DownloadService;
/**
* POI下载的Servlet
* @author heyang
*
*/
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 56890894234786L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
String fileName="download.xls";
response.setHeader("Content-disposition", "attachment; filename="+fileName);// 设定输出文件头
response.setContentType("application/msexcel");// 定义输出类型
try{
int rowCount=Integer.parseInt(request.getParameter("rowCount"));
// 表头行
String[] headers=new String[]{"更新ID","账期","基站编号","基站名称","站点状态","部门名称","站点类型","占用类型","预提(元)","未核销金额","上期未核销","开始月份","结束月份","上期抄表数","本期抄表数","电价","电量","本期报账(元)","补提(元)","预提汇总(元)","成本中心","专业","本期报账单号","基站类别","线损"};
DownloadService service=new DownloadService();
HSSFWorkbook workbook=service.generateWorkbook(rowCount, headers.length);
ServletOutputStream out = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(out);
workbook.write(bos);
bos.flush();
bos.close();
}catch(Exception ex){
ex.printStackTrace();
}
return ;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
doPost(request, response);
}
}
import java.io.BufferedOutputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.heyang.service.DownloadService;
/**
* POI下载的Servlet
* @author heyang
*
*/
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 56890894234786L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
String fileName="download.xls";
response.setHeader("Content-disposition", "attachment; filename="+fileName);// 设定输出文件头
response.setContentType("application/msexcel");// 定义输出类型
try{
int rowCount=Integer.parseInt(request.getParameter("rowCount"));
// 表头行
String[] headers=new String[]{"更新ID","账期","基站编号","基站名称","站点状态","部门名称","站点类型","占用类型","预提(元)","未核销金额","上期未核销","开始月份","结束月份","上期抄表数","本期抄表数","电价","电量","本期报账(元)","补提(元)","预提汇总(元)","成本中心","专业","本期报账单号","基站类别","线损"};
DownloadService service=new DownloadService();
HSSFWorkbook workbook=service.generateWorkbook(rowCount, headers.length);
ServletOutputStream out = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(out);
workbook.write(bos);
bos.flush();
bos.close();
}catch(Exception ex){
ex.printStackTrace();
}
return ;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
doPost(request, response);
}
}
Service代码:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->package com.heyang.service;
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;
/**
* 下载服务类
*
* @author heyang
*
*/
public class DownloadService{
/**
* 生成工作簿对象
* @param rowCount
* @param columnCount
* @return
*/
public HSSFWorkbook generateWorkbook(int rowCount,int columnCount) throws Exception{
HSSFWorkbook workbook = new HSSFWorkbook(); //产生工作簿对象
HSSFSheet sheet = workbook.createSheet(); //产生工作表对象
String value=null;
HSSFRow row = null;
HSSFCell cell = null;
for(int i=0;i<rowCount;i++){
row = sheet.createRow(i);//创建一行
for(int j=0;j<columnCount;j++){
value=""+i+","+j;
cell = row.createCell(j);
cell.setCellValue(value);
cell = null;
}
row = null;
}
row = null;
cell = null;
return workbook;
}
}
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;
/**
* 下载服务类
*
* @author heyang
*
*/
public class DownloadService{
/**
* 生成工作簿对象
* @param rowCount
* @param columnCount
* @return
*/
public HSSFWorkbook generateWorkbook(int rowCount,int columnCount) throws Exception{
HSSFWorkbook workbook = new HSSFWorkbook(); //产生工作簿对象
HSSFSheet sheet = workbook.createSheet(); //产生工作表对象
String value=null;
HSSFRow row = null;
HSSFCell cell = null;
for(int i=0;i<rowCount;i++){
row = sheet.createRow(i);//创建一行
for(int j=0;j<columnCount;j++){
value=""+i+","+j;
cell = row.createCell(j);
cell.setCellValue(value);
cell = null;
}
row = null;
}
row = null;
cell = null;
return workbook;
}
}
上一篇: 平面设计师必备的十个技能
下一篇: 2011年项目回顾与总结分析
推荐阅读
-
PHP使用ajax的post方式下载excel文件简单示例
-
showModelDialog弹出文件下载窗口的使用示例
-
java使用POI实现excel文件的导入和导出(通用方法)
-
AngularJS基于http请求实现下载php生成的excel文件功能示例
-
3种下载文件程序的思考,为何使用NIO进行异步网络通讯
-
导出大数据量excel文件——poi的SXSSFWorkbook对象使用
-
使用POI同时对Excel文件进行读和写操作时避免Invalid header signatu
-
JavaWeb中使用poi进行excel的自定义模板下载
-
使用python对文件中的单词进行提取的方法示例
-
Linux使用scp命令进行文件远程的上传或下载