使用poi导出execl
2024-09-21 01:00:13
使用poi需要用到的jar包
本文的导出基于execl的模板导出,在大部分表头固定而格式花样比较复杂的建议使用本文介绍的方法(表头固定,只需要填充值)
1、在webroot目录下新建report文件夹来存放模板execl文件
2、jsp前台请求对应的action代码
String path = request.getSession().getServletContext().getRealPath("/")+"report/goodslaunch.xls";
String id = request.getParameter("id");//需要导出的数据的某Id
String name = this.xxService.loadNameById(id);//查询出id对应的真实名称 ByteArrayOutputStream os = (ByteArrayOutputStream)this.xxxService.expExecl(id, path);
if(null!=os && os.size()>0){
byte[] buffer = os.toByteArray(); String fileName = "[" + name + "]XX模块." + path.substring(path.lastIndexOf(".")+1, path.length());
// 设置response的Header
response.setContentType("application/x-msdownload; charset=UTF-8");
response.addHeader("content-type", "application/x-msexcel");
response.addHeader("content-disposition", "attachment; filename="+ new String(fileName.getBytes("gb2312"), "iso8859-1")); //这里必须转码,或者会有问题
response.setContentLength(buffer.length); OutputStream out = response.getOutputStream();
out.write(buffer);
out.flush();
os.close();
}
3、service代码
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory; /**
* 导出生成execl
* @throws Exception
*/
public OutputStream expExecl(String id,String execlPath) throws Exception{
List<XxDto> data = this.getSearchResult(id); Workbook wb = WorkbookFactory.create(new File(execlPath)); Font font = wb.createFont();
font.setFontName("微软雅黑");
font.setFontHeightInPoints((short) 10); CellStyle style = wb.createCellStyle();
style.setBorderBottom((short)1);
style.setBorderLeft((short)1);
style.setBorderRight((short)1);
style.setBorderTop((short)1);
style.setFont(font); if(data.size()>0){
Sheet sheet = wb.getSheetAt(0);
int rownum = 1;
for(XxDto item : data){
Row row = sheet.createRow(rownum++);
int cellnum = 0;
row.createCell(cellnum++).setCellValue(item.getXxx());
row.createCell(cellnum++).setCellValue(item.getXxx());
row.createCell(cellnum++).setCellValue(item.getXxx());
row.createCell(cellnum++).setCellValue(item.getXxx());
row.createCell(cellnum++).setCellValue(item.getXxx());
row.createCell(cellnum++).setCellValue(item.getXxx());
row.createCell(cellnum++).setCellValue(item.getXxx()); //设置样式
for(int i=0; i<cellnum; i++){
row.getCell(i).setCellStyle(style);
}
}
}
OutputStream os = new ByteArrayOutputStream();
wb.write(os);
return os;
}
当没有模板时需要创建文件(2007和2007以前的execl的写法是不同的)
Workbook wb;
Sheet sheet;
File file = new File(fileName);
//文件不存在
if(!file.exists()){
addFlag = false;
}
if(!addFlag){
if("xls".equals(fileSuffix)){
//老execl
wb = new HSSFWorkbook();
}else{
//新execl
wb = new XSSFWorkbook();
}
sheet = wb.createSheet("data");
createExeclHead(sheet);
}else{
//利用工厂读取execl可以不需要关心execl的版本问题
wb = WorkbookFactory.create(file);
sheet = wb.getSheetAt(0);
}
更详细的用法可以去官网看文档:http://poi.apache.org/
最新文章
- Sql server 2008 R2 实现远程异地备份
- mysql linux 备份脚本
- VLC 重新编译第三方库的预编译包contrib
- centos下安装usb摄像头驱动
- SessionId
- Cg(C for Graphic)标准函数库之数学函数与几何函数
- 《SQL必知必会》学习笔记二)
- 利用scrapy框架进行爬虫
- JS windows对象的top属性
- Intellij IDEA 从数据库生成 JPA Entity
- 【Python 07】汇率兑换1.0-2(基本元素)
- Spring MVC 使用介绍(十)—— 编码
- CSS实现水平垂直居中的1010种方式
- 从本地上传项目到 github 以及从github 下载项目到本地环境
- BZOJ3453: tyvj 1858 XLkxc(拉格朗日插值)
- cve-2017-11882 poc分析
- 词云wordcloud入门示例
- CF886C Petya and Catacombs
- 2016年Godaddy最新域名转出教程
- Windows 下VC++6.0制作、使用动态库和静态库
热门文章
- Windows操作系统下的MySQL主从复制及读写分离
- WCF服务端调用client.
- 使用Bootstrap 3开发响应式网站实践03,轮播下方的内容排版
- 《Windows核心编程》第九章——用内核对象进行线程同步
- arm指令版本
- python 查看模块的存放位置
- python限制进程、子进程占用内存大小、CPU时间的方法:resource模块
- Maintenance Plans(维护计划)详解【转】
- 【转载】Hybrid APP了解
- org.dom4j.DocumentException: unknown protocol: d Nested exception: unknown