zoukankan      html  css  js  c++  java
  • excel 和 csv 读写DEMO

    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

    读取

    写入

  • 相关阅读:
    第15届创新英语大赛初赛第二阶段题目
    13.递归第一次
    13.递归第一次
    13.递归第一次
    13.递归第一次
    Algs4-1.3.19给出一段代码,删除链表的尾结点
    Algs4-1.3.18下面链表在代码的作用
    Algs4-1.3.17从文件中读取信息生成Transaction对象数组
    Algs4-1.3.16从文件中读取日期生成日期对象数组
    Algs4-1.3.15打印Queue中倒数第k个元素的值
  • 原文地址:https://www.cnblogs.com/xiaojiluben/p/14771766.html
Copyright © 2011-2022 走看看