|
@@ -3,17 +3,30 @@ package com.minpay.reportManage.action;
|
|
|
import java.io.IOException;
|
|
|
import java.io.OutputStream;
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
+import java.net.URLEncoder;
|
|
|
import java.text.ParseException;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Collection;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
|
import org.apache.commons.fileupload.FileItem;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFCell;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFCellStyle;
|
|
|
+import org.apache.poi.hssf.usermodel.HSSFPalette;
|
|
|
+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.hssf.util.HSSFColor;
|
|
|
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
|
|
+import org.apache.poi.ss.usermodel.CellStyle;
|
|
|
+import org.apache.poi.ss.usermodel.FillPatternType;
|
|
|
+import org.apache.poi.ss.usermodel.Font;
|
|
|
+import org.apache.poi.ss.usermodel.IndexedColors;
|
|
|
|
|
|
import com.minpay.common.bean.User;
|
|
|
import com.minpay.common.constant.Constant;
|
|
@@ -807,7 +820,7 @@ public class ReportManageAction implements IMINAction {
|
|
|
endTime = DateUtil.dateAddDay(endTime, -1);
|
|
|
}
|
|
|
out = response.getOutputStream();
|
|
|
- HSSFWorkbook wb = ReportExcelUtil.export(response, reportInf.getFileName(), excelHeader, resList);
|
|
|
+ HSSFWorkbook wb = export(response, reportInf.getFileName(), excelHeader, resList);
|
|
|
if(wb != null){
|
|
|
wb.write(out);
|
|
|
}
|
|
@@ -853,4 +866,124 @@ public class ReportManageAction implements IMINAction {
|
|
|
|
|
|
return res;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ public static <T> HSSFWorkbook export(HttpServletResponse response, String fileName, String[] excelHeader,
|
|
|
+ Collection<T> dataList) throws Exception {
|
|
|
+ // 设置请求
|
|
|
+ response.setContentType("application/application/vnd.ms-excel");
|
|
|
+ response.setHeader("Content-disposition",
|
|
|
+ "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
|
|
|
+ // 创建一个Workbook,对应一个Excel文件
|
|
|
+ HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
+ // 设置标题样式
|
|
|
+ HSSFCellStyle titleStyle = wb.createCellStyle();
|
|
|
+ // 设置字体样式
|
|
|
+ Font titleFont = wb.createFont();
|
|
|
+ titleFont.setFontHeightInPoints((short) 12); // 字体高度
|
|
|
+ titleFont.setFontName("黑体"); // 字体样式
|
|
|
+ titleStyle.setFont(titleFont);
|
|
|
+ titleStyle.setAlignment(CellStyle.ALIGN_CENTER);
|
|
|
+ // 在Workbook中添加一个sheet,对应Excel文件中的sheet
|
|
|
+ HSSFSheet sheet = wb.createSheet(fileName);
|
|
|
+ // 标题数组
|
|
|
+ String[] titleArray = new String[excelHeader.length];
|
|
|
+ // 字段名数组
|
|
|
+ String[] fieldArray = new String[excelHeader.length];
|
|
|
+ for (int i = 0; i < excelHeader.length; i++) {
|
|
|
+ String[] tempArray = excelHeader[i].split("#");// 临时数组 分割#
|
|
|
+ titleArray[i] = tempArray[0];
|
|
|
+ fieldArray[i] = tempArray[1];
|
|
|
+ }
|
|
|
+ // 在sheet中添加标题行
|
|
|
+ HSSFRow row = sheet.createRow((int) 0);// 行数从0开始
|
|
|
+ HSSFCell sequenceCell = row.createCell(0);// cell列 从0开始 第一列添加序号
|
|
|
+ sequenceCell.setCellValue("序号");
|
|
|
+ sequenceCell.setCellStyle(titleStyle);
|
|
|
+ sheet.autoSizeColumn(0);// 自动设置宽度
|
|
|
+ // 为标题行赋值
|
|
|
+ for (int i = 0; i < titleArray.length; i++) {
|
|
|
+ HSSFCell titleCell = row.createCell(i + 1);// 0号位被序号占用,所以需+1
|
|
|
+ titleCell.setCellValue(titleArray[i]);
|
|
|
+ titleCell.setCellStyle(titleStyle);
|
|
|
+ sheet.autoSizeColumn(i + 1);// 0号位被序号占用,所以需+1
|
|
|
+ sheet.setColumnWidth(i+1,titleArray[i].getBytes().length*256+500);
|
|
|
+ }
|
|
|
+ // 数据样式 因为标题和数据样式不同 需要分开设置 不然会覆盖
|
|
|
+ HSSFCellStyle dataStyle = wb.createCellStyle();
|
|
|
+ // 设置数据字体
|
|
|
+ Font dataFont = wb.createFont();
|
|
|
+ dataFont.setFontHeightInPoints((short) 10); // 字体高度
|
|
|
+ dataFont.setFontName("宋体"); // 字体
|
|
|
+ dataStyle.setFont(dataFont);
|
|
|
+ dataStyle.setAlignment(CellStyle.ALIGN_CENTER);
|
|
|
+
|
|
|
+ // 自定义颜色
|
|
|
+ HSSFPalette palette = wb.getCustomPalette();
|
|
|
+ palette.setColorAtIndex((short)9, (byte) (0xff & 240), (byte) (0xff & 165), (byte) (0xff & 172));
|
|
|
+ palette.setColorAtIndex((short)10, (byte) (0xff & 255), (byte) (0xff & 192), (byte) (0xff & 0));
|
|
|
+
|
|
|
+ HSSFCellStyle dataStyle2 = wb.createCellStyle();
|
|
|
+ dataStyle2.setFont(dataFont);
|
|
|
+ dataStyle2.setFillPattern(CellStyle.SOLID_FOREGROUND);
|
|
|
+ dataStyle2.setFillForegroundColor((short)9);
|
|
|
+ dataStyle2.setAlignment(CellStyle.ALIGN_CENTER);
|
|
|
+
|
|
|
+ HSSFCellStyle dataStyle3 = wb.createCellStyle();
|
|
|
+ dataStyle3.setFont(dataFont);
|
|
|
+ dataStyle3.setFillPattern(CellStyle.SOLID_FOREGROUND);
|
|
|
+ dataStyle3.setFillForegroundColor((short)10);
|
|
|
+ dataStyle3.setAlignment(CellStyle.ALIGN_CENTER);
|
|
|
+
|
|
|
+ // 遍历集合数据,产生数据行
|
|
|
+ Iterator<T> it = dataList.iterator();
|
|
|
+ int index = 0;
|
|
|
+ while (it.hasNext()) {
|
|
|
+ index++;
|
|
|
+ row = sheet.createRow(index);
|
|
|
+ // 为序号赋值
|
|
|
+ HSSFCell sequenceCellValue = row.createCell(0);// 序号值永远是第0列
|
|
|
+ sequenceCellValue.setCellValue(index);
|
|
|
+ sequenceCellValue.setCellStyle(dataStyle);
|
|
|
+// sheet.autoSizeColumn(0);
|
|
|
+ T t = (T) it.next();
|
|
|
+ Map<String, Object> map = (Map<String, Object> )t;
|
|
|
+ HSSFCell dataCell = null;
|
|
|
+ for (int i = 0; i < fieldArray.length; i++) {
|
|
|
+ dataCell = row.createCell(i + 1);
|
|
|
+ String fieldName = fieldArray[i];
|
|
|
+ String value = CommonUtil.objToString(map.get(fieldName));
|
|
|
+ // 每日时长列
|
|
|
+ if (i >= 15) {
|
|
|
+ if (CommonUtil.isEmpty(value)) {
|
|
|
+ dataCell.setCellValue("");
|
|
|
+ dataCell.setCellStyle(dataStyle);
|
|
|
+ } else {
|
|
|
+ dataCell.setCellValue(value);// 为当前列赋值
|
|
|
+ dataCell.setCellStyle(dataStyle2);
|
|
|
+ }
|
|
|
+ //用户编号列
|
|
|
+ } else if (i == 6) {
|
|
|
+ // 低电压时长
|
|
|
+ String sj = String.valueOf(map.get(fieldArray[i + 1]));
|
|
|
+ if (CommonUtil.compare(sj, "48") != -1) {
|
|
|
+ dataCell.setCellValue(value);// 为当前列赋值
|
|
|
+ dataCell.setCellStyle(dataStyle3);
|
|
|
+ } else {
|
|
|
+ dataCell.setCellValue(value);// 为当前列赋值
|
|
|
+ dataCell.setCellStyle(dataStyle);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ dataCell.setCellStyle(dataStyle);
|
|
|
+ if (CommonUtil.isEmpty(value)) {
|
|
|
+ dataCell.setCellValue(""); // 为当前列赋值
|
|
|
+ } else {
|
|
|
+ dataCell.setCellValue(CommonUtil.objToString(value));// 为当前列赋值
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sheet.setColumnWidth(fieldArray.length, 20 * 256);
|
|
|
+ return wb;
|
|
|
+ }
|
|
|
}
|