String _currentPage = request.getParameter("currentPage");
Integer currentPage = 0;
if(_currentPage == null){
currentPage = 1;
}else{
currentPage = Integer.valueOf(_currentPage);
}
log.info("导出excel页码: " + currentPage); /**map为空时查询全部的配件信息*/
Map map = new HashMap();
map.put("flag",10);
map.put("unionFlag",0); //0表示单件商品,而非组合商品。
Integer totalSize = partsMgrService.totalPartsInfo(map);
/**封装pageUtil对象*/
PageUtil pageUtil = new PageUtil(totalSize,Integer.valueOf(currentPage)); //读取第一页,与partsmgr.jsp查询保持一致。
map.put("pageUtil",pageUtil);
//map.put("flag",10);
list = partsMgrService.listPartsInfo(map); try {
HSSFWorkbook wb = partsMgrService.export(list); //调用service方法~!
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=partsList.xls");
OutputStream ouputStream = response.getOutputStream();
wb.write(ouputStream);
ouputStream.flush();
ouputStream.close(); } catch (Exception e) {
e.printStackTrace();
log.error("配件导出excel错误:", e);
}
@Override
public HSSFWorkbook export(List<PartsInfoDTO> list) throws Exception{
String[] excelHeader = {"ID", "编码", "名称","库存","成本价","市场价","销售价","重量","上架","所属品牌","所属分类","所属供应商","图片"}; HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("养车之家后台配件列表");
// sheet.setDefaultRowHeight((short)300);
// sheet.setDefaultColumnWidth((short)3000);
sheet.setColumnWidth(0, 3766); //第一个参数代表列id(从0开始),第2个参数代表宽度值 参考 :"2012-08-10"的宽度为2500 // sheet.setColumnWidth(0, 1000);
/**没有效果 ~!!! 140123 huangjing
* 是设置了sheet.autoSizeColumn(i); 的原因
* */
sheet.setColumnWidth(1, 3766);
sheet.setColumnWidth(2, 3766);
sheet.setColumnWidth(3, 3766);
sheet.setColumnWidth(4, 3766);
sheet.setColumnWidth(5, 3766);
sheet.setColumnWidth(6, 3766);
sheet.setColumnWidth(7, 3766);
sheet.setColumnWidth(8, 3766);
sheet.setColumnWidth(9, 3766);
sheet.setColumnWidth(10, 3766);
sheet.setColumnWidth(11, 3766);
sheet.setColumnWidth(12, 3766); HSSFCellStyle style = wb.createCellStyle();
style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
style.setWrapText(true); //设置是否能够换行,能够换行为true HSSFRow row = sheet.createRow((int) 0);
for (int i = 0; i < excelHeader.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellValue(excelHeader[i]);
cell.setCellStyle(style);
// sheet.autoSizeColumn(i);
} for (int i = 0; i < list.size(); i++) {
row = sheet.createRow(i + 1);
PartsInfoDTO dto = list.get(i);
HSSFCell cell = null;
cell = row.createCell(0);
cell.setCellValue(dto.getId());
cell.setCellStyle(style); cell = row.createCell(1);
if(dto.getPartsCode() != null){
cell.setCellValue(dto.getPartsCode());
}else{
cell.setCellValue("-");
}
cell.setCellStyle(style); cell = row.createCell(2);
if(dto.getPartsName() != null){
cell.setCellValue(dto.getPartsName());
}else{
cell.setCellValue("-");
}
cell.setCellStyle(style); cell = row.createCell(3);
if(dto.getStoreNum() != null){
cell.setCellValue(dto.getStoreNum());
}else{
cell.setCellValue("-");
}
cell.setCellStyle(style); cell = row.createCell(4);
if(dto.getCostPrice() != null){
cell.setCellValue(dto.getCostPrice());
}else{
cell.setCellValue("-");
}
cell.setCellStyle(style); cell = row.createCell(5);
if(dto.getMarketPrice() != null){
cell.setCellValue(dto.getMarketPrice());
}else{
cell.setCellValue("-");
}
cell.setCellStyle(style); cell = row.createCell(6);
if(dto.getRetailPrice() != null){
cell.setCellValue(dto.getRetailPrice());
}else{
cell.setCellValue("-");
}
cell.setCellStyle(style); cell = row.createCell(7);
if(dto.getWeight() != null){
cell.setCellValue(dto.getWeight());
}else{
cell.setCellValue("-");
}
cell.setCellStyle(style); cell = row.createCell(8);
if(dto.getUpdown() == 0){
cell.setCellValue("否");
}else{
cell.setCellValue("是");
}
cell.setCellStyle(style); cell = row.createCell(9);
if(dto.getPartsBrandDto().getBrandName() != null){
cell.setCellValue(dto.getPartsBrandDto().getBrandName());
}else{
cell.setCellValue("-");
}
cell.setCellStyle(style); String typeName = "";
if(dto.getPartsType2Dto().getTypeName() != null){
typeName = dto.getPartsType2Dto().getTypeName();
}else{
typeName = "-";
}
if(dto.getPartsTypeDto().getTypeName() != null){
typeName += "-" + dto.getPartsTypeDto().getTypeName();
}else{
typeName += "-";
}
cell = row.createCell(10);
cell.setCellValue(typeName);
cell.setCellStyle(style); cell = row.createCell(11);
if(dto.getSupplyInfoDto().getSupplyName() != null){
cell.setCellValue(dto.getSupplyInfoDto().getSupplyName());
}else{
cell.setCellValue("-");
}
cell.setCellStyle(style); cell = row.createCell(12);
if(dto.getPic().equals("none.jpg")){
cell.setCellValue("无");
}else{
cell.setCellValue("有");
}
cell.setCellStyle(style); }
return wb; }

