|
|
|
@ -18,6 +18,7 @@ import java.util.Map;
|
|
|
|
|
import java.util.Set; |
|
|
|
|
import java.util.UUID; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
import org.apache.poi.ss.usermodel.BorderStyle; |
|
|
|
|
import org.apache.poi.ss.usermodel.Cell; |
|
|
|
|
import org.apache.poi.ss.usermodel.CellStyle; |
|
|
|
@ -38,6 +39,7 @@ import org.apache.poi.ss.usermodel.VerticalAlignment;
|
|
|
|
|
import org.apache.poi.ss.usermodel.Workbook; |
|
|
|
|
import org.apache.poi.ss.usermodel.WorkbookFactory; |
|
|
|
|
import org.apache.poi.ss.util.CellRangeAddressList; |
|
|
|
|
import org.apache.poi.util.IOUtils; |
|
|
|
|
import org.apache.poi.xssf.streaming.SXSSFWorkbook; |
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFClientAnchor; |
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFDataValidation; |
|
|
|
@ -116,12 +118,12 @@ public class ExcelUtil<T>
|
|
|
|
|
* 统计列表 |
|
|
|
|
*/ |
|
|
|
|
private Map<Integer, Double> statistics = new HashMap<Integer, Double>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 数字格式 |
|
|
|
|
*/ |
|
|
|
|
private static final DecimalFormat DOUBLE_FORMAT = new DecimalFormat("######0.00"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 实体对象 |
|
|
|
|
*/ |
|
|
|
@ -339,6 +341,23 @@ public class ExcelUtil<T>
|
|
|
|
|
return exportExcel(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 对list数据源将其里面的数据导入到excel表单 |
|
|
|
|
* |
|
|
|
|
* @param response 返回数据 |
|
|
|
|
* @param list 导出数据集合 |
|
|
|
|
* @param sheetName 工作表的名称 |
|
|
|
|
* @return 结果 |
|
|
|
|
* @throws IOException |
|
|
|
|
*/ |
|
|
|
|
public void exportExcel(HttpServletResponse response, List<T> list, String sheetName) throws IOException |
|
|
|
|
{ |
|
|
|
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
|
|
|
|
response.setCharacterEncoding("utf-8"); |
|
|
|
|
this.init(list, sheetName, Type.EXPORT); |
|
|
|
|
exportExcel(response.getOutputStream()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 对list数据源将其里面的数据导入到excel表单 |
|
|
|
|
* |
|
|
|
@ -351,6 +370,43 @@ public class ExcelUtil<T>
|
|
|
|
|
return exportExcel(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 对list数据源将其里面的数据导入到excel表单 |
|
|
|
|
* |
|
|
|
|
* @param sheetName 工作表的名称 |
|
|
|
|
* @return 结果 |
|
|
|
|
*/ |
|
|
|
|
public void importTemplateExcel(HttpServletResponse response, String sheetName) throws IOException |
|
|
|
|
{ |
|
|
|
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); |
|
|
|
|
response.setCharacterEncoding("utf-8"); |
|
|
|
|
this.init(null, sheetName, Type.IMPORT); |
|
|
|
|
exportExcel(response.getOutputStream()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 对list数据源将其里面的数据导入到excel表单 |
|
|
|
|
* |
|
|
|
|
* @return 结果 |
|
|
|
|
*/ |
|
|
|
|
public void exportExcel(OutputStream out) |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
writeSheet(); |
|
|
|
|
wb.write(out); |
|
|
|
|
} |
|
|
|
|
catch (Exception e) |
|
|
|
|
{ |
|
|
|
|
log.error("导出Excel异常{}", e.getMessage()); |
|
|
|
|
} |
|
|
|
|
finally |
|
|
|
|
{ |
|
|
|
|
IOUtils.closeQuietly(wb); |
|
|
|
|
IOUtils.closeQuietly(out); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 对list数据源将其里面的数据导入到excel表单 |
|
|
|
|
* |
|
|
|
@ -361,27 +417,7 @@ public class ExcelUtil<T>
|
|
|
|
|
OutputStream out = null; |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
// 取出一共有多少个sheet.
|
|
|
|
|
double sheetNo = Math.ceil(list.size() / sheetSize); |
|
|
|
|
for (int index = 0; index <= sheetNo; index++) |
|
|
|
|
{ |
|
|
|
|
createSheet(sheetNo, index); |
|
|
|
|
|
|
|
|
|
// 产生一行
|
|
|
|
|
Row row = sheet.createRow(0); |
|
|
|
|
int column = 0; |
|
|
|
|
// 写入各个字段的列头名称
|
|
|
|
|
for (Object[] os : fields) |
|
|
|
|
{ |
|
|
|
|
Excel excel = (Excel) os[1]; |
|
|
|
|
this.createCell(excel, row, column++); |
|
|
|
|
} |
|
|
|
|
if (Type.EXPORT.equals(type)) |
|
|
|
|
{ |
|
|
|
|
fillExcelData(index, row); |
|
|
|
|
addStatisticsRow(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
writeSheet(); |
|
|
|
|
String filename = encodingFilename(sheetName); |
|
|
|
|
out = new FileOutputStream(getAbsoluteFile(filename)); |
|
|
|
|
wb.write(out); |
|
|
|
@ -394,27 +430,35 @@ public class ExcelUtil<T>
|
|
|
|
|
} |
|
|
|
|
finally |
|
|
|
|
{ |
|
|
|
|
if (wb != null) |
|
|
|
|
IOUtils.closeQuietly(wb); |
|
|
|
|
IOUtils.closeQuietly(out); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 创建写入数据到Sheet |
|
|
|
|
*/ |
|
|
|
|
public void writeSheet() |
|
|
|
|
{ |
|
|
|
|
// 取出一共有多少个sheet.
|
|
|
|
|
double sheetNo = Math.ceil(list.size() / sheetSize); |
|
|
|
|
for (int index = 0; index <= sheetNo; index++) |
|
|
|
|
{ |
|
|
|
|
createSheet(sheetNo, index); |
|
|
|
|
|
|
|
|
|
// 产生一行
|
|
|
|
|
Row row = sheet.createRow(0); |
|
|
|
|
int column = 0; |
|
|
|
|
// 写入各个字段的列头名称
|
|
|
|
|
for (Object[] os : fields) |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
wb.close(); |
|
|
|
|
} |
|
|
|
|
catch (IOException e1) |
|
|
|
|
{ |
|
|
|
|
e1.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
Excel excel = (Excel) os[1]; |
|
|
|
|
this.createCell(excel, row, column++); |
|
|
|
|
} |
|
|
|
|
if (out != null) |
|
|
|
|
if (Type.EXPORT.equals(type)) |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
out.close(); |
|
|
|
|
} |
|
|
|
|
catch (IOException e1) |
|
|
|
|
{ |
|
|
|
|
e1.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
fillExcelData(index, row); |
|
|
|
|
addStatisticsRow(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -486,7 +530,7 @@ public class ExcelUtil<T>
|
|
|
|
|
headerFont.setColor(IndexedColors.WHITE.getIndex()); |
|
|
|
|
style.setFont(headerFont); |
|
|
|
|
styles.put("header", style); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
style = wb.createCellStyle(); |
|
|
|
|
style.setAlignment(HorizontalAlignment.CENTER); |
|
|
|
|
style.setVerticalAlignment(VerticalAlignment.CENTER); |
|
|
|
@ -550,8 +594,7 @@ public class ExcelUtil<T>
|
|
|
|
|
} |
|
|
|
|
else if (ColumnType.IMAGE == attr.cellType()) |
|
|
|
|
{ |
|
|
|
|
ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1), |
|
|
|
|
cell.getRow().getRowNum() + 1); |
|
|
|
|
ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1), cell.getRow().getRowNum() + 1); |
|
|
|
|
String imagePath = Convert.toStr(value); |
|
|
|
|
if (StringUtils.isNotEmpty(imagePath)) |
|
|
|
|
{ |
|
|
|
@ -561,7 +604,7 @@ public class ExcelUtil<T>
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 获取画布 |
|
|
|
|
*/ |
|
|
|
@ -804,7 +847,7 @@ public class ExcelUtil<T>
|
|
|
|
|
} |
|
|
|
|
return StringUtils.stripEnd(propertyString.toString(), separator); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 解析字典值 |
|
|
|
|
* |
|
|
|
@ -830,7 +873,7 @@ public class ExcelUtil<T>
|
|
|
|
|
{ |
|
|
|
|
return DictUtils.getDictValue(dictType, dictLabel, separator); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 合计统计信息 |
|
|
|
|
*/ |
|
|
|
@ -867,7 +910,7 @@ public class ExcelUtil<T>
|
|
|
|
|
cell = row.createCell(0); |
|
|
|
|
cell.setCellStyle(styles.get("total")); |
|
|
|
|
cell.setCellValue("合计"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (Integer key : keys) |
|
|
|
|
{ |
|
|
|
|
cell = row.createCell(key); |
|
|
|
@ -985,7 +1028,7 @@ public class ExcelUtil<T>
|
|
|
|
|
this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList()); |
|
|
|
|
this.maxHeight = getRowHeight(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 根据注解获取最大行高 |
|
|
|
|
*/ |
|
|
|
@ -1099,7 +1142,7 @@ public class ExcelUtil<T>
|
|
|
|
|
} |
|
|
|
|
return val; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 判断是否是空行 |
|
|
|
|
* |
|
|
|
@ -1112,7 +1155,7 @@ public class ExcelUtil<T>
|
|
|
|
|
{ |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) |
|
|
|
|
for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) |
|
|
|
|
{ |
|
|
|
|
Cell cell = row.getCell(i); |
|
|
|
|
if (cell != null && cell.getCellType() != CellType.BLANK) |
|
|
|
|