一.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. 现代C++学习笔记之二入门篇2,数据转换

    static_cast:    这种强制转换只会在编译时检查. 如果编译器检测到您尝试强制转换完全不兼容的类型,则static_cast会返回错误. 您还可以使用它在基类指针和派生类指针之间强制转换, ...

  2. css总结3:Flex 布局教程:Flex-demos(转)

    上一篇文章介绍了Flex布局的语法,今天介绍常见布局的Flex写法. 你会看到,不管是什么布局,Flex往往都可以几行命令搞定. 我只列出代码,详细的语法解释请查阅<Flex布局教程:语法篇&g ...

  3. c#缓存介绍

    #缓存介绍(转) 本章导读 缓存主要是为了提高数据的读取速度.因为服务器和应用客户端之间存在着流量的瓶颈,所以读取大容量数据时,使用缓存来直接为客户端服务,可以减少客户端与服务器端的数据交互,从而大大 ...

  4. MongoDB整理笔记の管理Replica Sets

    一.读写分离 从库能进行查询,这样可以分担主库的大量的查询请求.   1.先向主库中插入一条测试数据 [root@localhost bin]# ./mongo --port 28010 MongoD ...

  5. MariaDB 数据库迁移

    一.为什么要迁移 我的七月小说站点放在JCloud上,恕我直言,配合我的Aliyun服务器进行数据交互,那是相当的慢,没办法,京东云上面十几块钱的公网ip,也就这样了. 所以我决定把web服务器和数据 ...

  6. 【转载】C# DataGridView 通过代码设置样式

    // 表格上下左右自适应 dataGridView.Anchor = (AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom | An ...

  7. shipyard

    https://www.ivankrizsan.se/2016/07/10/managing-containers-shipyard/ kubernetes中文社区:https://www.kuber ...

  8. 汇编Shellcode的技巧

    汇编Shellcode的技巧 来源  https://www.4hou.com/technology/3893.html 本文参考来源于pentest 我们在上一篇提到要要自定义shellcode,不 ...

  9. JavaScript定位导航滚动2

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  10. c语言指针的简单实例

    c语言的指针的存在使得c语言对硬件的操控,以及灵活性得到了极大的提高. 但是指针的使用存在着很多难点问题. #include<stdlib.h> #include<stdio.h&g ...