码迷,mamicode.com
首页 > 其他好文 > 详细

excel 和 csv 读写DEMO

时间:2021-05-24 10:39:35      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:inpu   user   code   http   red   book   throws   array   val   

CSV文件读取

javacsv

读取

  1. 引入依赖
        <!-- https://mvnrepository.com/artifact/net.sourceforge.javacsv/javacsv -->
        <dependency>
            <groupId>net.sourceforge.javacsv</groupId>
            <artifactId>javacsv</artifactId>
            <version>2.0</version>
        </dependency>
  1. 读取代码
    private void readCsv(File file) throws IOException {
        BufferedInputStream bufferedInputStream = null;
        CsvReader reader = null;
        try {
            bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
            reader = new CsvReader(bufferedInputStream, ‘,‘, Charset.forName("GBK"));
            while (reader.readRecord()) {
                List<String> strings = Arrays.asList(reader.getValues());
                System.out.println(strings);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (bufferedInputStream != null) {
                bufferedInputStream.close();
            }
        }
    }

写入

    private void writeCsv(String[] strs) throws IOException {
        String property = System.getProperty("user.dir");
        File file = new File(property + "/result/commnet.csv");
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        CsvWriter csvWriter = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
            csvWriter = new CsvWriter(bufferedOutputStream, ‘,‘, Charset.forName("GBK"));
            for (String str : strs) {
                csvWriter.writeRecord(strs);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (csvWriter != null) {
                csvWriter.close();
            }
            if (bufferedOutputStream != null) {
                bufferedOutputStream.close();
            }
        }
    }

Excel文件读取

POI

POI中读取与excel不同之处:

  1. 使用excel打开的文件行列号是从1开始的,在程序中是从0开始的;
  2. POI使用遍历时一般使用getLastRowNum()获取最后一行行号,注意,这里获取到的是最后一行的行号,而不是总行数,所以遍历要使用<=,不然会漏掉最后一行

读取

  1. 引入依赖
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.0.0</version>
        </dependency>
  1. 读取代码
    private void readAllLine(File file) {
        try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
             XSSFWorkbook sheets = new XSSFWorkbook(bufferedInputStream)
        ) {
            XSSFSheet sheetA = sheets.getSheetAt(0);
            // 注意这里获取到的是最后一行的行号,而不是总行数,所以遍历要使用<=,不然会漏掉最后一行
            int lastRowNum = sheetA.getLastRowNum();
            for (int i = 0; i <= lastRowNum; i++) {
                XSSFRow row = sheetA.getRow(i);
                XSSFCell cell = row.getCell(0);
                String stringCellValue = cell.getStringCellValue();
                System.out.println("the row of " + i + " : " + stringCellValue);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

写入(不过单纯用于统计导出数据使用csv可能更好点,使用excel主要用于做报表)

  1. 写入代码
    /**
     * 写入excel
     * @param strs 待写入数据
     */
    private void writeArgs(String[] strs){
        File file = new File(System.getProperty("user.dir") + "/result/abc.xlsx");
        if (file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
        try (
                BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
                XSSFWorkbook sheets = new XSSFWorkbook();
                ){
            XSSFSheet sheetName = sheets.createSheet("sheetName");
            for (int i = 0; i < strs.length; i++) {
                XSSFRow row = sheetName.createRow(i);
                row.createCell(0).setCellValue(strs[i]);
            }
            sheets.write(bufferedOutputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Hutool

读取

写入

excel 和 csv 读写DEMO

标签:inpu   user   code   http   red   book   throws   array   val   

原文地址:https://www.cnblogs.com/xiaojiluben/p/14771766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!