一.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. HDU 3368 Reversi (暴力,DFS)

    题意:给定一个8*8的棋盘,然后要懂黑白棋,现在是黑棋走了,问你放一个黑子,最多能翻白子多少个. 析:我是这么想的,反正才是8*8的棋盘,那么就暴吧,反正不会超时,把每一个格能暴力的都暴力,无非是上, ...

  2. 第02章-装配Bean

    1. Spring配置的可选方案 在XML中进行显式配置: 在Java中进行显式配置: 隐式的bean发现机制和自动装配. 2. 自动化装配bean Spring从两个角度来实现自动化装配: 组件扫描 ...

  3. (转)不定义JQuery插件,不要说会JQuery

    原文地址:http://www.cnblogs.com/xcj26/p/3345556.html 一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写$("#" ...

  4. POJ2442 Sequence(堆的骚操作)

    Description Given m sequences, each contains n non-negative integer. Now we may select one number fr ...

  5. C# 的 Task、Thread、ThreadPool 之间有什么异同?

    Thread就是Thread,需要自己调度,适合长跑型的操作. ThreadPool是Thread基础上的一个线程池,目的是减少频繁创建线程的开销.线程很贵,要开新的stack,要增加CPU上下文切换 ...

  6. DDD模式

    http://www.cnblogs.com/landeanfen/p/4816706.html https://www.cnblogs.com/malaoko/p/8732552.html

  7. asp.net站点阻止某个文件夹或者文件被浏览器访问

    一个站点根目录下面有一个Config文件夹,这个文件夹里面都是一些json格式的txt文本,文本是一种静态资源,如果知道这个文本的地址,就可以在浏览器中输入地址打开这个文本,别人就可以看到站点的配置, ...

  8. Window 7 Professional 多语言设置

    1. 正常情况下,WINDOW系统只提供企业和旗舰版的语言切换的界面设置,其他版本没有. 2. 首先下载语言包,然后解压待用. 3. 以管理员身份运行命令窗口,如下输入: 4. 上面完成后,下载 ht ...

  9. rsync 备份服务搭建(完成)

    rsync服务守护进程 服务器端配置过程: 1. 检查rsync是否安装: rpm -qa rsync 2.添加rsync服务的用户,管理本地目录 useradd-s /sbin/nologin -M ...

  10. cinder create volume的流程-scheduler调度

    创建 Volume 时,cinder-scheduler 会基于容量.Volume Type 等条件选择出最合适的存储节点,然后让其创建 Volume. 1.cinder-scheduler配置相关项 ...