csv導入導出組件jcsv

jcsv

介紹

jcsv一個簡單的、輕量級的csv導入、導出庫,相對於opencsv與javacsv,jcsv側重於導入導出,包括導入校驗、導出模板等。 源代碼地址:https://gitee.com/lpxs/jcsv.gitjava

使用說明

jcsv如今只支持集成到springboot工程中git

依賴庫

<dependency>
  <groupId>com.github.lipengxs.jcsv</groupId>
  <artifactId>jcsv-spring-boot-starter</artifactId>
  <version>1.0.0</version>
</dependency>

配置說明

csv-config:
  exportc:
    - id: aa
      compress:
        enabled: true
        file-size: 10000
        type: zip
      template: /template/aa.csv

  importc:
    - id: throng-0
      desc : "上傳member_id"
      max-size: 30 #單位m
      start-row: 2
      separator: ","
      valicate:
        - { col: 0, name: member_id,validateRegex: "^\\d{1,10}$", hint: "請填寫10位之內的數字",required: true}
    - id: throng-1
      desc : "上傳email+語言+站點"
      max-size: 30 #單位m
      separator: ","
      start-row: 2
      valicate:
        - { col: 0, name: email,required: true,validateRegex: "^[éûàçùôîèíá¡N¿UóñúäöüßàèùåäöãáéàêçíşçöüğşàâäèéêëîïôœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇA-Za-z0-9_\\-\\.\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$", hint: "郵箱地址錯誤"}
        - { col: 1, name: language ,required: true,hint: "語言錯誤" ,td-id: 11}
        - { col: 2, name: site_id,required: true,hint: "站點錯誤" ,td-id: 13 }
    - id: throng-3
      desc: "上傳member_id+佔位符"
      max-size: 30 #單位m
      separator: ","
      start-row: 2
      check-column-size: false //該字段是表示不限制字段個數,沒有在valicate中配置不須要校驗
      valicate:
        - { col: 0, name: member_id,validateRegex: "^\\d{1,10}$", hint: "請填寫10位之內的數字",required: true}
    - id: throng-4
      desc: "上傳email"
      max-size: 30 #單位m
      start-row: 2
      separator: ","
      valicate:
        - { col: 0, name: email,required: true,validateRegex: "^[éûàçùôîèíá¡N¿UóñúäöüßàèùåäöãáéàêçíşçöüğşàâäèéêëîïôœùûüÿçÀÂÄÈÉÊËÎÏÔŒÙÛÜŸÇA-Za-z0-9_\\-\\.\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$", hint: "郵箱地址錯誤"}

導入

@Autowize
private CsvContext csvContext;
public BaseDataAPI upload(HttpServletRequest request,
                                        @RequestParam("file") MultipartFile file){
try {
            if(file==null){
                return D.error("請上傳文件");
            }
           
            //解析模板內容,對比模板內容是否和上傳的同樣外層須要抓取異常由於錯誤信息是以異常來進行鑑別
            List<Map<String,Object>> list= csvContext.transfer(file,"throng-"+uploadType,product);
            
            return D.ok(hash);
        } catch (CsvImportException e) {
            return D.error(e.getMessage());
        } catch (Exception e) {
            return D.error(e.getMessage());
        }
 }

導出

@Autowize
private CsvContext csvContext;
public BaseDataAPI export(){

         、try {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } catch (UnsupportedEncodingException var3) {
                var3.printStackTrace();
            }
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM.toString());
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
            FileCopyUtils.copy(csvContext.export(id,data), response.getOutputStream());
        } catch (Exception var4) {
            var4.printStackTrace();
        }
 }
相關文章
相關標籤/搜索