使用POI操作excel(读取和创建)

POI提供API给Java程序对Microsoft Office格式档案读和写的功能。 
POI可以操作的文档格式有excel,word,powerpoint等,在此介绍POI操作excel的步骤,关于POI操作其他文档的步骤,大家可以参考其他资料。 
首先需要去apache poi的官网上下载poi开发包。我这里使用的版本是3.17
需要引入的依赖为:

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17-beta1</version>
</dependency>

代码如下

package com.soukenan.util.filter; 

import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 

import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.ss.usermodel.Sheet; 
import org.apache.poi.ss.usermodel.Workbook; 
import org.apache.poi.ss.usermodel.WorkbookFactory; 

public class POITest { 
    //使用POI创建excel工作簿 
    public static void createWorkBook() throws IOException { 
        //创建excel工作簿 
        Workbook wb = new HSSFWorkbook(); 
        //创建第一个sheet(页),命名为 new sheet 
        Sheet sheet = wb.createSheet("new sheet"); 
        //Row 行 
        //Cell 方格 
        // Row 和 Cell 都是从0开始计数的 

        // 创建一行,在页sheet上 
        Row row = sheet.createRow((short) 0); 
        // 在row行上创建一个方格 
        Cell cell = row.createCell(0); 
        //设置方格的显示 
        cell.setCellValue(1); 

        // Or do it on one line. 
        row.createCell(1).setCellValue(1.2); 
        row.createCell(2).setCellValue("This is a string 速度反馈链接"); 
        row.createCell(3).setCellValue(true); 

        //创建一个文件 命名为workbook.xls 
        FileOutputStream fileOut = new FileOutputStream("workbook.xls"); 
        // 把上面创建的工作簿输出到文件中 
        wb.write(fileOut); 
        //关闭输出流 
        fileOut.close(); 
    } 

    //使用POI读入excel工作簿文件 
    public static void readWorkBook() throws Exception { 
        // poi读取excel 
        //创建要读入的文件的输入流 
        InputStream inp = new FileInputStream("workbook.xls"); 

        //根据上述创建的输入流 创建工作簿对象 
        Workbook wb = WorkbookFactory.create(inp); 
        //得到第一页 sheet 
        //页Sheet是从0开始索引的 
        Sheet sheet = wb.getSheetAt(0); 
        //利用foreach循环 遍历sheet中的所有行 
        for (Row row : sheet) { 
            //遍历row中的所有方格 
            for (Cell cell : row) { 
                //输出方格中的内容,以空格间隔 
                System.out.print(cell.toString() + "  "); 
            } 
            //每一个行输出之后换行 
            System.out.println(); 
        } 
        //关闭输入流 
        inp.close(); 
    } 

    public static void main(String[] args) throws Exception { 
        // POITest.createWorkBook(); 
        POITest.readWorkBook(); 
    } 
} 



@Test
    public void poifirst() throws IOException {

        HSSFWorkbook wk = new HSSFWorkbook();
        HSSFSheet sheet = wk.createSheet("成绩表");
        HSSFRow row1 = sheet.createRow(0);
        HSSFCell cell = row1.createCell(0);
        cell.setCellValue("学生成绩表");
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 2));

        HSSFRow row2 = sheet.createRow(1);
        row2.createCell(0).setCellValue("学生编号");
        row2.createCell(0).setCellValue("学生姓名");
        row2.createCell(0).setCellValue("学生年龄");

        HSSFRow row3 = sheet.createRow(2);
        row3.createCell(0).setCellValue("1");
        row3.createCell(1).setCellValue("张三");
        row3.createCell(2).setCellValue("18");

        FileOutputStream outputStream = new FileOutputStream("e:\\workbook.xls");
        wk.write(outputStream);
        outputStream.flush();


    }


    @Test
    public void poisecond() throws IOException {
        HSSFWorkbook wk = new HSSFWorkbook();
        HSSFSheet sheet = wk.createSheet("学生表");
        HSSFRow row = sheet.createRow(0);

        HSSFCell cell = row.createCell((short) 0);
        cell.setCellValue("学生编号");
        cell = row.createCell((short) 1);
        cell.setCellValue("学生姓名");
        cell = row.createCell((short) 2);
        cell.setCellValue("学生年龄");

        List<Student> list = new ArrayList<Student>();
        Student student = new Student();
        student.setId(1);
        student.setName("张三");
        student.setAge(18);

        Student student1 = new Student();
        student1.setId(2);
        student1.setName("李四");
        student1.setAge(20);

        list.add(student);
        list.add(student1);

        for (short i = 0; i < list.size(); i++) {
            row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(list.get(i).getId());
            row.createCell(1).setCellValue(list.get(i).getName());
            row.createCell(2).setCellValue(list.get(i).getAge());
        }

        FileOutputStream outputStream = new FileOutputStream("e:\\workbooks.xls");
        wk.write(outputStream);
        outputStream.flush();
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值