Apache POI实现excel导出
链接:http://poi.apache.org/
Excel数据导出步骤:
使用poi 完成账户数据的导出功能 导入poi jar包并添加到classpath中
1、查询数据
2、定义导出头
String fileName="测试数据.xls";
resp.setHeader("Content-disposition","attachment;filename="new String(fileName.getBytes("gb2312"),"ISO8859-1")); //设置文件头编码格式
resp.setContentType("APPLICATION/OCTET-STREAM;charset=UTF-8");//设置类型
resp.setHeader("Cache-Control","no-cache");//设置头
resp.setDateHeader("Expires", 0);//设置日期头
3、创建工作簿HSSFWorkbook 对象
HSSFWorkbook book=new HSSFWorkbook();
4、由工作簿创建表HSSFSheet对象
HSSFSheet sheet=book.createSheet("账户表数据");
5、创建行对象Row
Row row=sheet.createRow(i);
6、创建单元格 Cell
Cell cell=row.createCell(0);
日期格式处理:
CellStyle cellStyle=book.createCellStyle(); CellStyle cellStyle=book.createCellStyle(); cellStyle.setDataFormat(book.createDataFormat().getFormat("yyyy-MM-dd")); cell.setCellStyle(cellStyle) |
7、写出流 刷新缓冲流 关闭流对象
book.write(resp.getOutputStream());
resp.getOutputStream().flush();
resp.getOutputStream().close();
-----------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------------
1、导入相应jar包
2、相应方法
/**
* excel导出
* @param req
* @param resp
* void
* @throws IOException
*/
private void exportAccountData(HttpServletRequest req,
HttpServletResponse resp) throws IOException { String sql=" select id,aname,type,money,remark,create_time as createTime,update_Time as updateTime from account "
+ " where user_id=?"; User user=(User) req.getSession().getAttribute("user"); List<Account> list=MyBaseDao.queryRows(sql, new Object[]{user.getId()}, Account.class); if(null!=list&&list.size()>0){ String fileName="账户数据.xls";
resp.setHeader("Content-disposition","attachment;filename="
+new String(fileName.getBytes("gb2312"),"ISO8859-1")); //设置文件头编码格式
resp.setContentType("APPLICATION/OCTET-STREAM;charset=UTF-8");//设置类型
resp.setHeader("Cache-Control","no-cache");//设置头
resp.setDateHeader("Expires", 0);//设置日期头 HSSFWorkbook book=new HSSFWorkbook(); HSSFSheet sheet=book.createSheet();
CellStyle cellStyle=book.createCellStyle(); cellStyle.setDataFormat(book.createDataFormat().getFormat("yyyy-MM-dd")); for(int i=0;i<list.size();i++){
Account account=list.get(i);
Row row=sheet.createRow(i); Cell cell1=row.createCell(0);
cell1.setCellValue(account.getId()); Cell cell2=row.createCell(1);
cell2.setCellValue(account.getAname());
Cell cell3=row.createCell(2);
cell3.setCellValue(account.getMoney());
Cell cell4=row.createCell(3);
cell4.setCellValue(account.getType());
Cell cell5=row.createCell(4);
cell5.setCellValue(account.getRemark()); Cell cell6=row.createCell(5);
cell6.setCellValue(account.getCreateTime());
cell6.setCellStyle(cellStyle); Cell cell7=row.createCell(6);
cell7.setCellStyle(cellStyle);
cell7.setCellValue(account.getUpdateTime());
} book.write(resp.getOutputStream()); resp.getOutputStream().flush();
resp.getOutputStream().close();
}
}
3、jsp中添加excel导出
queryAccount.jsp
最新文章
- Java 消息摘要 散列 MD5 SHA
- Coursera-Getting and Cleaning Data-Week3-dplyr+tidyr+lubridate的组合拳
- php SimpleXML 例子
- WPF入门教程系列一——基础
- Unity3D引擎扩展中的编辑器定制方法
- MSSQL 判断一个时间段是否在另一个时间段内!
- 聊聊 if else 那些事
- error C2065: “CMainFrame”: 未声明的标识符
- 黄聪:走进wordpress 详细说说template-loader.php
- 【转】【玩转cocos2d-x之二十三】多线程和同步03-图片异步加载
- JavaScript遍历方式详解
- 关于贴友的一个书本页面简单布局(html+css)的实现
- 将项目添加到Finder侧边栏和工具栏
- Android TextView属性
- Polygon对象
- C与C++不同
- 单链表的插入删除操作(c++实现)
- Video Cards
- 基于jquery的城市选择插件
- GDI+_Png图片浏览器