ApachePOI, generate Excel reports via java code

ApachePOI Introduction

Statement of needs

In the enterprise application development, Excel report is one of the most common reporting needs. Excel report development is generally divided into two forms:

1, in order to facilitate operations, bulk upload of Excel-based reports

2, Excel reports generated by the java code.

Excel common operational tool

Java common way used to operate Excl generally have two kinds: JXL and POI.

  • JXL Excel can only operate belong to the older framework, it only supports the version of Excel 95-2000. Now we have stopped updating and maintenance.

  • POI is an apache project, to be Microsoft Word Excel, Ppt operate, including office2003 and 2007, Excl2003 and 2007. poi now it has been updated. So now the mainstream use POI.

What is the POI

Apache POI is the Apache Software Foundation open source projects, cross-platform Java prepared by the free open-source Java API, Apache POI API provides functionality to the Java language operation of Microsoft Office.

Apache POI is the most popular operating API component of Microsoft Office, with the POI can easily complete such as: data report generation, data bulk upload, data backup and so on.

 

 ApachePOI Getting Case

step

  1. Create a project poi_demo

  2. Add dependent

  3. Write test classes and achieve excel export, import excel

 

1. Create a project poi_demo

 

 

2. Add dependence

<?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. Write a test class, achieve export excel, excel import

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.
The Cell Cell row.createCell = (0);
cell.setCellValue ( "manufacturer");
the Cell Cell2 row.createCell = (. 1);
cell2.setCellValue ( "NO");
the Cell row.createCell the cell 3 = (2);
cell3.setCellValue ( "number");
wb.write (new new a FileOutputStream ( "D: /test.xlsx"));
}

@ // read the Test Excel
public void TestRead () throws IOException {
//. 1 . workbook
workbook wb = new new XSSFWorkbook (new new FileInputStream ( "d: /test.xlsx"));
// 2 Worksheet
Sheet sheet = wb.getSheet ( "the list of goods");
// 3 row
row row = sheet.getRow (0);
.. 4 // column
System.out.println(row.getCell(0).getStringCellValue());
System.out.println(row.getCell(1).getStringCellValue());
System.out.println(row.getCell(2).getStringCellValue());

}
}

  

 

Guess you like

Origin www.cnblogs.com/DispatcherServlet/p/11543108.html