展开全部
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.plocc.framework.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class FileUtils extends org.apache.commons.io.FileUtils {
public FileUtils() {
}
public static String exportExcel(Map dataMap) throws Exception {
HSSFWorkbook workbook = null;
File fileOut;
if (null !e5a48de588b63231313335323631343130323136353331333365653766= dataMap.get("tempPath")) {
try {
fileOut = new File((String)dataMap.get("tempPath"));
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(fileOut));
workbook = new HSSFWorkbook(fs);
} catch (Exception var7) {
var7.printStackTrace();
throw new Exception("exportExcel---Excel导出出错:" + var7.getMessage());
}
} else {
workbook = new HSSFWorkbook();
}
try {
createSheet(workbook, dataMap);
} catch (Exception var6) {
var6.printStackTrace();
throw new Exception("exportExcel---Excel导出出错:" + var6.getMessage());
}
fileOut = null;
String filePath = (String)dataMap.get("filePath");
if (StringUtils.isEmpty(filePath)) {
throw new Exception("exportExcel---Excel导出出错:无效文件路径!");
} else {
try {
FileOutputStream fileOut = new FileOutputStream(filePath);
workbook.write(fileOut);
fileOut.flush();
fileOut.close();
return filePath;
} catch (Exception var5) {
fileOut.close();
var5.printStackTrace();
throw new Exception("exportExcel---Excel导出出错:" + var5.getMessage());
}
}
}
private static void createSheet(HSSFWorkbook workbook, Map dataMap) throws Exception {
HSSFSheet sheet = null;
if (null != dataMap.get("tempPath")) {
sheet = workbook.getSheetAt(0);
} else {
String sheetName = "sheet1";
if (null != dataMap.get("sheetName")) {
sheetName = (String)dataMap.get("sheetName");
}
sheet = workbook.createSheet(sheetName);
}
HSSFFont font = workbook.createFont();
font.setFontHeightInPoints((short)12);
HSSFCellStyle styleHeader = workbook.createCellStyle();
styleHeader.setAlignment((short)2);
styleHeader.setFont(font);
styleHeader.setFillForegroundColor((short)22);
styleHeader.setFillPattern((short)1);
styleHeader.setBorderTop((short)1);
styleHeader.setBorderBottom((short)1);
styleHeader.setBorderLeft((short)1);
styleHeader.setBorderRight((short)1);
HSSFCellStyle styleBody = workbook.createCellStyle();
styleBody.setBorderTop((short)1);
styleBody.setBorderBottom((short)1);
styleBody.setBorderLeft((short)1);
styleBody.setBorderRight((short)1);
styleBody.setWrapText(true);
HSSFRow row = null;
HSSFCell cell = null;
int titleRowNum = 0;
int startRowNum = 0;
if (null != dataMap.get("startRowNum")) {
startRowNum = ((Integer)dataMap.get("startRowNum")).intValue() - 1;
}
List> dataTitle = (List)dataMap.get("dataTitle");
int i;
if (dataTitle != null && dataTitle.size() > 0) {
titleRowNum = dataTitle.size();
for(int i = 0; i
row = sheet.createRow(i + startRowNum);
for(i = 0; i
cell = row.createCell(i);
cell.setCellValue((String)((List)dataTitle.get(i)).get(i));
cell.setCellStyle(styleHeader);
sheet.setColumnWidth(i, 6400);
}
}
}
List> dataBody = (List)dataMap.get("dataBody");
if (dataBody != null && dataBody.size() > 0) {
for(i = 0; i
row = sheet.createRow(i + titleRowNum + startRowNum);
if (dataBody.get(i) != null && ((List)dataBody.get(i)).size() > 0) {
for(int j = 0; j
cell = row.createCell(j);
cell.setCellValue((String)((List)dataBody.get(i)).get(j));
cell.setCellStyle(styleBody);
}
}
}
}
}
}
#------------------------------------
@SuppressWarnings("unchecked")
@RequestMapping("/exportDataFile")
public void exportDataFile(HttpServletRequest request, HttpServletResponse response, IbPlan ibPlan)
throws Exception {
Map mallMap = bsMallService.getAllBsMallMap(null);
// 查询数据
Paginator paginator = new Paginator();
paginator.setItemsPerPage(Constants.DOWNLOAD_LIMIT);
paginator.setParams(ibPlan);
paginator = ibPlanService.getListBySelectivePage(paginator);
List ibPlanList = (List) paginator.getResults();
// 报表数据
Map dataMap = new HashMap();
// 文件路径
dataMap.put("filePath", servletContext.getRealPath("/") + "merchantInfo" + "-"
+ DateUtil.formatDate(new Date(), "yyyyMMdd") + ".xls");
// 模板路径
// dataMap.put("tempPath", servletContext.getRealPath("/") +
// "excelTemplate/InfoTemplate.xls");
// dataMap.put("startRowNum", 3);
// 表单名
dataMap.put("sheetName", "招商信息");
// 标题部分
List> dataTitle = new ArrayList>();
List title = new ArrayList();
title.add("品牌");
title.add("项目");
title.add("当前进度");
title.add("商务网批");
title.add("招商人员");
title.add("更新时间");
title.add("最新留言/备注");
dataTitle.add(title);
dataMap.put("dataTitle", dataTitle);
// 数据部分
List> dataBody = new ArrayList>();
List body = null;
for (IbPlan entity : ibPlanList) {
body = new ArrayList();
// 品牌
body.add(entity.getBrandName());
// 项目
if (null != mallMap.get(entity.getMallId())) {
body.add(mallMap.get(entity.getMallId()).getMallName());
} else {
body.add("");
}
// 当前进度
body.add(MerchantProgressEnum.getMap().get(entity.getStatus()));
// 商务网批
if (null != entity.getProgressList() && entity.getProgressList().size() >= 3) {
body.add(entity.getProgressList().get(2).getNetcommentNo());
} else {
body.add("");
}
// 招商人员
body.add(entity.getManagerName());
// 更新时间
body.add(DateUtils.format(entity.getUpdatedDate(), "yyyy-MM-dd"));
// 最新留言/备注
body.add(entity.getContent());
dataBody.add(body);
}
dataMap.put("dataBody", dataBody);
// 报表导出
try {
String filePath = FileUtils.exportExcel(dataMap);
// 获取文件
File file = new File(filePath);
// 取得文件名
String filename = file.getName();
// 以流的形式下载文件
InputStream fis = new BufferedInputStream(new FileInputStream(filePath));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.addHeader("Content-Length", "" + file.length());
response.setContentType("application/octet-stream;charset=UTF-8");
OutputStream output = response.getOutputStream();
output.write(buffer);
output.flush();
} catch (Exception e) {
e.printStackTrace();
logger.error("getDataFile---Excel导出出错-----" + e.getMessage());
}
}