Java代码如何读取Excel文件呢?
下文笔者讲述java代码读取excel的方法及示例分享,如下所示
借助Apache POI框架
即可读取和操作excel
Apache POI简介
Apache POI
是Apache的开源库
其提供一系列Java API
可操作Microsoft Office文档
例:读取excel
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数据
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时----操作日期和公式
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中复杂数据类型
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();
}
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


