poi导出excel
2015-08-20 19:18
176 查看
public String getListExcel() throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
authenticationCount = new AuthenticationCount();
authenticationCount.setStaticsIndex(request.getParameter("staticsIndex"));
try {
List<AuthenticationCount> authList = (List<AuthenticationCount>) getWhiteListsMap()
.get("dataList");
if (authList != null && authList.size() > 0) {
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("导出表");
sheet.setDefaultColumnWidth(40);
sheet.setDefaultRowHeightInPoints(20);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("地市");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("组名");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("用户手机号");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("统计结果");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
for (int i = 0; i < authList.size(); i++) {
row = sheet.createRow((int) i + 1);
AuthenticationCount authCount = (AuthenticationCount) authList
.get(i);
// 第四步,创建单元格,并设置值
row.createCell(0).setCellValue(authCount.getCompany());
row.createCell(1).setCellValue(authCount.getGroupName());
row.createCell(2).setCellValue(authCount.getMobilePhone());
row.createCell(3).setCellValue(authCount.getCount());
}
// response.addHeader("Content-Disposition", "attachment;filename=\"" + resourcefile.getName());
response.setContentType("application/vnd.ms-excel");
String dataTableName = DateUtils.format(new Date(),"yyyyMM")+"01"+new Date().getTime();
response.addHeader("Content-disposition","attachment;filename=" + dataTableName + ".xls");
OutputStream ouputStream = response.getOutputStream(); //new FileOutputStream("e:\\d.xls");
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
<script>
var globalCxtPath = "<%=basePath%>";
var staticIndex = $('')
function exportExcel(){
var staticsIndex = $('input[name="authenticationCount.staticsIndex"]:checked').val();
alert(staticsIndex);
var url = "portalStatics_getListExcel.action?staticsIndex="+staticsIndex;
window.location.href = globalCxtPath+url;
}
</script>
所用包 poi-3.13-beta1-20150723.jar
public String getListExcel() throws Exception {
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
authenticationCount = new AuthenticationCount();
authenticationCount.setStaticsIndex(request.getParameter("staticsIndex"));
try {
List<AuthenticationCount> authList = (List<AuthenticationCount>) getWhiteListsMap()
.get("dataList");
if (authList != null && authList.size() > 0) {
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet("导出表");
sheet.setDefaultColumnWidth(40);
sheet.setDefaultRowHeightInPoints(20);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int) 0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("地市");
cell.setCellStyle(style);
cell = row.createCell((short) 1);
cell.setCellValue("组名");
cell.setCellStyle(style);
cell = row.createCell((short) 2);
cell.setCellValue("用户手机号");
cell.setCellStyle(style);
cell = row.createCell((short) 3);
cell.setCellValue("统计结果");
cell.setCellStyle(style);
cell = row.createCell((short) 4);
for (int i = 0; i < authList.size(); i++) {
row = sheet.createRow((int) i + 1);
AuthenticationCount authCount = (AuthenticationCount) authList
.get(i);
// 第四步,创建单元格,并设置值
row.createCell(0).setCellValue(authCount.getCompany());
row.createCell(1).setCellValue(authCount.getGroupName());
row.createCell(2).setCellValue(authCount.getMobilePhone());
row.createCell(3).setCellValue(authCount.getCount());
}
// response.addHeader("Content-Disposition", "attachment;filename=\"" + resourcefile.getName());
response.setContentType("application/vnd.ms-excel");
String dataTableName = DateUtils.format(new Date(),"yyyyMM")+"01"+new Date().getTime();
response.addHeader("Content-disposition","attachment;filename=" + dataTableName + ".xls");
OutputStream ouputStream = response.getOutputStream(); //new FileOutputStream("e:\\d.xls");
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
<script>
var globalCxtPath = "<%=basePath%>";
var staticIndex = $('')
function exportExcel(){
var staticsIndex = $('input[name="authenticationCount.staticsIndex"]:checked').val();
alert(staticsIndex);
var url = "portalStatics_getListExcel.action?staticsIndex="+staticsIndex;
window.location.href = globalCxtPath+url;
}
</script>
所用包 poi-3.13-beta1-20150723.jar
相关文章推荐
- error: Refusing toundefine while domain managed save image exists
- 畅通工程
- poj2038Fractal盒分形深搜递归
- 精通Hibernate——Session的保存、更新、删除和查询
- Robotium调用getActivity()导致程序挂起的方法
- 问题解决——复合检测项目的定义和使用
- 链表基本功能
- POJ-2593
- 安装Maven
- JAVA简单的群聊例子
- .net Socket编程
- poj 3259 Wormholes 【判断负环】
- 二叉搜索树递归算法
- android音乐柱状频谱实现
- eclipes下NDK调试出现Unknown Application ABI:Unable to detect application ABI's错误的解决
- NYOJ 236 心急的c小加
- redis批量删除
- 推荐一个自动切图/自动裁图/合并图集软件
- IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)
- [LeetCode]Search a 2D Matrix II