说明:
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);
}
}
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;
}
}