美文网首页
01 -java 读 / 写 excel

01 -java 读 / 写 excel

作者: 刘小刀tina | 来源:发表于2020-02-23 15:42 被阅读0次

1. pom.xml

     <!-- 日期时间工具-->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.10.1</version>
        </dependency>

        <!--读取excel文档数据依赖的包-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>


2. java 代码读excel文件 (针对07版本的excel)

 //对excel 进行读
    @Test
    public void TestRead07() throws IOException {
        //1 获取输出流
        String path="/Users/lvxiaokai/Desktop/1.xlsx";//指定文件的路径
        FileInputStream is = new FileInputStream(path);
        Workbook workbook = new XSSFWorkbook(is);
        Sheet sheet = workbook.getSheetAt(0);
        //读取第一行 第一列
        Row row = sheet.getRow(0);
        Cell cell = row.getCell(0);
        //输出单元内容
        System.out.println("输出单元内容");
        System.out.println(cell.getStringCellValue());
        //操作结束。关闭文件
        is.close();
    }

3. java 代码写 Excel 文件(针对07版本的excel)

  //对 Excel 进行写
    @Test
    public void TestWrite07() throws IOException {
       //1创建workbook
        XSSFWorkbook workbook = new XSSFWorkbook();
        //2 根据workbook创建sheet
        XSSFSheet sheet1 = workbook.createSheet("会员列表");
        //3 根据sheet创建row ->行
        XSSFRow row1 = sheet1.createRow(0);
        //4 根据行创建cell ->列
        XSSFCell cell1 = row1.createCell(0);
        //5 向cell里面设置值
        cell1.setCellValue("lucy");
        //6 使用输出流写到文件中
        String path ="/Users/lvxiaokai/Desktop/3.xlsx" ;//目标文件的本地路径
        FileOutputStream out = new FileOutputStream(path);
        //7 把workbook内容通过输出流写入到文件中
        workbook.write(out);
        //关闭流
        out.close();
        System.out.println("使用输出流写到文件中成功!");

    }

4. 针对03版本的excel pom.xml中的依赖jar包

<!-- 日期时间工具-->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.10.1</version>
        </dependency>

        <!--读取03版本excel文档数据依赖的包-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>

java 代码参考如下

 //对03版本excel 进行读
    @Test
    public void TestRead03() throws IOException {
        //1 获取输出流
        String path="/Users/lvxiaokai/Desktop/1.xls";//指定文件的路径
        FileInputStream is = new FileInputStream(path);
        HSSFWorkbook workbook = new HSSFWorkbook(is);
        Sheet sheet = workbook.getSheetAt(0);
        //读取第一行 第一列
        Row row = sheet.getRow(0);
        Cell cell = row.getCell(0);
        //输出单元内容
        System.out.println("输出单元内容");
        System.out.println(cell.getStringCellValue());
        //操作结束。关闭文件
        is.close();
    }

    //对对03版本 Excel 进行写
    @Test
    public void TestWrite03() throws IOException {
        //1创建workbook
        HSSFWorkbook workbook = new HSSFWorkbook();
        //2 根据workbook创建sheet
        HSSFSheet sheet1 = workbook.createSheet("会员列表");
        //3 根据sheet创建row ->行
        HSSFRow row1 = sheet1.createRow(0);
        //4 根据行创建cell ->列
        HSSFCell cell1 = row1.createCell(0);
        //5 向cell里面设置值
        cell1.setCellValue("lucy");
        //6 使用输出流写到文件中
        String path ="/Users/lvxiaokai/Desktop/3.xls" ;//目标文件的本地路径
        FileOutputStream out = new FileOutputStream(path);
        //7 把workbook内容通过输出流写入到文件中
        workbook.write(out);
        //关闭流
        out.close();
        System.out.println("使用输出流写到文件中成功!");

    }

相关文章

网友评论

      本文标题:01 -java 读 / 写 excel

      本文链接:https://www.haomeiwen.com/subject/ympjqhtx.html