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扩展名改为 ...
随机推荐
- CentOS7下源码包方式安装rabbitmq
1.先安装erlang http://www.cnblogs.com/justphp/p/6093880.html 2.下载rabbitmq rpm包: wget http://www.rabbitm ...
- 分享一个好用的功能强大的节点树jQuery插件
http://www.treejs.cn/
- C# 取得内网IP、外网IP、客户端IP方法
前言 在 Windows Form Application 里对于取得 IP Address 有内网.外网两种 IP Address ,如果只需要取得内网 IP Address ,可以透过使用 IPH ...
- 解决Eclipse+ADT连接夜神模拟器失败问题
问题1: 运行夜神模拟器,cmd执行 adb devices不显示 答案1: 原因可能是夜神模拟器的adb版本与sdk下的adb版本不一致,拷贝sdk下的adb.exe并改名为nox_adb.exe替 ...
- orcl 对table的一些操作
删除 table:drop table 表名: 恢复删除 : flashback table 表名 to before drop: 清空table : truncate table 表名; 恢复清空: ...
- Task async await
暇之余,究多Task.async.await. using System; using System.Collections.Generic; using System.Linq; using Sys ...
- 从头开始学eShopOnContainers——设置WebSPA单页应用程序
一.简介 Web SPA单页应用程序需要一些额外的步骤才能使其工作,因为它需要在生成Docker镜像之前构建JavaScript框架依赖项和JS代码. 二.安装基础环境 1.安装NPM 为了能够使用n ...
- 以太坊系列之五: p2p的nat模块--以太坊源码学习
p2p的nat模块 该模块相对比较简单,因为nat的真正实现并不在此模块,主要是使用了第三方的nat-upnp和nat-pmp来实现真正的穿透(端口映射). 对外公布的接口 ```go // An i ...
- 日期 Date()
1.Date 对象用于处理日期和时间.创建 Date 对象的语法:var myDate=new Date()Date 对象会自动把当前日期和时间保存为其初始值.2.参数形式有以下5种: new Dat ...
- 关于Java中SQL语句的拼接规则
实现目标语句是这个注意,这里的java变量是idd int idd; String sql = "SELECT id, piUrl FROM picinfos WHERE id BETWEE ...