|
@@ -0,0 +1,224 @@
|
|
|
+package com.persagy.apm.common.excel;
|
|
|
+
|
|
|
+import com.persagy.apm.common.configuration.DataUtils;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
+import org.apache.poi.ss.util.WorkbookUtil;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.ByteArrayInputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.lang.reflect.Field;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class ExcelUtils {
|
|
|
+
|
|
|
+ /***
|
|
|
+ * @description:导出excel
|
|
|
+ * @param: sheetName 页签名称
|
|
|
+ * @param: titleNameList 表头列表
|
|
|
+ * @param: titleCodeList 表头编码列表
|
|
|
+ * @param: dataObjectList 数据对象列表
|
|
|
+ * @return: void
|
|
|
+ * @exception:
|
|
|
+ * @author: fenghanchao
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 11:02 2020/7/9
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ public static <T> Workbook exportToExcel(String sheetName, List<String> titleNameList, List<String> titleCodeList
|
|
|
+ , List<T> dataObjectList) throws Exception {
|
|
|
+ // 根据模板,创建工作簿
|
|
|
+ Workbook workBook = new XSSFWorkbook();
|
|
|
+ String safeName = WorkbookUtil.createSafeSheetName(sheetName);
|
|
|
+ Sheet sheet = workBook.createSheet(safeName);
|
|
|
+
|
|
|
+ for (int columnNumber = 0; columnNumber < titleNameList.size(); columnNumber++) {
|
|
|
+ createCell(sheet, 0, columnNumber, titleNameList.get(columnNumber));
|
|
|
+ }
|
|
|
+ // 输出流
|
|
|
+ int skipNum = 1;
|
|
|
+ for (int rowNumber = 0; rowNumber < dataObjectList.size(); rowNumber++) {
|
|
|
+ T dataObject = dataObjectList.get(rowNumber);
|
|
|
+ for (int columnNumber = 0; columnNumber < titleCodeList.size(); columnNumber++) {
|
|
|
+ Field field = dataObject.getClass().getDeclaredField(titleCodeList.get(columnNumber));
|
|
|
+ field.setAccessible(true);
|
|
|
+ createCell(sheet, rowNumber + skipNum, columnNumber, field.get(dataObject));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return workBook;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void createCell(Sheet sheet, int rowNumber, int cellNumber, Object content) {
|
|
|
+ Row row = sheet.getRow(rowNumber);
|
|
|
+ if (row == null) {
|
|
|
+ row = sheet.createRow(rowNumber);
|
|
|
+ }
|
|
|
+ Cell cell = row.getCell(cellNumber);
|
|
|
+ if (cell == null) {
|
|
|
+ cell = row.createCell(cellNumber);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (content == null) {
|
|
|
+ content = "";
|
|
|
+ }
|
|
|
+ cell.setCellValue(content.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Workbook exportToExcel(String sheetName, int skipRows, int skipColumns, List<List<String>> rowInfoList) throws Exception {
|
|
|
+ Workbook workBook = new XSSFWorkbook();
|
|
|
+ String safeName = WorkbookUtil.createSafeSheetName(sheetName);
|
|
|
+ Sheet sheet = workBook.createSheet(safeName);
|
|
|
+ exportToExcel(sheet, skipRows, skipColumns, rowInfoList);
|
|
|
+ return workBook;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void exportToExcel(Sheet sheet, int skipRows, int skipColumns, List<List<String>> rowInfoList) throws Exception {
|
|
|
+ for (int rowNumber = 0; rowNumber < rowInfoList.size(); rowNumber++) {
|
|
|
+ for (int columnNumber = 0; columnNumber < rowInfoList.get(rowNumber).size(); columnNumber++) {
|
|
|
+ createCell(sheet, rowNumber + skipRows, columnNumber + skipColumns, rowInfoList.get(rowNumber).get(columnNumber));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void excelResponse(HttpServletResponse response, Workbook wb, String fileName) {
|
|
|
+ OutputStream out = null;
|
|
|
+ try {
|
|
|
+ out = response.getOutputStream();
|
|
|
+ response.setContentType("application/vnd.ms-excel");
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename="
|
|
|
+ + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));
|
|
|
+ wb.write(out);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ out.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 获取excel的workbook
|
|
|
+ * @param: filePath 示例: "config/budget/template/excel/MonthEnergyExportTemplate.xlsx"
|
|
|
+ * @return: org.apache.poi.ss.usermodel.Workbook
|
|
|
+ * @author: fenghanchao
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 10:34 2021/5/14
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ public static Workbook getWorkbook(String filePath) throws Exception {
|
|
|
+ InputStream in = ExcelUtils.class.getResourceAsStream(filePath);
|
|
|
+ // 读取excel模板
|
|
|
+ return new XSSFWorkbook(in);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 合并一行的单元格
|
|
|
+ * @param: workbook
|
|
|
+ * @param: rowNum
|
|
|
+ * @param: mergeCellsList eg: [[0,1,2],[3,4],[5,6]] 合并0,1,2单元格; 3,4单元格; 5,6单元格
|
|
|
+ * @return: void
|
|
|
+ * @exception:
|
|
|
+ * @author: fenghanchao
|
|
|
+ * @company: Persagy Technology Co.,Ltd
|
|
|
+ * @since: 10:48 2021/5/14
|
|
|
+ * @version: V1.0
|
|
|
+ */
|
|
|
+ public static void mergeRowCell(Workbook workbook, List<ExcelMergeDTO> mergeDTOList) throws Exception {
|
|
|
+ if (CollectionUtils.isEmpty(mergeDTOList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Sheet sheet0 = workbook.getSheetAt(0);
|
|
|
+ mergeDTOList.forEach(mergeDTO -> {
|
|
|
+ CellRangeAddress region = new CellRangeAddress(mergeDTO.getStartRow(), mergeDTO.getEndRow(), mergeDTO.getStartColumn()
|
|
|
+ , mergeDTO.getEndColumn());
|
|
|
+ sheet0.addMergedRegion(region);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public static List<List<String>> getExcelSheetInfo(byte[] excelByteArray, String sheetName) throws Exception {
|
|
|
+ InputStream in = new ByteArrayInputStream(excelByteArray);
|
|
|
+ Workbook workBook = WorkbookFactory.create(in);
|
|
|
+ // 遍历excel中的sheet
|
|
|
+ List<List<String>> resultInfo = new ArrayList<>();
|
|
|
+ Sheet sheet = workBook.getSheet(sheetName);
|
|
|
+ if (sheet == null) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+
|
|
|
+ int lastColumnNum = 0;
|
|
|
+ for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
|
|
|
+ Row row = sheet.getRow(rowNum);
|
|
|
+ if (row == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (lastColumnNum < row.getLastCellNum()) {
|
|
|
+ lastColumnNum = row.getLastCellNum();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
|
|
|
+ Row row = sheet.getRow(rowNum);
|
|
|
+ ArrayList<String> curarr = new ArrayList<>();
|
|
|
+ if (row == null) {
|
|
|
+ for (int columnNum = 0; columnNum < lastColumnNum; columnNum++) {
|
|
|
+ curarr.add("");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for (int columnNum = 0; columnNum < lastColumnNum; columnNum++) {
|
|
|
+ Cell cell = row.getCell(columnNum);
|
|
|
+ curarr.add(getCellStringValue(cell));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ resultInfo.add(curarr);
|
|
|
+ }
|
|
|
+ return resultInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getCellStringValue(Cell cell) throws Exception {
|
|
|
+ if (cell == null) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ String value;
|
|
|
+ switch (cell.getCellTypeEnum()) {
|
|
|
+ case STRING:
|
|
|
+ value = cell.getStringCellValue();
|
|
|
+ break;
|
|
|
+ case NUMERIC:
|
|
|
+ if (DateUtil.isCellDateFormatted(cell)) {
|
|
|
+ value = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(cell.getDateCellValue());
|
|
|
+ } else {
|
|
|
+ value = DataUtils.parseString(cell.getNumericCellValue());
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case BOOLEAN:
|
|
|
+ value = String.valueOf(cell.getBooleanCellValue());
|
|
|
+ break;
|
|
|
+ case BLANK:
|
|
|
+ value = "";
|
|
|
+ break;
|
|
|
+ case ERROR:
|
|
|
+ throw new Exception("can not resolve this cell! CELL_TYPE_ERROR! ");
|
|
|
+ case FORMULA:
|
|
|
+ value = cell.getCellFormula();
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ value = cell.getStringCellValue();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ return StringUtils.trim(value);
|
|
|
+ }
|
|
|
+}
|