<dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>${easypoi-web.version}</version> </dependency>
<easypoi-web.version>3.2.0</easypoi-web.version>
文件轉 Base64、文件轉字符串

package com.yirui.supervisor.util; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.Base64; public class FileBase64 { public static void main(String[] args) { String path="D:\\a.pdf"; // decryptByBase64(s,path); } public static Boolean decryptByBase64(String base64, String filePath) { if (base64 == null && filePath == null) { return false; } try { Files.write(Paths.get(filePath), Base64.getMimeDecoder().decode(base64), StandardOpenOption.CREATE); } catch (IOException e) { e.printStackTrace(); } return true; } public static String encryptToBase64(String filePath) { if (filePath == null) { return null; } try { byte[] b = Files.readAllBytes(Paths.get(filePath)); return Base64.getMimeEncoder().encodeToString(b); } catch (IOException e) { e.printStackTrace(); } return null; } }
文件轉base64第二種
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
String filebyteString = Base64.encodeBase64String(FileUtils.readFileToByteArray(file));
導入獲取數據的工具類

package com.yirui.supervisor.util; import cn.afterturn.easypoi.excel.ExcelImportUtil; import cn.afterturn.easypoi.excel.entity.ImportParams; import java.io.File; import java.io.IOException; import java.util.List; public class ExlsxFileUtil { public static <T> List<T> getData(String fileBase64, Class<T> clazz) throws IOException { String filePath = System.getProperty("user.dir") +".xlsx"; Boolean aBoolean = FileBase64.decryptByBase64(fileBase64, filePath); if(aBoolean){ ImportParams params = new ImportParams(); params.setHeadRows(1); File file = new File(filePath); List<T> list = ExcelImportUtil.importExcel(file,clazz, params); deleteFile(filePath); return list; }else{ return null; } } public static void deleteFile(String filePath){ File file = new File(filePath); if(file.exists()){ file.delete(); } } }
// replace格式為 "替換前的值_替換后的值" @Excel(name = "性別*", replace = {"男_0", "女_1"}) private Integer gender;
easypoi導出
poi
@ApiOperation(value = "項目_導入模板下載", notes = "項目_導入模板下載") @PostMapping("/mould") public void mould(HttpServletResponse response) throws Exception { ExportParams params = new ExportParams(); Workbook workbook = ExcelExportUtil.exportExcel(params, ProjectImport.class, new ArrayList<>()); OutputStream out = response.getOutputStream(); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename="+new String("項目_導入模板.xlsx".getBytes("utf-8"),"ISO-8859-1")); workbook.write(out); out.close(); workbook.close(); }
加密只讀
Sheet sheet = workbook.getSheetAt(0); //excel加密只讀 sheet.protectSheet(UUID.randomUUID().toString());
生成下拉
Sheet sheet = workbook.getSheetAt(0); // 只對(0,0)單元格有效 CellRangeAddressList regions = new CellRangeAddressList(1, 1000, 3, 3); // 生成下拉框內容 DVConstraint constraint = DVConstraint.createExplicitListConstraint(new String[] {"建設單位", "監理單位", "施工單位(總包)", "施工單位(分包)", "設計單位", "勘察單位", "檢測單位", "監測單位", "商混供應單位"}); // 綁定下拉框和作用區域 HSSFDataValidation data_validation = new HSSFDataValidation(regions,constraint); // 對sheet頁生效 sheet.addValidationData(data_validation);
處理不能超過255的異常
public static HSSFDataValidation getDataValidationList4Col( int firstRow, int endRow,int firstCol, int endCol, List<String> colName, Workbook wbCreat) { String[] dataArray = colName.toArray(new String[0]); Sheet hidden = wbCreat.createSheet("hidden"); Cell cell = null; for (int i = 0, length = dataArray.length; i < length; i++) { String name = dataArray[i]; Row row = hidden.createRow(i); cell = row.createCell(0); cell.setCellValue(name); } Name namedCell = wbCreat.createName(); namedCell.setNameName("hidden"); namedCell.setRefersToFormula("hidden!$A$1:$A$" + dataArray.length); //加載數據,將名稱為hidden的 DVConstraint constraint = DVConstraint.createFormulaListConstraint("hidden"); // 設置數據有效性加載在哪個單元格上,四個參數分別是:起始行、終止行、起始列、終止列 CellRangeAddressList addressList = new CellRangeAddressList(firstRow, endRow, firstCol, endCol); HSSFDataValidation validation = new HSSFDataValidation(addressList, constraint); //將第二個sheet設置為隱藏 wbCreat.setSheetHidden(1, true); return validation; }
獲取sheet名稱 /easypoi獲取sheet名稱
ExcelImportResult<Map> result = ExcelImportUtil.importExcelMore(file, Map.class, params); Workbook workbook = result.getFailWorkbook(); String name = workbook.getSheetAt(0).getSheetName();