寫入 Excel 文件
// 寫入 Excel 文件
// ============================================================================== //
Workbook wb = new HSSFWorkbook(); // or Workbook wb = new XSSFWorkbook();
// Create a sheet.
Sheet sheet = wb.createSheet("The Name of the Sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow(0);
// Create a cell and put a value in it.
Cell cell = row.createCell(0);
cell.setCellValue("Let me have a try!");
// Or do it on one line.
row.createCell(1).setCellValue("Let me have a try!");
/*
備註:
try (...) {
...
}
上述的表達可以在全部語句執行完以後自動釋放括號中的資源。
*/
try (OutputStream fileOut = new FileOutputStream("workbook.xls")) { // or workbook.xlsx
wb.write(fileOut);
} catch (FileNotFoundException e) {
System.out.println("The path is not correct!!!");
System.exit(-1);
} catch (IOException e) {
e.printStackTrace();
}
讀取 Excel 文件
// 讀取 Excel 文件到 Workbook 對象中
// ============================================================================== //
/*
使用 File 對象比使用 InputStream 對象所需的內存資源更少,由於使用 InputStream 對象
須要將整個文件緩存到內存中。
*/
// Use a file
try {
Workbook wb = WorkbookFactory.create(new File("MyExcel.xls")); // or MyExcel.xlsx
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
// Use an InputStream, needs more memory
try (InputStream in = new FileInputStream("MyExcel.xls")) { // or MyExcel.xlsx
Workbook wb = WorkbookFactory.create(in);
} catch (FileNotFoundException e) {
System.out.println("The file does not exist or the path is not correct!!!");
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
遍歷 Excel 文件
// 遍歷 Excel
// ============================================================================== //
try {
Workbook wb = WorkbookFactory.create(new File("MyExcel.xls")); // or MyExcel.xlsx
for (Sheet sheet : wb) {
for (Row row : sheet) {
for (Cell cell : row) {
// pass
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
須要的 maven 依賴
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
完整代碼
package ai.labrador.poi.excel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import java.io.*;
/**
* 學習使用 HSSF 或者 XSSF 讀寫 Excel 文件
* Reference:
* [Busy Developers' Guide to HSSF and XSSF Features](http://poi.apache.org/spreadsheet/quick-guide.html#NewWorkbook)
*
* @author Genpeng Xu
* @date 2018/06/05
*/
public class LearnHSSFAndXSSF {
public static void main(String[] args) {
// 寫入 Excel 文件
// ============================================================================== //
Workbook wb = new HSSFWorkbook(); // or Workbook wb = new XSSFWorkbook();
// Create a sheet.
Sheet sheet = wb.createSheet("The Name of the Sheet");
// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow(0);
// Create a cell and put a value in it.
Cell cell = row.createCell(0);
cell.setCellValue("Let me have a try!");
// Or do it on one line.
row.createCell(1).setCellValue("Let me have a try!");
/*
備註:
try (...) {
...
}
上述的表達可以在全部語句執行完以後自動釋放括號中的資源。
*/
try (OutputStream fileOut = new FileOutputStream("workbook.xls")) { // or workbook.xlsx
wb.write(fileOut);
} catch (FileNotFoundException e) {
System.out.println("The path is not correct!!!");
System.exit(-1);
} catch (IOException e) {
e.printStackTrace();
}
// 讀取 Excel 文件到 Workbook 對象中
// ============================================================================== //
/*
使用 File 對象比使用 InputStream 對象所需的內存資源更少,由於使用 InputStream 對象
須要將整個文件緩存到內存中。
*/
// Use a file
try {
Workbook wb = WorkbookFactory.create(new File("MyExcel.xls")); // or MyExcel.xlsx
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
// Use an InputStream, needs more memory
try (InputStream in = new FileInputStream("MyExcel.xls")) { // or MyExcel.xlsx
Workbook wb = WorkbookFactory.create(in);
} catch (FileNotFoundException e) {
System.out.println("The file does not exist or the path is not correct!!!");
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
// 遍歷 Excel
// ============================================================================== //
try {
Workbook wb = WorkbookFactory.create(new File("MyExcel.xls")); // or MyExcel.xlsx
for (Sheet sheet : wb) {
for (Row row : sheet) {
for (Cell cell : row) {
// pass
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidFormatException e) {
e.printStackTrace();
}
}
}