|
@@ -31,6 +31,8 @@ import org.apache.poi.ss.util.CellRangeAddressList;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.util.ResourceUtils;
|
|
|
+
|
|
|
+import com.ruoyi.common.utils.StringUtils;
|
|
|
import com.ruoyi.framework.aspectj.lang.annotation.Excel;
|
|
|
import com.ruoyi.framework.shiro.web.session.OnlineWebSessionManager;
|
|
|
import com.ruoyi.framework.web.domain.AjaxResult;
|
|
@@ -58,28 +60,32 @@ public class ExcelUtil<T>
|
|
|
|
|
|
Workbook workbook = WorkbookFactory.create(input);
|
|
|
Sheet sheet = workbook.getSheet(sheetName);
|
|
|
- if (!sheetName.trim().equals(""))
|
|
|
+ if (StringUtils.isNotEmpty(sheetName))
|
|
|
{
|
|
|
- sheet = workbook.getSheet(sheetName);
|
|
|
+
|
|
|
+ sheet = workbook.getSheet(sheetName);
|
|
|
}
|
|
|
if (sheet == null)
|
|
|
{
|
|
|
- sheet = workbook.getSheetAt(0);
|
|
|
+
|
|
|
+ sheet = workbook.getSheetAt(0);
|
|
|
}
|
|
|
int rows = sheet.getPhysicalNumberOfRows();
|
|
|
|
|
|
if (rows > 0)
|
|
|
{
|
|
|
-
|
|
|
- Field[] allFields = clazz.getDeclaredFields();
|
|
|
- Map<Integer, Field> fieldsMap = new HashMap<Integer, Field>();
|
|
|
+
|
|
|
+ Field[] allFields = clazz.getDeclaredFields();
|
|
|
+
|
|
|
+ Map<Integer, Field> fieldsMap = new HashMap<Integer, Field>();
|
|
|
for (int col = 0; col < allFields.length; col++)
|
|
|
{
|
|
|
Field field = allFields[col];
|
|
|
|
|
|
if (field.isAnnotationPresent(Excel.class))
|
|
|
{
|
|
|
- field.setAccessible(true);
|
|
|
+
|
|
|
+ field.setAccessible(true);
|
|
|
fieldsMap.put(col, field);
|
|
|
}
|
|
|
}
|
|
@@ -104,13 +110,15 @@ public class ExcelUtil<T>
|
|
|
}
|
|
|
|
|
|
String c = cell.getStringCellValue();
|
|
|
- if (c.equals(""))
|
|
|
+ if (StringUtils.isEmpty(c))
|
|
|
{
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- entity = (entity == null ? clazz.newInstance() : entity);
|
|
|
- Field field = fieldsMap.get(j);
|
|
|
+
|
|
|
+ entity = (entity == null ? clazz.newInstance() : entity);
|
|
|
+
|
|
|
+ Field field = fieldsMap.get(j);
|
|
|
|
|
|
Class<?> fieldType = field.getType();
|
|
|
if (String.class == fieldType)
|
|
@@ -179,7 +187,8 @@ public class ExcelUtil<T>
|
|
|
*/
|
|
|
public AjaxResult exportExcel(List<T> list, String sheetName)
|
|
|
{
|
|
|
- Field[] allFields = clazz.getDeclaredFields();
|
|
|
+
|
|
|
+ Field[] allFields = clazz.getDeclaredFields();
|
|
|
List<Field> fields = new ArrayList<Field>();
|
|
|
|
|
|
for (Field field : allFields)
|
|
@@ -190,32 +199,39 @@ public class ExcelUtil<T>
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- HSSFWorkbook workbook = new HSSFWorkbook();
|
|
|
+
|
|
|
+ HSSFWorkbook workbook = new HSSFWorkbook();
|
|
|
|
|
|
int sheetSize = 65536;
|
|
|
- double sheetNo = Math.ceil(list.size() / sheetSize);
|
|
|
+
|
|
|
+ double sheetNo = Math.ceil(list.size() / sheetSize);
|
|
|
for (int index = 0; index <= sheetNo; index++)
|
|
|
{
|
|
|
- HSSFSheet sheet = workbook.createSheet();
|
|
|
+
|
|
|
+ HSSFSheet sheet = workbook.createSheet();
|
|
|
if (sheetNo == 0)
|
|
|
{
|
|
|
workbook.setSheetName(index, sheetName);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- workbook.setSheetName(index, sheetName + index);
|
|
|
+
|
|
|
+ workbook.setSheetName(index, sheetName + index);
|
|
|
}
|
|
|
HSSFRow row;
|
|
|
HSSFCell cell;
|
|
|
|
|
|
- row = sheet.createRow(0);
|
|
|
+
|
|
|
+ row = sheet.createRow(0);
|
|
|
|
|
|
for (int i = 0; i < fields.size(); i++)
|
|
|
{
|
|
|
Field field = fields.get(i);
|
|
|
Excel attr = field.getAnnotation(Excel.class);
|
|
|
- cell = row.createCell(i);
|
|
|
- cell.setCellType(HSSFCell.CELL_TYPE_STRING);
|
|
|
+
|
|
|
+ cell = row.createCell(i);
|
|
|
+
|
|
|
+ cell.setCellType(HSSFCell.CELL_TYPE_STRING);
|
|
|
HSSFCellStyle cellStyle = workbook.createCellStyle();
|
|
|
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
|
|
|
cellStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
|
|
@@ -230,8 +246,10 @@ public class ExcelUtil<T>
|
|
|
else
|
|
|
{
|
|
|
HSSFFont font = workbook.createFont();
|
|
|
- font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
|
|
|
- cellStyle.setFont(font);
|
|
|
+
|
|
|
+ font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
|
|
|
+
|
|
|
+ cellStyle.setFont(font);
|
|
|
cellStyle.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
|
|
|
|
|
|
sheet.setColumnWidth(i, 3766);
|
|
@@ -240,17 +258,20 @@ public class ExcelUtil<T>
|
|
|
cellStyle.setWrapText(true);
|
|
|
cell.setCellStyle(cellStyle);
|
|
|
|
|
|
- cell.setCellValue(attr.name());
|
|
|
+
|
|
|
+ cell.setCellValue(attr.name());
|
|
|
|
|
|
|
|
|
- if (!attr.prompt().trim().equals(""))
|
|
|
+ if (StringUtils.isNotEmpty(attr.prompt()))
|
|
|
{
|
|
|
- setHSSFPrompt(sheet, "", attr.prompt(), 1, 100, i, i);
|
|
|
+
|
|
|
+ setHSSFPrompt(sheet, "", attr.prompt(), 1, 100, i, i);
|
|
|
}
|
|
|
|
|
|
if (attr.combo().length > 0)
|
|
|
{
|
|
|
- setHSSFValidation(sheet, attr.combo(), 1, 100, i, i);
|
|
|
+
|
|
|
+ setHSSFValidation(sheet, attr.combo(), 1, 100, i, i);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -263,18 +284,22 @@ public class ExcelUtil<T>
|
|
|
for (int i = startNo; i < endNo; i++)
|
|
|
{
|
|
|
row = sheet.createRow(i + 1 - startNo);
|
|
|
- T vo = (T) list.get(i);
|
|
|
+
|
|
|
+ T vo = (T) list.get(i);
|
|
|
for (int j = 0; j < fields.size(); j++)
|
|
|
{
|
|
|
- Field field = fields.get(j);
|
|
|
- field.setAccessible(true);
|
|
|
+
|
|
|
+ Field field = fields.get(j);
|
|
|
+
|
|
|
+ field.setAccessible(true);
|
|
|
Excel attr = field.getAnnotation(Excel.class);
|
|
|
try
|
|
|
{
|
|
|
|
|
|
if (attr.isExport())
|
|
|
{
|
|
|
- cell = row.createCell(j);
|
|
|
+
|
|
|
+ cell = row.createCell(j);
|
|
|
cell.setCellStyle(cs);
|
|
|
try
|
|
|
{
|
|
@@ -292,11 +317,13 @@ public class ExcelUtil<T>
|
|
|
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
|
|
|
if (vo == null)
|
|
|
{
|
|
|
- cell.setCellValue("");
|
|
|
+
|
|
|
+ cell.setCellValue("");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- cell.setCellValue(field.get(vo) == null ? "" : String.valueOf(field.get(vo)));
|
|
|
+
|
|
|
+ cell.setCellValue(field.get(vo) == null ? "" : String.valueOf(field.get(vo)));
|
|
|
}
|
|
|
|
|
|
}
|
|
@@ -329,18 +356,18 @@ public class ExcelUtil<T>
|
|
|
*
|
|
|
* @param col
|
|
|
*/
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
* 设置单元格上提示
|
|
@@ -362,9 +389,9 @@ public class ExcelUtil<T>
|
|
|
|
|
|
CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
|
|
|
|
|
|
- HSSFDataValidation data_validation_view = new HSSFDataValidation(regions, constraint);
|
|
|
- data_validation_view.createPromptBox(promptTitle, promptContent);
|
|
|
- sheet.addValidationData(data_validation_view);
|
|
|
+ HSSFDataValidation dataValidationView = new HSSFDataValidation(regions, constraint);
|
|
|
+ dataValidationView.createPromptBox(promptTitle, promptContent);
|
|
|
+ sheet.addValidationData(dataValidationView);
|
|
|
return sheet;
|
|
|
}
|
|
|
|
|
@@ -387,8 +414,8 @@ public class ExcelUtil<T>
|
|
|
|
|
|
CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
|
|
|
|
|
|
- HSSFDataValidation data_validation_list = new HSSFDataValidation(regions, constraint);
|
|
|
- sheet.addValidationData(data_validation_list);
|
|
|
+ HSSFDataValidation dataValidationList = new HSSFDataValidation(regions, constraint);
|
|
|
+ sheet.addValidationData(dataValidationList);
|
|
|
return sheet;
|
|
|
}
|
|
|
|