实现从数据库中查询数据按当前分页来导出~!!!
导出效果图:

java poi导出EXCEL xls文件代码的更多相关文章

  1. java poi导入EXCEL xls文件代码

    /** * */ package com.bn.car.common.report.excel; import java.io.FileInputStream; import java.io.IOEx ...

  2. 重构:以Java POI 导出EXCEL为例

    重构 开头先抛出几个问题吧,这几个问题也是<重构:改善既有代码的设计>这本书第2章的问题. 什么是重构? 为什么要重构? 什么时候要重构? 接下来就从这几个问题出发,通过这几个问题来系统的 ...

  3. Java POI 导出EXCEL经典实现 Java导出Excel

    转自http://blog.csdn.net/evangel_z/article/details/7332535 在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者 ...

  4. 重构:以Java POI 导出EXCEL为例2

    前言 上一篇博文已经将一些对象抽象成成员变量以及将一些代码块提炼成函数.这一节将会继续重构原有的代码,将一些函数抽象成类,增加成员变量,将传入的参数合成类等等. 上一篇博文地址:http://www. ...

  5. java POI导出Excel文件数据库的数据

    在web开发中,有一个经典的功能,就是数据的导入导出.特别是数据的导出,在生产管理或者财务系统中用的非常普遍,因为这些系统经常要做一些报表打印的工作.这里我简单实现导出Excel文件. POI jar ...

  6. Java POI导出Excel不弹框选择下载路径(下载文件不选择下载路径,默认) Chrome

    在Chrome浏览器中,Java导出Excel文件时,浏览器弹出提示框,需要选择下载路径 在Chrome中的高级设置中,把“下载前询问每个文件的保存位置”去掉就解决了 DEEPLOVE(LC)

  7. java:POI导出excel

    POI是一个开源项目,专用于java平台上操作MS OFFICE,企业应用开发中可用它方便导出Excel. 下面是使用示例: 1.maven中先添加依赖项 <dependency> < ...

  8. POI导出Excel(xls、xlsx均可以,也支持图片)——(三)

    Jar包

  9. spring boot:使用poi导出excel电子表格文件(spring boot 2.3.1)

    一,什么是poi? 1,poi poi是用来兼容微软文档格式的java api, 它是apache的顶级项目之一, 也是我们在生产环境中导出excel时使用最多的库 2,poi官方网站: http:/ ...

随机推荐

  1. javascript之正则表达式总结

    了解RegExp类型: ECMAScript通过RegExp类型来支持正则表达式. var expression=/pattern/flags; 正则表达式的模式(pattern)部分: 可以是任何简 ...

  2. git与github

    Git-版本控制,多人协作,分布式: Github-开源项目,远程仓库: 我在github上建立了一个仓库 https://github.com/abcd/2016ife: 现在我要把它下载到本地: ...

  3. JavaScript学习笔记 -- 带参数arguments的函数的用法

    JavaScript函数有带参数与不带参数两种形式,不带参数情况如下: function myFunction() { alert('HelloWorld!') } 在这种类型的函数中,输出值是确定的 ...

  4. Java获取操作系统信息

    今天在看jdk的demo时候发现java获取系统操作系统的一些信息,例如内存使用情况,于是自己也想研究研究! 百度一番,发现有2种方法! 1.sun自带的API 2.第三方jar(Sigar)   先 ...

  5. Microsoft Office Access 不能在追加查询中追加所有记录

    有客户反映,我们的软件ACCESS数据库版本在使用时会出现"Microsoft Office Access 不能在追加查询中追加所有记录"的错误.使用客户的数据库调试软件发现错误出 ...

  6. Python's Exception 层级结构

    BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exception +-- StopIteration ...

  7. 火狐谷歌浏览器Json查看插件

    1.搜: Firefox的JSON插件 参考: Chrome/FireFox浏览器下处理JSON的插件_Bruce_新浪博客 JSONView :: Firefox 附加组件 但是后来去发现没用: 打 ...

  8. 12.iscsi-target

    server: 环境:rhel7.2  软件包:targetcli-2.1.fb41-3.el7.noarch,selinux-policy-targeted-3.13.1-60.el7.noarch ...

  9. 使用C++读取UTF8及GBK系列的文本方法及原理

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4374404.html 1.读取UTF-8编码文本原理 首先了解UTF-8的编码方式,UTF- ...

  10. ubuntu下安装 Source insight

    习惯了在source insight下编辑阅读源码,在linux下用vi总是用不好 ,还是在ubuntu上用回熟悉的source insight. 在ubuntu中,安装windows程序用wine, ...