ApachePOI,經過java代碼生成Excel報表

ApachePOI 介紹

需求說明

在企業級應用開發中,Excel報表是一種最多見的報表需求。Excel報表開發通常分爲兩種形式:java

一、爲了方便操做,基於Excel的報表批量上傳數據apache

二、經過java代碼生成Excel報表。框架

常見的Excel操做工具

Java中常見的用來操做Excl的方式通常有2種:JXL和POI。xss

  • JXL只能對Excel進行操做,屬於比較老的框架,它只支持到Excel 95-2000的版本。如今已經中止更新和維護。maven

  • POI是apache的項目,可對微軟的Word,Excel,Ppt進行操做,包括office2003和2007,Excl2003和2007。poi如今一直有更新。因此如今主流使用POI。工具

什麼是POI

Apache POI是Apache軟件基金會的開源項目,由Java編寫的免費開源的跨平臺的 Java API,Apache POI提供API給Java語言操做Microsoft Office的功能。測試

Apache POI是目前最流行的操做Microsoft Office的API組件,藉助POI能夠方便的完成諸如:數據報表生成,數據批量上傳,數據備份等工做。spa

 

 ApachePOI 入門案例

步驟

  1. 建立項目poi_demoexcel

  2. 添加依賴code

  3. 編寫測試類,實現導出excel、導入excel

 

1.建立項目poi_demo

 

 

2.添加依賴

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  <modelVersion>4.0.0</modelVersion>  
  <groupId>com.yk</groupId>  
  <artifactId>poi_demo</artifactId>  
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <dependencies>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>4.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>4.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml-schemas</artifactId>
      <version>4.0.1</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
  </dependencies>
</project>

3.編寫測試類,實現導出excel、導入excel

package com.yk.test;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.xssf.usermodel.XSSFWorkbook;import org.junit.Test;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class TestPoi {    @Test//導出Excel    public void TestWrite() throws IOException {        //1.工做簿        Workbook wb = new XSSFWorkbook();        //2.工做單        Sheet sheet = wb.createSheet("貨物清單");        //3.行        Row row = sheet.createRow(0);        //4.列        Cell cell = row.createCell(0);        cell.setCellValue("生產廠家");        Cell cell2 = row.createCell(1);        cell2.setCellValue("貨號");        Cell cell3 = row.createCell(2);        cell3.setCellValue("數量");        wb.write(new FileOutputStream("d:/test.xlsx"));    }    @Test//讀取Excel    public void TestRead() throws IOException {        //1.工做簿        Workbook wb = new XSSFWorkbook(new FileInputStream("d:/test.xlsx"));        //2.工做單        Sheet sheet = wb.getSheet("貨物清單");        //3.行        Row row = sheet.getRow(0);        //4.列        System.out.println(row.getCell(0).getStringCellValue());        System.out.println(row.getCell(1).getStringCellValue());        System.out.println(row.getCell(2).getStringCellValue());    }}
相關文章
相關標籤/搜索