|
@@ -150,6 +150,8 @@ public class ExcelUtil<T>
|
|
*/
|
|
*/
|
|
public String[] excludeFields;
|
|
public String[] excludeFields;
|
|
|
|
|
|
|
|
+ public String[] includeFields;
|
|
|
|
+
|
|
public ExcelUtil(Class<T> clazz)
|
|
public ExcelUtil(Class<T> clazz)
|
|
{
|
|
{
|
|
this.clazz = clazz;
|
|
this.clazz = clazz;
|
|
@@ -168,6 +170,10 @@ public class ExcelUtil<T>
|
|
|
|
|
|
public void init(List<T> list, String sheetName, String title, Type type)
|
|
public void init(List<T> list, String sheetName, String title, Type type)
|
|
{
|
|
{
|
|
|
|
+ this.init(list, sheetName, title, type, new String[0]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public void init(List<T> list, String sheetName, String title, Type type, String[] exportField) {
|
|
if (list == null)
|
|
if (list == null)
|
|
{
|
|
{
|
|
list = new ArrayList<T>();
|
|
list = new ArrayList<T>();
|
|
@@ -176,6 +182,8 @@ public class ExcelUtil<T>
|
|
this.sheetName = sheetName;
|
|
this.sheetName = sheetName;
|
|
this.type = type;
|
|
this.type = type;
|
|
this.title = title;
|
|
this.title = title;
|
|
|
|
+ this.includeFields = exportField;
|
|
|
|
+
|
|
createExcelField();
|
|
createExcelField();
|
|
createWorkbook();
|
|
createWorkbook();
|
|
createTitle();
|
|
createTitle();
|
|
@@ -201,7 +209,9 @@ public class ExcelUtil<T>
|
|
Cell titleCell = titleRow.createCell(0);
|
|
Cell titleCell = titleRow.createCell(0);
|
|
titleCell.setCellStyle(styles.get("title"));
|
|
titleCell.setCellStyle(styles.get("title"));
|
|
titleCell.setCellValue(title);
|
|
titleCell.setCellValue(title);
|
|
- sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), titleRow.getRowNum(), titleLastCol));
|
|
|
|
|
|
+ if (titleLastCol != 0) {
|
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), titleRow.getRowNum(), titleLastCol));
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -492,6 +502,13 @@ public class ExcelUtil<T>
|
|
this.init(list, sheetName, title, Type.EXPORT);
|
|
this.init(list, sheetName, title, Type.EXPORT);
|
|
exportExcel(response);
|
|
exportExcel(response);
|
|
}
|
|
}
|
|
|
|
+ public void exportExcel(HttpServletResponse response, List<T> list, String sheetName, String title, String [] columns)
|
|
|
|
+ {
|
|
|
|
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
|
+ this.init(list, sheetName, title, Type.EXPORT, columns);
|
|
|
|
+ exportExcel(response);
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
* 对list数据源将其里面的数据导入到excel表单
|
|
* 对list数据源将其里面的数据导入到excel表单
|
|
@@ -543,6 +560,13 @@ public class ExcelUtil<T>
|
|
exportExcel(response);
|
|
exportExcel(response);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public void importTemplateExcel(HttpServletResponse response, String sheetName, String title, String []exportFields) {
|
|
|
|
+ response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
|
+ this.init(null, sheetName, title, Type.IMPORT, exportFields);
|
|
|
|
+ exportExcel(response);
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 对list数据源将其里面的数据导入到excel表单
|
|
* 对list数据源将其里面的数据导入到excel表单
|
|
*
|
|
*
|
|
@@ -557,6 +581,7 @@ public class ExcelUtil<T>
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
catch (Exception e)
|
|
{
|
|
{
|
|
|
|
+ e.printStackTrace();
|
|
log.error("导出Excel异常{}", e.getMessage());
|
|
log.error("导出Excel异常{}", e.getMessage());
|
|
}
|
|
}
|
|
finally
|
|
finally
|
|
@@ -651,6 +676,7 @@ public class ExcelUtil<T>
|
|
row = sheet.createRow(rowNo);
|
|
row = sheet.createRow(rowNo);
|
|
// 得到导出对象.
|
|
// 得到导出对象.
|
|
T vo = (T) list.get(i);
|
|
T vo = (T) list.get(i);
|
|
|
|
+// System.out.println("i: " + i + ", vo: " + vo.toString());
|
|
Collection<?> subList = null;
|
|
Collection<?> subList = null;
|
|
if (isSubList())
|
|
if (isSubList())
|
|
{
|
|
{
|
|
@@ -666,8 +692,10 @@ public class ExcelUtil<T>
|
|
}
|
|
}
|
|
}
|
|
}
|
|
int column = 0;
|
|
int column = 0;
|
|
|
|
+ int columnNum = 0;
|
|
for (Object[] os : fields)
|
|
for (Object[] os : fields)
|
|
{
|
|
{
|
|
|
|
+ columnNum++;
|
|
Field field = (Field) os[0];
|
|
Field field = (Field) os[0];
|
|
Excel excel = (Excel) os[1];
|
|
Excel excel = (Excel) os[1];
|
|
if (Collection.class.isAssignableFrom(field.getType()) && StringUtils.isNotNull(subList))
|
|
if (Collection.class.isAssignableFrom(field.getType()) && StringUtils.isNotNull(subList))
|
|
@@ -700,6 +728,8 @@ public class ExcelUtil<T>
|
|
{
|
|
{
|
|
this.addCell(excel, row, vo, field, column++);
|
|
this.addCell(excel, row, vo, field, column++);
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // setSizeColumn(sheet,columnNum-1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -844,14 +874,53 @@ public class ExcelUtil<T>
|
|
{
|
|
{
|
|
// 填充默认样式,防止合并单元格样式失效
|
|
// 填充默认样式,防止合并单元格样式失效
|
|
sheet.setDefaultColumnStyle(column, styles.get(StringUtils.format("data_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor())));
|
|
sheet.setDefaultColumnStyle(column, styles.get(StringUtils.format("data_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor())));
|
|
|
|
+
|
|
if (attr.needMerge())
|
|
if (attr.needMerge())
|
|
{
|
|
{
|
|
sheet.addMergedRegion(new CellRangeAddress(rownum - 1, rownum, column, column));
|
|
sheet.addMergedRegion(new CellRangeAddress(rownum - 1, rownum, column, column));
|
|
|
|
+
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return cell;
|
|
return cell;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 自适应宽度(中文支持)
|
|
|
|
+ *
|
|
|
|
+ * @param sheet sheet
|
|
|
|
+ * @param size 因为for循环从0开始,size值为 列数-1
|
|
|
|
+ */
|
|
|
|
+ public void setSizeColumn(Sheet sheet, int size) {
|
|
|
|
+ for (int columnNum = 0; columnNum <= size; columnNum++) {
|
|
|
|
+ int columnWidth = sheet.getColumnWidth(columnNum) / 256;
|
|
|
|
+ for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
|
|
|
|
+ Row currentRow;
|
|
|
|
+ //当前行未被使用过
|
|
|
|
+ if (sheet.getRow(rowNum) == null) {
|
|
|
|
+ currentRow = sheet.createRow(rowNum);
|
|
|
|
+ } else {
|
|
|
|
+ currentRow = sheet.getRow(rowNum);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (currentRow.getCell(columnNum) != null) {
|
|
|
|
+ Cell currentCell = currentRow.getCell(columnNum);
|
|
|
|
+ if (currentCell.getCellType() == CellType.STRING) {
|
|
|
|
+ int length = currentCell.getStringCellValue().getBytes().length;
|
|
|
|
+ if (columnWidth < length) {
|
|
|
|
+ columnWidth = length;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if(30 * 256 * 3 > columnWidth * 256){
|
|
|
|
+ sheet.setColumnWidth(columnNum, columnWidth * 256);
|
|
|
|
+ }else{
|
|
|
|
+ sheet.setColumnWidth(columnNum, 30 * 256 * 3);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 设置单元格信息
|
|
* 设置单元格信息
|
|
*
|
|
*
|
|
@@ -1028,7 +1097,7 @@ public class ExcelUtil<T>
|
|
* @param endCol 结束列
|
|
* @param endCol 结束列
|
|
*/
|
|
*/
|
|
public void setPromptOrValidation(Sheet sheet, String[] textlist, String promptContent, int firstRow, int endRow,
|
|
public void setPromptOrValidation(Sheet sheet, String[] textlist, String promptContent, int firstRow, int endRow,
|
|
- int firstCol, int endCol)
|
|
|
|
|
|
+ int firstCol, int endCol)
|
|
{
|
|
{
|
|
DataValidationHelper helper = sheet.getDataValidationHelper();
|
|
DataValidationHelper helper = sheet.getDataValidationHelper();
|
|
DataValidationConstraint constraint = textlist.length > 0 ? helper.createExplicitListConstraint(textlist) : helper.createCustomConstraint("DD1");
|
|
DataValidationConstraint constraint = textlist.length > 0 ? helper.createExplicitListConstraint(textlist) : helper.createCustomConstraint("DD1");
|
|
@@ -1356,6 +1425,9 @@ public class ExcelUtil<T>
|
|
{
|
|
{
|
|
this.fields = getFields();
|
|
this.fields = getFields();
|
|
this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
|
|
this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
|
|
|
|
+ if (this.includeFields.length > 0) {
|
|
|
|
+ this.fields = this.fields.stream().filter(v -> Arrays.stream(this.includeFields).anyMatch(e -> e.equals(((Field)v[0]).getName()))).collect(Collectors.toList());
|
|
|
|
+ }
|
|
this.maxHeight = getRowHeight();
|
|
this.maxHeight = getRowHeight();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -1688,6 +1760,7 @@ public class ExcelUtil<T>
|
|
}
|
|
}
|
|
return method;
|
|
return method;
|
|
}
|
|
}
|
|
|
|
+
|
|
//读取表头
|
|
//读取表头
|
|
public List<String> readExcelHeaders(InputStream inputStream) throws IOException {
|
|
public List<String> readExcelHeaders(InputStream inputStream) throws IOException {
|
|
List<String> headers = new ArrayList<>();
|
|
List<String> headers = new ArrayList<>();
|
|
@@ -1702,6 +1775,7 @@ public class ExcelUtil<T>
|
|
Cell cell = cellIterator.next();
|
|
Cell cell = cellIterator.next();
|
|
headers.add(cell.getStringCellValue());
|
|
headers.add(cell.getStringCellValue());
|
|
}
|
|
}
|
|
|
|
+
|
|
return headers;
|
|
return headers;
|
|
}
|
|
}
|
|
}
|
|
}
|