Apache POI介绍 {#title-1}
Apache POI,是Apache的开源库,其提供了一系列的Java API,以便Java开发人员能够操作Microsoft Office文档,这其中就包括Excel。
Apache POI提供了用于读取和写入Excel文件的类库. 这些类库支持老版本的.xls和新版本的.xlsx文件格式。下面是一个基本的读取Excel文件的Apache POI代码例子:
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
public class ReadExcelWithApachePOI {
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream(new File("path to the file"));
Workbook workbook = new XSSFWorkbook(file);
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
System.out.println("Cell Value: " + cell.getStringCellValue());
workbook.close();
file.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
使用Apache POI读取Excel数据 {#title-2}
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.*;
public class Reader {
public static void main(String[] args) throws Exception {
try (FileInputStream fis = new FileInputStream(new File("Your Excel File"));
Workbook workbook = WorkbookFactory.create(fis)) {
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
String cellValue = null;
switch (cell.getCellType()) {
case STRING:
cellValue = cell.getStringCellValue();
break;
case NUMERIC:
cellValue = String.valueOf(cell.getNumericCellValue());
break;
case BOOLEAN:
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
default:
break;
}
System.out.print(cellValue + "\t");
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
处理Excel中的日期和公式 {#title-3}
Apache POI 不仅可以读取文本和数字,还可以处理Excel中的日期和公式。
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.*;
public class Reader {
public static void main(String[] args) throws Exception {
try (FileInputStream fis = new FileInputStream(new File("Your Excel File"));
Workbook workbook = WorkbookFactory.create(fis)) {
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
String cellValue = null;
switch (cell.getCellType()) {
case STRING:
cellValue = cell.getStringCellValue();
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
cellValue = cell.getDateCellValue().toString();
} else {
cellValue = String.valueOf(cell.getNumericCellValue());
}
break;
case BOOLEAN:
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
default:
break;
}
System.out.print(cellValue + "\t");
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
处理Excel中的复杂数据类型 {#title-4}
有时可能需要处理的Excel文件含有复杂的数据类型,如下的代码片段演示了如何处理公式:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
public class Database {
public static void main(String[] args) throws Exception {
try (FileInputStream fis = new FileInputStream(new File("Your Excel File"));
Workbook workbook = WorkbookFactory.create(fis)) {
FormulaEvaluator evaluator = new XSSFFormulaEvaluator((XSSFWorkbook) workbook);
Sheet sheet = workbook.getSheetAt(0);
for (Row r : sheet) {
for (Cell c : r) {
if (c.getCellType() == CellType.FORMULA) {
evaluator.evaluateFormulaCellEnum(c);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
这样,Apache POI 便能够对Excel 文件进行全面的读取和处理。