链接: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

最新文章

  1. Java 消息摘要 散列 MD5 SHA
  2. Coursera-Getting and Cleaning Data-Week3-dplyr+tidyr+lubridate的组合拳
  3. php SimpleXML 例子
  4. WPF入门教程系列一——基础
  5. Unity3D引擎扩展中的编辑器定制方法
  6. MSSQL 判断一个时间段是否在另一个时间段内!
  7. 聊聊 if else 那些事
  8. error C2065: “CMainFrame”: 未声明的标识符
  9. 黄聪:走进wordpress 详细说说template-loader.php
  10. 【转】【玩转cocos2d-x之二十三】多线程和同步03-图片异步加载
  11. JavaScript遍历方式详解
  12. 关于贴友的一个书本页面简单布局(html+css)的实现
  13. 将项目添加到Finder侧边栏和工具栏
  14. Android TextView属性
  15. Polygon对象
  16. C与C++不同
  17. 单链表的插入删除操作(c++实现)
  18. Video Cards
  19. 基于jquery的城市选择插件
  20. GDI+_Png图片浏览器

热门文章

  1. Android中的动画使用总结
  2. Loadrunder之脚本篇——参数化取值策略
  3. JS字符串数组转换
  4. 跨平台移动开发_Windows Phone 8 使用 PhoneGap 方法
  5. 关闭Selinux 命令
  6. 简单web作业---书籍介绍的相关网页编写
  7. C++中substr的用法
  8. java JSON 序列化类(List&lt;Object&gt; 转String)
  9. Spring插件的安装与卸载---笔记
  10. spark学习2-1(hive1.2安装)