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; }

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

最新文章

  1. (转) 从0开始搭建SQL Server AlwaysOn 第三篇(配置AlwaysOn)
  2. Mac下git命令自动补全
  3. 跳转到自己App的“通知”
  4. 让编辑器支持word的复制黏贴,支持截屏的黏贴
  5. Ue4中的框选函数
  6. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q125-Q126)
  7. SQL Server批量数据导出导入Bulk Insert使用
  8. java正则表达式 非捕获组详解
  9. HTML5 编辑 API 之 Range 对象(二)
  10. Charles抓Https的包
  11. Customizing the Test Runner
  12. unigui数据库连接池
  13. Java 使用BigDecimal类处理高精度计算
  14. Azure Pipelines-部署代理问题
  15. 系统垃圾清理利器CCleaner v5.30.6063绿色单文件版(增强版)
  16. Alpha版本项目展示要求(加入模板)
  17. mysql5.7 主从复制的正常切换【转】
  18. TP5常量
  19. Liunx下NFS服务器的搭建与配置
  20. love2d教程33--window模块

热门文章

  1. (转)Yale CAS + .net Client 实现 SSO(6)
  2. linux install mpi4py
  3. Codevs 1039 :数的划分
  4. JPA SQL 的复杂查询createNamedQuery
  5. 支持IE6的树形节结构TreeTable实际应用案例
  6. Turn.js 实现翻书效果的学习与总结
  7. Linux之mount命令详解
  8. Redis 四:存储类型之无序集合
  9. boost安装
  10. [MVC] - 异步调用后台的常用方法。