目录
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
功能 | 描述 |
---|---|
HSSFWorkBook | 提供读写Microsoft Excel格式档案的功能,xls文档 |
XSSFWorkBook | 提供读写Microsoft Excel OOXML格式档案的功能,xlsx文件 |
HWPF | 提供读写Microsoft Word格式档案的功能 |
HSLF | 提供读写Microsoft PowerPoint格式档案的功能 |
HDGF | 提供读写Microsoft Visio格式档案的功能 |
Apache POI - the Java API for Microsoft Documents
创建项目:
导入pom.xml依赖
- <dependencies>
-
- <dependency>
- <groupId>org.apache.poigroupId>
- <artifactId>poiartifactId>
- <version>3.9version>
- dependency>
-
- <dependency>
- <groupId>org.apache.poigroupId>
- <artifactId>poi-ooxmlartifactId>
- <version>3.9version>
- dependency>
-
- <dependency>
- <groupId>joda-timegroupId>
- <artifactId>joda-timeartifactId>
- <version>2.10.1version>
- dependency>
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
- dependency>
- dependencies>
在2003版本中excel文件拓展名为xls
名词:
工作薄:一个excel文件,就是一个工作薄
工作表:一个工作薄中,可以所有多个工作表Sheet
行:每一个工作表,包含多行row
单元格:每行有多个单元格Cell组成
文件存放位置方法:
- public String path(){
- String path = getClass().getResource("/").getPath();
- return path;
- }
注意:
Workbook workbook = new HSSFWorkbook();
进行POI技术生成Excel文件,方法:
- @Test
- //测试写入Excel文件以xls为后缀结尾的
- public void testWritexls() throws IOException {
- //需求:向:xls文档写数据
- //创建新的Execl工作薄
- Workbook workbook = new HSSFWorkbook();
- //在Excel文件中创建一个工作表,表头是爱吃豆的土豆
- Sheet sheet = workbook.createSheet("爱吃豆的土豆");
- for (int r = 0; r < 5; r++) {
- //在工作表中创建五行
- Row row = sheet.createRow(r);
- for (int i = 0; i < 10; i++) {
- //在工作表中创建十列 也就是单元格
- Cell cell = row.createCell(i);
- //在单元格中放入数据
- cell.setCellValue("土豆"+r+":"+i);
- }
- }
- //生成到Excel文件的存放位置
- String file = path()+"土豆.xls";
- //创建输出文件流
- FileOutputStream out = new FileOutputStream(file);
- //输出到工作薄中
- workbook.write(out);
- //进行关流
- out.close();
- System.out.println("写入成功");
- }
示例:
注意:
Workbook hssfWorkbook = new HSSFWorkbook(fileInputStream);
xls 2003 文件读操作
- //读xls
- @Test
- public void testReadxls() throws IOException {
- //确定输入文件流
- FileInputStream fileInputStream = new FileInputStream(path()+"土豆.xls");
- //工作薄
- Workbook hssfWorkbook = new HSSFWorkbook(fileInputStream);
-
- //获取要读文件的工作表的表名
- Sheet sheet = hssfWorkbook.getSheet("爱吃豆的土豆");
- //获得第一行索引
- int startRow = sheet.getFirstRowNum();
- //获得最后一行索引
- int endRow = sheet.getLastRowNum();
- //循环遍历方式进行读取
- for (int r =startRow; r <=endRow ; r++) {
- //读取每一行内容
- Row row = sheet.getRow(r);
- //获取每一行开始单元格和结尾单元格
- short startCell = row.getFirstCellNum();
- short endCell = row.getLastCellNum();
- //遍历每一个单元格
- for(int c = startCell ; c < endCell ; c++){
- Cell cell = row.getCell(c);
- //打印内容
- System.out.print(cell.getStringCellValue());
- System.out.print(",");
- }
- System.out.println();
- }
- }
注意:
Workbook workbook = new XSSFWorkbook();
- @Test
- public void testWriteXlsx() throws IOException {
- //需求:向:xlsx文档写数据
- //1创建工作 Workbook
- Workbook workbook = new XSSFWorkbook();
- Sheet sheet = workbook.createSheet("爱吃豆的土豆");
- for (int r = 0; r < 5; r++) {
- Row row = sheet.createRow(r);
- for (int i = 0; i < 10; i++) {
- Cell cell = row.createCell(i);
- cell.setCellValue("土豆"+r+":"+i);
- }
- }
- //
- String file = path()+"土豆xlsx.xlsx";
- FileOutputStream out = new FileOutputStream(file);
- workbook.write(out);
- //进行关流
- out.close();
- System.out.println("写入成功");
- }
注意:
Workbook workbook = new XSSFWorkbook(fileInputStream);
- //读xlsx类型
- @Test
- public void testReadxlsx() throws IOException {
- FileInputStream fileInputStream = new FileInputStream(path()+"土豆xlsx.xlsx");
- Workbook workbook = new XSSFWorkbook(fileInputStream);
- //获得工作表
- Sheet sheet = workbook.getSheet("爱吃豆的土豆");
- //获得行
- int startRow = sheet.getFirstRowNum();
- int endRow = sheet.getLastRowNum();
-
- for (int r =startRow; r <=endRow ; r++) {
- Row row = sheet.getRow(r);
-
- short startCell = row.getFirstCellNum();
- short endCell = row.getLastCellNum();
-
- for(int c = startCell ; c < endCell ; c++){
- Cell cell = row.getCell(c);
- //打印内容
- System.out.print(cell.getStringCellValue());
- System.out.print(",");
- }
- System.out.println();
- }
- }
- @Test
- public void testRead07() throws Exception{
-
- InputStream is = new FileInputStream("d:/0704.xlsx");
-
- Workbook workbook = new XSSFWorkbook(is);
- Sheet sheet = workbook.getSheetAt(0);
-
- // 读取第一行第一列
- Row row = sheet.getRow(0);
- Cell cell1 = row.getCell(0);
- Cell cell2 = row.getCell(1);
-
-
- // 输出单元内容
- System.out.println(cell1.getStringCellValue());
- System.out.println(cell2.getNumericCellValue());
-
- // 操作结束,关闭文件
- is.close();
- }