java将list<T>导出为xls文件
一.action层
/**
* 导出list
*/
@SuppressWarnings("unchecked")
public void exportBatch() {
request = ServletActionContext.getRequest();
response = ServletActionContext.getResponse();
String batchNo = request.getParameter("batchNo");
try {
//查询导出List<T>列
exportlList = this.batchService.findExportBatchService(batchNo);
} catch (Exception e) {
e.printStackTrace();
}
//导出表格
HSSFWorkbook wb = batchService.exportBatch(exportlList);
File xlsFile = new File("D://" + "fielName.xls");
try {
FileOutputStream fos = new FileOutputStream(xlsFile);
wb.write(fos);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
String fileName = "fielName.xls";
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/xls");
response.setHeader("Location", fileName);
response.setHeader("Content-Disposition", "attachment;fileName="+ new String((xlsFile.getName()).getBytes("UTF-8")));
ServletOutputStream os = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
FileInputStream fis = new FileInputStream(xlsFile);
byte[] b = new byte[1000];
int length;
while ((length = fis.read(b)) > 0) {
bos.write(b, 0, length);
}
os.flush();
bos.flush();
os.close();
bos.close();
fis.close();
xlsFile.delete();
} catch (Exception e1) {
e1.printStackTrace();
}
}
二、serviceImpl层
/**
* 导出Excel
*/
@SuppressWarnings("deprecation")
public HSSFWorkbook exportBatch(List<SecurityCode> list) {
//获取客户端访问路径
String qRCodeUrl="http://www.baidu.com";
// 第一步,创建一个webbook,对应一个Excel文件
HSSFWorkbook wb = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
String cardInfoName = "sheet页名";
HSSFSheet sheet = wb.createSheet(cardInfoName);
// 第三步,在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);
cell.setCellValue("列名一");
cell.setCellStyle(style);
cell = row.createCell((short) 5);
cell.setCellValue("列名一");
cell.setCellStyle(style);
cell = row.createCell((short) 6);
cell.setCellValue("列名一");
cell.setCellStyle(style);
for (int i = 0; i < list.size(); i++) {
row = sheet.createRow((int) i + 1);
SecurityCode m = list.get(i);
row.createCell((short) 0).setCellValue(i + 1);
row.createCell((short) 1).setCellValue(m.getBatchNo() == null || "null".equals(m.getBatchNo()) ? "" : m.getBatchNo().toString());
row.createCell((short) 2).setCellValue(m.getProductNo() == null || "null".equals(m.getProductNo() ) ? "" : m.getProductNo() .toString());
row.createCell((short) 3).setCellValue(m.getProductName() == null|| "null".equals(m.getProductName()) ? "" : m.getProductName().toString());
row.createCell((short) 4).setCellValue(m.getCreateTime() == null|| "null".equals(m.getCreateTime()) ? "" : m.getCreateTime().toString());
row.createCell((short) 5).setCellValue(m.getSecurityNo() == null|| "null".equals(m.getSecurityNo()) ? "" : m.getSecurityNo().toString());
row.createCell((short) 6).setCellValue(qRCodeUrl == null|| "null".equals(qRCodeUrl) ? "" :qRCodeUrl.toString()+m.getSecurityNo());
}
return wb;
}
java将list<T>导出为xls文件的更多相关文章
- 用DataGridView导入TXT文件,并导出为XLS文件
使用 DataGridView 控件,可以显示和编辑来自多种不同类型的数据源的表格数据.也可以导出.txt,.xls等格式的文件.今天我们就先介绍一下用DataGridView把导入txt文件,导出x ...
- 导出csv xls文件数字会自动变科学计数法的解决方式
将数据导出excel文件变成科学计数法问题: 分析: 用程序导出的csv文件,当字段中有比较长的数字字段存在时,在用excel软件查看csv文件时就会变成科学技术法的表现形式. 其实这个 ...
- java poi导出EXCEL xls文件代码
String _currentPage = request.getParameter("currentPage"); Integer currentPage = 0; if(_cu ...
- NPOI 操作数据库中数据的导入导出(Excel.xls文件) 和null数据的处理。
App.config: <?xml version="1.0" encoding="utf-8" ?> <configuration> ...
- java使用POI jar包读写xls文件
主要使用poi jar来操作excel文件.代码中用到的数据库表信息见ORACLE之表.使用public ArrayList<Person> getPersonAllRecords()获得 ...
- phpexcel 导出到xls文件的时候出现乱码解决
在header() 前面加上ob_end_clean() 函数, 清除缓冲区, 这样就不会乱码了! <?php include 'global.php'; $ids = $_GET['ids'] ...
- 使用phpexcel导出到xls文件的时候出现乱码解决
<?php include 'global.php'; $ids = $_GET['ids']; $sql = "select * from crm_cost_end where id ...
- 多线程导出大规模excel文件
文章有点水,和前几篇没有太大区别,但是单线程处理大文件导出会非常耗时间,用到多线程才能更加合理的利用资源.大文件也可能会超出excel工作表范围.这里也有相应处理 参考:用DataGridView导入 ...
- 在ASP.NET Web Forms中使用页面导出伪xls Excel表格
将数据导出为Excel表格是比较常见的需求,也有很多组件支持导出真正的Excel表格.由于Excel能打开HTML文件,并支持其中的table元素以及p之类的文本元素的显示,所以把.html扩展名改为 ...
随机推荐
- [转]B+Tree图解
一, M阶B+树的定义(M阶是指一个节点最多能拥有的孩子数,M>2): 图1.1 3阶B+树 (1)根结点只有1个,分支数量范围[2,m]. (2)除根以外的非叶子结点,每个结点包含分支数 ...
- openfiler的介绍与使用
一. Openfiler简介 Openfiler 能把标准x86/64架构的系统变成一个强大的NAS.SAN存储和IP存储网关,为管理员提供一个强大的管理平台,并能能应付未来的存储需求.依赖如VMwa ...
- 获取oracle 库所有表名
(mybatis多参)
- MySQL中查询时对字母大小写的区分
我相信很多人在mysql中查询时都遇到过mysql不区分字母大小写的情况:如以下例子: 1.SELECT * FROM `user` WHERE userpass = 'Z20'; 结果为: 2.SE ...
- 利用keytool工具生成数字证书
一.制作数字证书 因测试微信小程序, 腾讯要求使用 https协议,所以需要使用证书.使用jdk工具制作数字证书流程如下: 1.查看JDK是否安装,使用命令java -version 2.切换目录至 ...
- 写excel
一.写excel import xlwt book = xlwt.Workbook()# 创建excel sheet = book.add_sheet('stu_info')# 加一个sheet sh ...
- Jmeter_使用IE代理录制脚本
因为项目登录的密码需要RSA加密,选用了jmeter作为压测工具: 就自己本次项目,顺便学习Jmeter,做一个简单的记录,本文主要介绍使用IE代理录制脚本: 自己也尝试过使用Badboy录制,还是喜 ...
- Nginx 进行性能配置
总所周知,网络上我们购买的服务器的性能各不相同,如果采用 Nginx 的默认配置的话,无法将服务器的全部性能优势发挥出来,我们应该选择适合自己需求的配置. 当我们默认安装后 Nginx 后,我们便得到 ...
- networkx如何将图写到邻接矩阵里?
nx.write_adjlist(G1,graph_filename1)#生成的是二进制文件nx.write_adjlist(G2,graph_filename2)
- Jenkins持续集成企业实战系列之Jenkins插件下载及邮件配置-----05
注:原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任. 最初接触Jenkins也是由于公司需求,根据公司需求Java代码项目升级的.(公司是 ...