使用hutool工具類進行簡單的excel導入和導出的工具類

public class ExcelUtils { private static final Logger logger = LoggerFactory.getLogger(ExcelUtils.class); private static List<List<Object>> lineList = new ArrayList<>(); /** * excel 導出工具類 * * @param response * @param fileName 文件名 * @param projects 對象集合 * @param columnNames 導出的excel中的列名 * @param keys 對應的是對象中的字段名字 * @throws IOException */
    public static void export(HttpServletResponse response, String fileName, List<?> projects, String[] columnNames, String[] keys) throws IOException { ExcelWriter bigWriter = ExcelUtil.getBigWriter(); for (int i = 0; i < columnNames.length; i++) { bigWriter.addHeaderAlias(columnNames[i], keys[i]); bigWriter.setColumnWidth(i, 20); } // 一次性寫出內容,使用默認樣式,強制輸出標題
        bigWriter.write(projects, true); //response爲HttpServletResponse對象
        response.setContentType("application/vnd.ms-excel;charset=utf-8"); //test.xls是彈出下載對話框的文件名,不能爲中文,中文請自行編碼
        response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + ".xlsx").getBytes(), "iso-8859-1")); ServletOutputStream out = response.getOutputStream(); bigWriter.flush(out, true); // 關閉writer,釋放內存
 bigWriter.close(); //此處記得關閉輸出Servlet流
 IoUtil.close(out); } /** * excel導入工具類 * * @param file 文件 * @param columNames 列對應的字段名 * @return 返回數據集合 * @throws OperationException * @throws IOException */
    public static List<Map<String, Object>> leading(MultipartFile file, String[] columNames) throws OperationException, IOException { String fileName = file.getOriginalFilename(); // 上傳文件爲空
        if (StringUtils.isEmpty(fileName)) { throw new OperationException(ReturnCodeEnum.OPERATION_EXCEL_ERROR, "沒有導入文件"); } //上傳文件大小爲1000條數據
        if (file.getSize() > 1024 * 1024 * 10) { logger.error("upload | 上傳失敗: 文件大小超過10M,文件大小爲:{}", file.getSize()); throw new OperationException(ReturnCodeEnum.OPERATION_EXCEL_ERROR, "上傳失敗: 文件大小不能超過10M!"); } // 上傳文件名格式不正確
        if (fileName.lastIndexOf(".") != -1 && !".xlsx".equals(fileName.substring(fileName.lastIndexOf(".")))) { throw new OperationException(ReturnCodeEnum.OPERATION_EXCEL_ERROR, "文件名格式不正確, 請使用後綴名爲.XLSX的文件"); } //讀取數據
        ExcelUtil.read07BySax(file.getInputStream(), 0, createRowHandler()); //去除excel中的第一行數據
        lineList.remove(0); //將數據封裝到list<Map>中
        List<Map<String, Object>> dataList = new ArrayList<>(); for (int i = 0; i < lineList.size(); i++) { if (null != lineList.get(i)) { Map<String, Object> hashMap = new HashMap<>(); for (int j = 0; j < columNames.length; j++) { Object property = lineList.get(i).get(j); hashMap.put(columNames[j], property); } dataList.add(hashMap); } else { break; } } return dataList; } /** * 經過實現handle方法編寫咱們要對每行數據的操做方式 */
    private static RowHandler createRowHandler() { //清空一下集合中的數據
 lineList.removeAll(lineList); return new RowHandler() { @Override public void handle(int sheetIndex, int rowIndex, List rowlist) { //將讀取到的每一行數據放入到list集合中
                JSONArray jsonObject = new JSONArray(rowlist); lineList.add(jsonObject.toList(Object.class)); } }; } }
相關文章
相關標籤/搜索