一.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文件的更多相关文章

  1. 用DataGridView导入TXT文件,并导出为XLS文件

    使用 DataGridView 控件,可以显示和编辑来自多种不同类型的数据源的表格数据.也可以导出.txt,.xls等格式的文件.今天我们就先介绍一下用DataGridView把导入txt文件,导出x ...

  2. 导出csv xls文件数字会自动变科学计数法的解决方式

    将数据导出excel文件变成科学计数法问题:     分析: 用程序导出的csv文件,当字段中有比较长的数字字段存在时,在用excel软件查看csv文件时就会变成科学技术法的表现形式.    其实这个 ...

  3. java poi导出EXCEL xls文件代码

    String _currentPage = request.getParameter("currentPage"); Integer currentPage = 0; if(_cu ...

  4. NPOI 操作数据库中数据的导入导出(Excel.xls文件) 和null数据的处理。

    App.config: <?xml version="1.0" encoding="utf-8" ?> <configuration> ...

  5. java使用POI jar包读写xls文件

    主要使用poi jar来操作excel文件.代码中用到的数据库表信息见ORACLE之表.使用public ArrayList<Person> getPersonAllRecords()获得 ...

  6. phpexcel 导出到xls文件的时候出现乱码解决

    在header() 前面加上ob_end_clean() 函数, 清除缓冲区, 这样就不会乱码了! <?php include 'global.php'; $ids = $_GET['ids'] ...

  7. 使用phpexcel导出到xls文件的时候出现乱码解决

    <?php include 'global.php'; $ids = $_GET['ids']; $sql = "select * from crm_cost_end where id ...

  8. 多线程导出大规模excel文件

    文章有点水,和前几篇没有太大区别,但是单线程处理大文件导出会非常耗时间,用到多线程才能更加合理的利用资源.大文件也可能会超出excel工作表范围.这里也有相应处理 参考:用DataGridView导入 ...

  9. 在ASP.NET Web Forms中使用页面导出伪xls Excel表格

    将数据导出为Excel表格是比较常见的需求,也有很多组件支持导出真正的Excel表格.由于Excel能打开HTML文件,并支持其中的table元素以及p之类的文本元素的显示,所以把.html扩展名改为 ...

随机推荐

  1. Linux下安装Pcntl PHP扩展

    //解压源码包 [root@centos src]# tar -zxvf php-5.5.35.tar.gz //进入pcntl扩展库 [root@centos src]# cd php-5.5.35 ...

  2. Java Serializable(序列化)的理解和总结

    1.序列化是干什么的?       简单说就是为了保存在内存中的各种对象的状态(也就是实例变量,不是方法),并且可以把保存的对象状态再读出来.虽然你可以用你自己的各种各样的方法来保存object st ...

  3. 【C#】LINQ

    一.什么是LINQ 长期以来,开发社区形成以下的格局: 1.面向对象与数据访问两个领域长期分裂,各自为政. 2.编程语言中的数据类型与数据库中的数据类型形成两套不同的体系,例如: C#中字符串用str ...

  4. iOS 查看代码总行数

    1.打开终端 2.cd 拖入工程 回车 3.输入命令 find . "(" -name "*.m" -or -name "*.mm" -or ...

  5. kali linux之sqlmap

    一款开源的命令行自动SQL注入工具,它能够对多种主流数据库进行扫描支持,基于Python环境. 检测动态页面中get/post参数,cookie,http头 数据榨取/文件系统访问 操作系统命令执行 ...

  6. 【转】VS2010不能引用System.Data.OracleClient解决方法

    源地址:http://blog.csdn.net/iloli/article/details/8484674

  7. Docker Community Edition 镜像使用帮助

    1.什么是Docker 容器技术 在计算机的世界中,容器拥有一段漫长且传奇的历史.容器与管理程序虚拟化 (hypervisor virtualization,HV)有所不同,管理程序虚拟化通过中间层将 ...

  8. linux的发行版

    Linux的不同版本以及应用领域 1.Linux内核及发行版介绍 <1>Linux内核版本 内核(kernel)是系统的心脏,是运行程序和管理像磁盘和打印机等硬件设备的核心程序,它提供了一 ...

  9. angularJs路由的使用

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  10. springcloud微服务总结 zuul

    一 springcloud网关组件理解: 为什么需要网关呢? 我们知道我们要进入一个服务本身,很明显我们没有特别好的办法,直接输入IP地址+端口号,我们知道这样的做法很糟糕的,这样的做法大有问题,首先 ...