Java 读取Excel并转CSV格式

 <!-- csv文件解析依赖 -->
        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>5.4</version>
        </dependency>

        <!-- 大exce -->
        <dependency>
            <groupId>com.monitorjbl</groupId>
            <artifactId>xlsx-streamer</artifactId>
            <version>2.1.0</version>
        </dependency>
import org.apache.poi.ss.usermodel.*;

// 换行
    public final static String NEW_LINE = "\r\n";
    public static void convertXlsToCsv(String sourcePath, String targetPath,String splitSymbol){
        FileInputStream in = null;
        try {
            in = new FileInputStream(new File(sourcePath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Workbook wk = StreamingReader.builder()
                .rowCacheSize(100)
                .bufferSize(2048)
                .open(in);
        int sheetNums = wk.getNumberOfSheets();
        int count = 0;
        StringBuilder fileBuilder = new StringBuilder();

        for (int i = 0; i < sheetNums; i++) {
            StreamingSheet sheet = (StreamingSheet) wk.getSheetAt(i);
            // 遍历所有的行
            for (Row row : sheet) {
                // 遍历所有的列
                int size = ((StreamingRow) row).getCellMap().size();
                for (int col = 0; col < size; col++) {
                    StreamingCell cell = (StreamingCell) row.getCell(col);
                    String tmp = (col == size - 1) ? "" : splitSymbol;
                    if(cell == null){
                        fileBuilder.append(" "+ tmp);
                    }else {
                        fileBuilder.append(getFilterSpecitalChar(cell.getStringCellValue()) + tmp);
                    }
                }
                // 换行
                fileBuilder.append(NEW_LINE);
                count++;
            }
        }
        System.out.println("Count: " + count);
        String res = fileBuilder.toString();
        try {
            File targetFile = new File(targetPath);
            if(!targetFile.exists()){
                targetFile.createNewFile();
            }
            Files.write(res.getBytes(StandardCharsets.UTF_8), targetFile);
        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            try {
                if(in != null)in.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }


    /**
     * 过滤特殊字符
     * @param str
     * @return
     */
    public static String getFilterSpecitalChar(String str){
        String regEx="[^0-9a-zA-Z\\u4E00-\\u9FA5\\u002A]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.replaceAll("").trim();
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值