|
|
@ -1,10 +1,7 @@
|
|
|
|
package com.ruoyi.common.utils.poi;
|
|
|
|
package com.ruoyi.common.utils.poi;
|
|
|
|
|
|
|
|
|
|
|
|
import com.ruoyi.common.annotation.Excel;
|
|
|
|
import com.ruoyi.common.annotation.Excel;
|
|
|
|
import org.apache.poi.ss.usermodel.Cell;
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
|
|
|
|
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
|
|
|
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
|
|
|
|
|
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
|
|
|
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
@ -48,6 +45,7 @@ public class MultiSheetExcelUtil {
|
|
|
|
fillDataRows(sheet, list, fields);
|
|
|
|
fillDataRows(sheet, list, fields);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void finishExport() throws IOException {
|
|
|
|
public void finishExport() throws IOException {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
String encodedFileName = encodeFilename(fileName);
|
|
|
|
String encodedFileName = encodeFilename(fileName);
|
|
|
@ -94,28 +92,6 @@ public class MultiSheetExcelUtil {
|
|
|
|
return fields;
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void createTitleRow(Sheet sheet, List<Object[]> fields) {
|
|
|
|
|
|
|
|
Row row = sheet.createRow(0);
|
|
|
|
|
|
|
|
for (int i = 0; i < fields.size(); i++) {
|
|
|
|
|
|
|
|
Cell cell = row.createCell(i);
|
|
|
|
|
|
|
|
cell.setCellValue((String) fields.get(i)[1]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private <T> void fillDataRows(Sheet sheet, List<T> list, List<Object[]> fields) {
|
|
|
|
|
|
|
|
int rowNum = 1;
|
|
|
|
|
|
|
|
for (T item : list) {
|
|
|
|
|
|
|
|
Row row = sheet.createRow(rowNum++);
|
|
|
|
|
|
|
|
for (int i = 0; i < fields.size(); i++) {
|
|
|
|
|
|
|
|
Cell cell = row.createCell(i);
|
|
|
|
|
|
|
|
Object[] fieldInfo = fields.get(i);
|
|
|
|
|
|
|
|
String fieldName = (String) fieldInfo[0];
|
|
|
|
|
|
|
|
Object value = getFieldValueByName(fieldName, item);
|
|
|
|
|
|
|
|
setCellValue(cell, value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Object getFieldValueByName(String fieldName, Object obj) {
|
|
|
|
private Object getFieldValueByName(String fieldName, Object obj) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
Field field = obj.getClass().getDeclaredField(fieldName);
|
|
|
|
Field field = obj.getClass().getDeclaredField(fieldName);
|
|
|
@ -144,4 +120,75 @@ public class MultiSheetExcelUtil {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 创建带样式的表头
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private CellStyle createHeaderStyle(Workbook workbook) {
|
|
|
|
|
|
|
|
CellStyle style = workbook.createCellStyle();
|
|
|
|
|
|
|
|
// 背景色
|
|
|
|
|
|
|
|
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
|
|
|
|
|
|
|
|
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
|
|
|
|
|
|
|
|
// 边框
|
|
|
|
|
|
|
|
style.setBorderTop(BorderStyle.THIN);
|
|
|
|
|
|
|
|
style.setBorderBottom(BorderStyle.THIN);
|
|
|
|
|
|
|
|
style.setBorderLeft(BorderStyle.THIN);
|
|
|
|
|
|
|
|
style.setBorderRight(BorderStyle.THIN);
|
|
|
|
|
|
|
|
// 字体
|
|
|
|
|
|
|
|
Font font = workbook.createFont();
|
|
|
|
|
|
|
|
font.setColor(IndexedColors.BLACK.getIndex());
|
|
|
|
|
|
|
|
font.setFontName("Arial");
|
|
|
|
|
|
|
|
font.setFontHeightInPoints((short) 12);
|
|
|
|
|
|
|
|
font.setBold(true);
|
|
|
|
|
|
|
|
style.setFont(font);
|
|
|
|
|
|
|
|
// 对齐方式
|
|
|
|
|
|
|
|
style.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
|
|
|
style.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return style;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 创建数据行的样式(可选)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private CellStyle createDataStyle(Workbook workbook) {
|
|
|
|
|
|
|
|
CellStyle style = workbook.createCellStyle();
|
|
|
|
|
|
|
|
// 设置边框
|
|
|
|
|
|
|
|
style.setBorderTop(BorderStyle.THIN);
|
|
|
|
|
|
|
|
style.setBorderBottom(BorderStyle.THIN);
|
|
|
|
|
|
|
|
style.setBorderLeft(BorderStyle.THIN);
|
|
|
|
|
|
|
|
style.setBorderRight(BorderStyle.THIN);
|
|
|
|
|
|
|
|
// 设置自动换行(可选)
|
|
|
|
|
|
|
|
style.setWrapText(true);
|
|
|
|
|
|
|
|
return style;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void createTitleRow(Sheet sheet, List<Object[]> fields) {
|
|
|
|
|
|
|
|
CellStyle headerStyle = createHeaderStyle(workbook);
|
|
|
|
|
|
|
|
CellStyle dataStyle = createDataStyle(workbook); // 数据行样式(可选)
|
|
|
|
|
|
|
|
Row row = sheet.createRow(0);
|
|
|
|
|
|
|
|
for (int i = 0; i < fields.size(); i++) {
|
|
|
|
|
|
|
|
Cell cell = row.createCell(i);
|
|
|
|
|
|
|
|
cell.setCellValue((String) fields.get(i)[1]);
|
|
|
|
|
|
|
|
cell.setCellStyle(headerStyle);
|
|
|
|
|
|
|
|
// 设置列宽
|
|
|
|
|
|
|
|
sheet.setColumnWidth(i, Math.max(sheet.getColumnWidth(i), 3000));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private <T> void fillDataRows(Sheet sheet, List<T> list, List<Object[]> fields) {
|
|
|
|
|
|
|
|
CellStyle dataStyle = createDataStyle(workbook); // 应用数据行样式
|
|
|
|
|
|
|
|
int rowNum = 1;
|
|
|
|
|
|
|
|
for (T item : list) {
|
|
|
|
|
|
|
|
Row row = sheet.createRow(rowNum++);
|
|
|
|
|
|
|
|
for (int i = 0; i < fields.size(); i++) {
|
|
|
|
|
|
|
|
Cell cell = row.createCell(i);
|
|
|
|
|
|
|
|
Object[] fieldInfo = fields.get(i);
|
|
|
|
|
|
|
|
String fieldName = (String) fieldInfo[0];
|
|
|
|
|
|
|
|
Object value = getFieldValueByName(fieldName, item);
|
|
|
|
|
|
|
|
setCellValue(cell, value);
|
|
|
|
|
|
|
|
cell.setCellStyle(dataStyle); // 应用数据行样式
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|