|
|
@@ -25,6 +25,8 @@ import java.lang.reflect.Method;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.net.URL;
|
|
|
import java.net.URLConnection;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Paths;
|
|
|
import java.text.DecimalFormat;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
@@ -175,6 +177,223 @@ public class NewExcelUtil<T> {
|
|
|
this.downloadPath=uploadPath;
|
|
|
return exportExcel();
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将数据合并按订单合并并导出CXCEL表到指定目录
|
|
|
+ * @param list 导出数据集合
|
|
|
+ * @param sheetName 工作表的名称
|
|
|
+ * @param uploadPath 导出本地地址
|
|
|
+ */
|
|
|
+ public Result OrderMergeAndExportExcel(List<T> list, String sheetName, String uploadPath) {
|
|
|
+ this.init(list, sheetName);
|
|
|
+ this.downloadPath=uploadPath;
|
|
|
+ return OrderMergeAndExportExcel();
|
|
|
+ }
|
|
|
+
|
|
|
+ private Result OrderMergeAndExportExcel() {
|
|
|
+ OutputStream out = null;
|
|
|
+ String filename="";
|
|
|
+ try {
|
|
|
+ // 取出一共有多少个sheet.
|
|
|
+ int sheetNo = (list.size() / sheetSize);
|
|
|
+ for (int index = 0; index <= sheetNo; index++) {
|
|
|
+ createSheet(sheetNo, index);
|
|
|
+ int line=0;
|
|
|
+ // 产生一行
|
|
|
+ Row title = sheet.createRow(line);
|
|
|
+ Cell cell = title.createCell(0);
|
|
|
+ cell.setCellValue(sheetName);
|
|
|
+ CellRangeAddress region = new CellRangeAddress(0, 0, 0, fields.size()-1);
|
|
|
+ sheet.addMergedRegion(region);
|
|
|
+ cell.setCellStyle(styles.get("total"));
|
|
|
+ if (StringUtils.isNotEmpty(screen)){
|
|
|
+ log.debug(screen);
|
|
|
+ Row r = sheet.createRow(++line);
|
|
|
+ Cell c = r.createCell(0);
|
|
|
+ c.setCellValue(screen);
|
|
|
+ CellRangeAddress rg = new CellRangeAddress(line, line, 0, fields.size()-1);
|
|
|
+ sheet.addMergedRegion(rg);
|
|
|
+ c.setCellStyle(styles.get("header"));
|
|
|
+ }
|
|
|
+ Row row = sheet.createRow(++line);
|
|
|
+ // 产生一行
|
|
|
+ int column = 0;
|
|
|
+ // 写入各个字段的列头名称
|
|
|
+ for (Object[] os : fields) {
|
|
|
+ Excel excel = (Excel) os[1];
|
|
|
+ this.createCell(excel, row, column++);
|
|
|
+ }
|
|
|
+ filename = encodingFilename(sheetName);
|
|
|
+ out = Files.newOutputStream(Paths.get(getAbsoluteFile(filename)));
|
|
|
+ fillMergeExcelData(index,++line);
|
|
|
+ //新增合并
|
|
|
+// mergeOrderNo();
|
|
|
+ addStatisticsRow();
|
|
|
+ }
|
|
|
+ wb.write(out);
|
|
|
+ return new Result().data(filename);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new BusinessException("导出Excel失败,请联系网站管理员!");
|
|
|
+ } finally {
|
|
|
+ if (wb != null) {
|
|
|
+ try {
|
|
|
+ wb.close();
|
|
|
+ } catch (IOException e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (out != null) {
|
|
|
+ try {
|
|
|
+ out.close();
|
|
|
+ } catch (IOException e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void fillMergeExcelData(int index, Integer line) throws Exception {
|
|
|
+ int startNo = index * sheetSize;
|
|
|
+ int endNo = Math.min(startNo + sheetSize, list.size());
|
|
|
+ if(line==null)line=2;
|
|
|
+ for (int i = startNo; i < endNo; i++) {
|
|
|
+ int start=i + line - startNo;
|
|
|
+ Row row = sheet.createRow(start);
|
|
|
+ // 得到导出对象.
|
|
|
+ T vo = (T) list.get(i);
|
|
|
+
|
|
|
+ String orderNo = (String) getValue(vo, "orderNo");
|
|
|
+ int x = i+1;
|
|
|
+ int count =0;
|
|
|
+ while (x<endNo){
|
|
|
+ T t1 = list.get(x);
|
|
|
+ String orderNo1 = (String) getValue(t1, "orderNo");
|
|
|
+ if (orderNo.equals(orderNo1)){
|
|
|
+ count++;
|
|
|
+ x++;
|
|
|
+ }else{
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(count>0){
|
|
|
+ int column = 0;
|
|
|
+ for (Object[] os : fields) {
|
|
|
+ Field field = (Field) os[0];
|
|
|
+ Excel excel = (Excel) os[1];
|
|
|
+ // 设置实体类私有属性可访问
|
|
|
+ field.setAccessible(true);
|
|
|
+ if (column<35){
|
|
|
+ this.addOrderMegerCell(excel, row, vo, field, column);
|
|
|
+ CellRangeAddress region = new CellRangeAddress(start, start+count, column, column);
|
|
|
+ sheet.addMergedRegion(region);
|
|
|
+ Row row1 = sheet.getRow(start);
|
|
|
+ Cell cell1 = row1.getCell(column);
|
|
|
+ CellStyle style = styles.get("data4");
|
|
|
+ cell1.setCellStyle(style);
|
|
|
+ column++;
|
|
|
+ }else {
|
|
|
+ this.addOrderMegerCell(excel, row, vo, field, column++);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (int j = 1; j <= count; j++) {
|
|
|
+ T vo3 = (T) list.get(i+j);
|
|
|
+ Row row3 = sheet.createRow(start+j);
|
|
|
+ int column3 = 0;
|
|
|
+ for (Object[] os : fields) {
|
|
|
+ Field field = (Field) os[0];
|
|
|
+ Excel excel = (Excel) os[1];
|
|
|
+ // 设置实体类私有属性可访问
|
|
|
+ field.setAccessible(true);
|
|
|
+ if (column3>34){
|
|
|
+ this.addOrderMegerCell(excel, row3, vo3, field, column3++);
|
|
|
+ }else {
|
|
|
+ column3++;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ i=i+count;
|
|
|
+ }else {
|
|
|
+ int column = 0;
|
|
|
+ for (Object[] os : fields) {
|
|
|
+ Field field = (Field) os[0];
|
|
|
+ Excel excel = (Excel) os[1];
|
|
|
+ // 设置实体类私有属性可访问
|
|
|
+ field.setAccessible(true);
|
|
|
+ Cell cell = this.addCell(excel, row, vo, field, column++);
|
|
|
+ cell.setCellStyle(styles.get("data4"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void mergeOrderNo() throws Exception {
|
|
|
+ int max = list.size();
|
|
|
+ for (int i = 0; i < max; i++) {
|
|
|
+ T t = list.get(i);
|
|
|
+ String orderNo = (String) getValue(t, "orderNo");
|
|
|
+ int count =0;
|
|
|
+ if (i!=max-1){
|
|
|
+ int x = i+1;
|
|
|
+ while (x<max){
|
|
|
+ T t1 = list.get(x);
|
|
|
+ String orderNo1 = (String) getValue(t1, "orderNo");
|
|
|
+ if (orderNo.equals(orderNo1)){
|
|
|
+ count++;
|
|
|
+ x++;
|
|
|
+ }else{
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (count>0&&orderNo.equals("1015315879283802112")){
|
|
|
+ int rowNum = i+2;
|
|
|
+ int rowNum1 = x+1;
|
|
|
+ Row row = sheet.getRow(rowNum);
|
|
|
+ Row row1 = sheet.getRow(rowNum1);
|
|
|
+ System.out.println("orderNo="+orderNo+",count="+count+",rowNum="+rowNum+",rowNum1="+rowNum1);
|
|
|
+ int index=0;
|
|
|
+ for (Object[] e : fields) {
|
|
|
+ Field field = (Field) e[0];
|
|
|
+ Excel attr = (Excel) e[1];
|
|
|
+ // 设置实体类私有属性可访问
|
|
|
+ field.setAccessible(true);
|
|
|
+ Object value = getTargetValue(t, field, attr);
|
|
|
+ Cell cell = row.createCell(index);
|
|
|
+ String dateFormat = attr.dateFormat();
|
|
|
+ String readConverterExp = attr.readConverterExp();
|
|
|
+ String separator = attr.separator();
|
|
|
+ String enumsName = attr.enumsName();
|
|
|
+ String methodName="getValueByCode";
|
|
|
+ if (StringUtils.isNotBlank(enumsName)&& StringUtils.isNotNull(value)) {
|
|
|
+ Class<?> clazz=Class.forName(enumsName);
|
|
|
+ Method method=clazz.getDeclaredMethod(methodName,Integer.class);
|
|
|
+ Object s=method.invoke(clazz,value);
|
|
|
+ setCellVo(s.toString(), attr, cell);
|
|
|
+ }else if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value)) {
|
|
|
+ value=DateUtils.parseDateToStr(dateFormat, (Date) value);
|
|
|
+ } else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value)) {
|
|
|
+ value=convertByExp(toStr(value), readConverterExp, separator);
|
|
|
+ } else if (value instanceof BigDecimal && -1 != attr.scale()) {
|
|
|
+ value=(((BigDecimal) value).setScale(attr.scale(), attr.roundingMode())).stripTrailingZeros().toPlainString();
|
|
|
+ } else{
|
|
|
+ // 设置列类型
|
|
|
+ setCellVo(value, attr, cell);
|
|
|
+ }
|
|
|
+ if (value==null)value="";
|
|
|
+ cell.setCellValue(value.toString());
|
|
|
+ CellRangeAddress region = new CellRangeAddress(rowNum, rowNum1, index, index);
|
|
|
+ sheet.addMergedRegion(region);
|
|
|
+ index++;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+// CellRangeAddress region = new CellRangeAddress(4, 5, 0, fields.size()-1);
|
|
|
+// sheet.addMergedRegion(region);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 直接导出CXCEL表
|
|
|
* @param list 导出数据集合
|
|
|
@@ -423,6 +642,22 @@ public class NewExcelUtil<T> {
|
|
|
style.setAlignment(HorizontalAlignment.RIGHT);
|
|
|
styles.put("data3", style);
|
|
|
|
|
|
+
|
|
|
+ style = wb.createCellStyle();
|
|
|
+ style.cloneStyleFrom(styles.get("data"));
|
|
|
+ style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+ style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ style.setBorderRight(BorderStyle.THIN);
|
|
|
+ style.setRightBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
|
|
|
+ style.setBorderLeft(BorderStyle.THIN);
|
|
|
+ style.setLeftBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
|
|
|
+ style.setBorderTop(BorderStyle.THIN);
|
|
|
+ style.setTopBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
|
|
|
+ style.setBorderBottom(BorderStyle.THIN);
|
|
|
+ style.setBottomBorderColor(IndexedColors.GREY_25_PERCENT.getIndex());
|
|
|
+ styles.put("data4", style);
|
|
|
+
|
|
|
+
|
|
|
return styles;
|
|
|
}
|
|
|
|
|
|
@@ -551,7 +786,49 @@ public class NewExcelUtil<T> {
|
|
|
cell = row.createCell(column);
|
|
|
int align = attr.align().value();
|
|
|
cell.setCellStyle(styles.get("data" + (align >= 1 && align <= 3 ? align : "")));
|
|
|
+ // 用于读取对象中的属性
|
|
|
+ Object value = getTargetValue(vo, field, attr);
|
|
|
+ String dateFormat = attr.dateFormat();
|
|
|
+ String readConverterExp = attr.readConverterExp();
|
|
|
+ String separator = attr.separator();
|
|
|
+ String enumsName = attr.enumsName();
|
|
|
+ String methodName="getValueByCode";
|
|
|
+ if (StringUtils.isNotBlank(enumsName)&& StringUtils.isNotNull(value)) {
|
|
|
+ Class<?> clazz=Class.forName(enumsName);
|
|
|
+ Object[] os=clazz.getEnumConstants();
|
|
|
+ Method method=clazz.getDeclaredMethod(methodName,Integer.class);
|
|
|
+ Object s=method.invoke(clazz,value);
|
|
|
+ setCellVo(s.toString(), attr, cell);
|
|
|
+ }else if (StringUtils.isNotEmpty(dateFormat) && StringUtils.isNotNull(value)) {
|
|
|
+ cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value));
|
|
|
+ } else if (StringUtils.isNotEmpty(readConverterExp) && StringUtils.isNotNull(value)) {
|
|
|
+ cell.setCellValue(convertByExp(toStr(value), readConverterExp, separator));
|
|
|
+ } else if (value instanceof BigDecimal && -1 != attr.scale()) {
|
|
|
+ cell.setCellValue((((BigDecimal) value).setScale(attr.scale(), attr.roundingMode())).stripTrailingZeros().toPlainString());
|
|
|
+ } else{
|
|
|
+ // 设置列类型
|
|
|
+ setCellVo(value, attr, cell);
|
|
|
+ }
|
|
|
+ addStatisticsData(column, toStr(value), attr);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("导出Excel失败{}", e);
|
|
|
+ }
|
|
|
+ return cell;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
+ public Cell addOrderMegerCell(Excel attr, Row row, T vo, Field field, int column) {
|
|
|
+ Cell cell = null;
|
|
|
+ try {
|
|
|
+ // 设置行高
|
|
|
+ row.setHeight(maxHeight);
|
|
|
+ // 根据Excel中设置情况决定是否导出,有些情况需要保持为空,希望用户填写这一列.
|
|
|
+ if (attr.isExport()) {
|
|
|
+ // 创建cell
|
|
|
+ cell = row.createCell(column);
|
|
|
+ int align = attr.align().value();
|
|
|
+ cell.setCellStyle(styles.get("data4"));
|
|
|
// 用于读取对象中的属性
|
|
|
Object value = getTargetValue(vo, field, attr);
|
|
|
String dateFormat = attr.dateFormat();
|