Excel導入導出(簡單、好用且輕量級的海量Excel文件導入導出解決方案)


excel導入導出

在實際項目開發中經常需要使用excel導入數據或導出數據到excel中,使用POI做開發有點麻煩,推薦大家一個特別好用的excel導入導出的組件,超級好用,gitee地址:https://gitee.com/wuwenze/ExcelKit

搭建步驟

首先導入依賴

<dependency>
    <groupId>com.wuwenze</groupId>
    <artifactId>ExcelKit</artifactId>
    <version>2.0.72</version>
    <exclusions>
        <exclusion>
            <artifactId>xml-apis</artifactId>
            <groupId>xml-apis</groupId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>xml-apis</groupId>
    <artifactId>xml-apis</artifactId>
    <version>1.4.01</version>
</dependency>

特別說明:如果只是導入Excelkit包的話,可能xml-apis包版本太低而報錯,所以從新高版本的xml-apis可以解決

創建實體類,配置exle與實體之間的映射關系

@Data
@Excel(value = "用戶信息")
@AllArgsConstructor
@NoArgsConstructor
public class User {

    @ExcelField(value = "編號", width = 30)
    private Long userId;

    @ExcelField(value = "用戶名",required = true)
    private String username;

    @ExcelField(value = "密碼", required = true, maxLength = 32)
    private String password;

    @ExcelField(value = "郵箱",required = true, maxLength = 32 )
    private String email;
}

一行代碼構建excel導入模板,創建controller類

@RestController
@Slf4j
public class ExcelController {

  /**
   * 下載excel模板
   *
   * @param response
   */
  @RequestMapping(value = "/downTemplate", method = RequestMethod.GET)
  public void downTemplate(HttpServletResponse response) {
    List<User> userList = new ArrayList<>();

    ExcelKit.$Export(User.class, response).downXlsx(userList, true);
  }
}

image-20211026231617507

通過excel導入模板數據

  @RequestMapping(value = "/importUser", method = RequestMethod.POST)
  public ResponseEntity<?> importUser(@RequestParam MultipartFile file) throws IOException {

    List<User> successList = Lists.newArrayList();
    List<Map<String, Object>> errorList = Lists.newArrayList();

    ExcelKit.$Import(User.class)
        .readXlsx(
            file.getInputStream(),
            new ExcelReadHandler<User>() {

              @Override
              public void onSuccess(int sheetIndex, int rowIndex, User entity) {
                successList.add(entity); // 單行讀取成功,加入入庫隊列。
              }

              @Override
              public void onError(int sheetIndex, int rowIndex, List<ExcelErrorField> errorFields) {
                // 讀取數據失敗,記錄了當前行所有失敗的數據

                Map<String, Object> errorMap = new HashMap<>();
                errorMap.put("sheetIndex", sheetIndex);
                errorMap.put("rowIndex", rowIndex);
                errorMap.put("errorFields", errorFields);

                errorList.add(errorMap);
              }
            });

    // TODO: 執行successList的入庫操作。

    HashMap<String, Object> resultMap = new HashMap<>();
    resultMap.put("data", successList);
    resultMap.put("haveError", !CollectionUtil.isEmpty(errorList));
    resultMap.put("error", errorList);
    return ResponseEntity.ok(resultMap);
  }

image-20211026231711468


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM