|
|
|
@ -2,18 +2,11 @@ package com.op.common.core.utils.poi;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
|
|
import com.op.common.core.domain.ExcelCol;
|
|
|
|
|
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
|
|
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
|
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
@ -35,22 +28,45 @@ public class ExcelMapUtil {
|
|
|
|
|
}else{
|
|
|
|
|
sheet = workbook.createSheet(sheetName);
|
|
|
|
|
}
|
|
|
|
|
// //创建主标题行(第一行)
|
|
|
|
|
// Row sheetTitleRow = sheet.createRow(0);
|
|
|
|
|
// Cell titleCell = sheetTitleRow.createCell(0);//创建第一行第一个单元格
|
|
|
|
|
// titleCell.setCellValue(title);//传值
|
|
|
|
|
// titleCell.setCellStyle(getHeaderFont(sheet.getWorkbook()));//设置样式
|
|
|
|
|
// //主标题行合并单元格
|
|
|
|
|
// CellRangeAddress cellAddresses = new CellRangeAddress(0, 0, 0, colSize - 1);
|
|
|
|
|
// sheet.addMergedRegion(cellAddresses);
|
|
|
|
|
//创建表头行(第二行)
|
|
|
|
|
Row sheetHeadRow = sheet.createRow(0);//1
|
|
|
|
|
//遍历表头名称,创建表头单元格
|
|
|
|
|
for(int i = 0 ; i < colSize ; i++){
|
|
|
|
|
sheet.setColumnWidth(i,(excelCol.get(i).getWidth())*256);//宽度单位是字符的256分之一
|
|
|
|
|
Cell headCell = sheetHeadRow.createCell(i);
|
|
|
|
|
headCell.setCellValue(excelCol.get(i).getTitle());//传值
|
|
|
|
|
headCell.setCellStyle(getHeaderFont(sheet.getWorkbook()));//设置样式
|
|
|
|
|
if(title != null){//定量分析
|
|
|
|
|
String[] title1s = title.split(",");
|
|
|
|
|
int rangeVal = colSize/title1s.length;
|
|
|
|
|
//创建主标题行(第一行)
|
|
|
|
|
Row sheetTitleRow = sheet.createRow(0);
|
|
|
|
|
|
|
|
|
|
Cell titleCell0 = sheetTitleRow.createCell(0);//创建第一行第一个单元格
|
|
|
|
|
titleCell0.setCellValue("");//传值
|
|
|
|
|
titleCell0.setCellStyle(getHeaderFont(sheet.getWorkbook()));//设置样式
|
|
|
|
|
|
|
|
|
|
//遍历表头名称,创建表头单元格
|
|
|
|
|
for (int i = 0; i < title1s.length; i++) {
|
|
|
|
|
Cell titleCell = sheetTitleRow.createCell(i*rangeVal+1);//创建第一行第一个单元格
|
|
|
|
|
titleCell.setCellValue(title1s[i]);//传值
|
|
|
|
|
titleCell.setCellStyle(getHeaderFont(sheet.getWorkbook()));//设置样式
|
|
|
|
|
//主标题行合并单元格
|
|
|
|
|
CellRangeAddress cellAddresses = new CellRangeAddress(0, 0, i*rangeVal+1, (i+1)*rangeVal);
|
|
|
|
|
sheet.addMergedRegion(cellAddresses);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//创建表头行(第二行)
|
|
|
|
|
Row sheetHeadRow = sheet.createRow(1);//1
|
|
|
|
|
//遍历表头名称,创建表头单元格
|
|
|
|
|
for (int i = 0; i < colSize; i++) {
|
|
|
|
|
sheet.setColumnWidth(i, (excelCol.get(i).getWidth()) * 256);//宽度单位是字符的256分之一
|
|
|
|
|
Cell headCell = sheetHeadRow.createCell(i);
|
|
|
|
|
headCell.setCellValue(excelCol.get(i).getTitle());//传值
|
|
|
|
|
headCell.setCellStyle(getHeaderFont(sheet.getWorkbook()));//设置样式
|
|
|
|
|
}
|
|
|
|
|
}else {
|
|
|
|
|
//创建表头行(第二行)
|
|
|
|
|
Row sheetHeadRow = sheet.createRow(0);//1
|
|
|
|
|
//遍历表头名称,创建表头单元格
|
|
|
|
|
for (int i = 0; i < colSize; i++) {
|
|
|
|
|
sheet.setColumnWidth(i, (excelCol.get(i).getWidth()) * 256);//宽度单位是字符的256分之一
|
|
|
|
|
Cell headCell = sheetHeadRow.createCell(i);
|
|
|
|
|
headCell.setCellValue(excelCol.get(i).getTitle());//传值
|
|
|
|
|
headCell.setCellStyle(getHeaderFont(sheet.getWorkbook()));//设置样式
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//将data中的值填充到excel
|
|
|
|
@ -65,12 +81,15 @@ public class ExcelMapUtil {
|
|
|
|
|
for (int i = 0 ; i < colSize ; i++ ){
|
|
|
|
|
Cell dataCell = dataRow.createCell(i);
|
|
|
|
|
dataCell.setCellStyle(getDataFont(workbook));
|
|
|
|
|
if(i>=2){
|
|
|
|
|
dataCell.setCellValue(getValueNum(jsonObject.get(excelCol.get(i).getField())));
|
|
|
|
|
}else{
|
|
|
|
|
if(title!=null){//定量分析
|
|
|
|
|
dataCell.setCellValue(getValue(jsonObject.get(excelCol.get(i).getField())));
|
|
|
|
|
}else{
|
|
|
|
|
if(i>=2){
|
|
|
|
|
dataCell.setCellValue(getValueNum(jsonObject.get(excelCol.get(i).getField())));
|
|
|
|
|
}else{
|
|
|
|
|
dataCell.setCellValue(getValue(jsonObject.get(excelCol.get(i).getField())));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
iterator.remove();
|
|
|
|
|
rowNum++;
|
|
|
|
@ -110,6 +129,7 @@ public class ExcelMapUtil {
|
|
|
|
|
cellStyle.setFont(font);
|
|
|
|
|
cellStyle.setAlignment(HorizontalAlignment.CENTER_SELECTION);//设置水平居中
|
|
|
|
|
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);//设置垂直居中
|
|
|
|
|
cellStyle.setWrapText(true);//设置单元格内容自动换行
|
|
|
|
|
return cellStyle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|