您的位置:首页 > 编程语言 > Java开发

Java 读取Excel(兼容两种格式)

2017-02-10 16:26 441 查看

本文将为您简单介绍java读取excel的方法

本文将为您简单介绍java读取excel的方法

你可能需要了解

你可能需要预先准备

接下来动手敲一敲

片尾留注

你可能需要了解

Java读写Excel的包是Apache POI,因此需要先获取POI的jar包,下载地址: [ 点我下载 ]

Excel2003 与Excel2007区别较大,最直观的就是两者的后缀不同,前者后缀为.xls,后者为.xlsx

读取“.xls”格式使用 import org.apache.poi.hssf.usermodel.*;包的内容,例如:HSSFWorkbook

读取“.xlsx”格式使用 import org.apache.poi.xssf.usermodel.*; 包的内容,例如:XSSFWorkbook

读取两种格式使用 import org.apache.poi.ss.usermodel.* 包的内容,例如:Workbook

你可能需要预先准备

新建JavaProject->新建lib文件夹->导入以下jar包



接下来动手敲一敲

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
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.apache.xmlbeans.impl.piccolo.io.FileFormatException;

public class ExcelReadTest {

private static final String EXTENSION_XLS = "xls";
private static final String EXTENSION_XLSX = "xlsx";

public static void main(String[] args) {
ExcelReadTest excelReadTest = new ExcelReadTest();
try {
//根据实际情况加载excel文件
//excelReadTest.readExcel("test.xls");
excelReadTest.readExcel("test.xlsx");
} catch (FileNotFoundException | FileFormatException e) {
e.printStackTrace();
}
}

/**
* 获取workbook对象(xls和xlsx对象不同,不过都是Workbook的实现类)
* @param filePath
* @return
* @throws IOException
*/
private Workbook getWorkbook(String filePath) throws IOException{
Workbook workbook = null;
InputStream iStream = new FileInputStream(filePath);
if(filePath.endsWith(EXTENSION_XLS)){
workbook = new HSSFWorkbook(iStream);
}else if(filePath.endsWith(EXTENSION_XLSX)){
workbook = new XSSFWorkbook(iStream);
}
return workbook;
}

/**
* 文件检查
* @param filePath
* @throws FileNotFoundException
* @throws FileFormatException
*/
private void preReadCheck(String filePath) throws FileNotFoundException, FileFormatException{
File file = new File(filePath);
if(!file.exists()){
throw new FileNotFoundException("传入的文件不存在:"+filePath);
}
if(!(filePath.endsWith(EXTENSION_XLS) || filePath.endsWith(EXTENSION_XLSX))){
throw new FileFormatException("传入的文件不是Excel");
}
}

public void readExcel(String filePath) throws FileNotFoundException, FileFormatException {
//1.检查文件
this.preReadCheck(filePath);
//2.获取workbook对象
Workbook workbook = null;
try {
workbook = this.getWorkbook(filePath);
//读文件
int sheetCount = workbook.getNumberOfSheets();//获取sheet数量
for(int i = 0;i<sheetCount;i++){
Sheet sheet = workbook.getSheetAt(i);
if(sheet==null){
continue;
}
int rowStart = sheet.getFirstRowNum();
int rowEnd = sheet.getLastRowNum();
Row row = null;
for(int j = rowStart ;j<rowEnd;j++){
row = sheet.getRow(j);
int cellStart = row.getFirstCellNum();
int cellEnd = row.getLastCellNum();
Cell cell = null;
for(int m = cellStart;m<cellEnd;m++){
4000

cell = row.getCell(m);
//获取单元格的值
System.out.print(this.getCellValue(cell, true)+"-");

}
System.out.println("");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

@SuppressWarnings("deprecation")
private String getCellValue(Cell cell,boolean treatAsStr){
if(cell==null){
return "";
}
if(treatAsStr){
//将其他格式转换为文本格式来读取
cell.setCellType(Cell.CELL_TYPE_STRING);
}
if(cell.getCellType()==Cell.CELL_TYPE_BOOLEAN){
return String.valueOf(cell.getBooleanCellValue());
}else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC){
return String.valueOf(cell.getNumericCellValue());
}else{
return String.valueOf(cell.getStringCellValue());
}
}
}


片尾留注

*本文部分引用了 幻影 的代码

*以上代码经个人实际测试有效,可以引用测试

*本篇文章的由来:

项目需求:“通过在表单提交时(POST方式)获取Excel文件并进行导入数据库操作”
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java apache poi excel