java poi判断excel是xlsx还是xls类型
作者:java_丫丫程序媛
这篇文章主要为大家详细介绍了如何利用java poi来判断excel是xlsx还是xls类型,文中的示例代码讲解详细,有需要的小伙伴可以参考一下
java poi判断excel是xlsx还是xls
依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.5</version>
</dependency>
代码
package com.test.excel;
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 java.io.File;
import java.nio.file.Files;
public class ExcelRateOneTest {
public static void main(String[] args) {
File file = new File("D:\\年轻干部人才库名册.xls");
try {
Workbook workbook = null;
try {
workbook = new XSSFWorkbook(Files.newInputStream(file.toPath()));
} catch (Exception e) {
System.out.println("该excel文件不是xlsx类型"+e.getMessage());
}
try {
if (null == workbook) {
workbook = new HSSFWorkbook(Files.newInputStream(file.toPath()));
}
} catch (Exception e) {
System.out.println("该文件不是excel文件"+e.getMessage());
}
if (workbook != null) {
Sheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
System.out.println("单元格内容:" + getCellValue(cell));
}
}catch (Exception e){
System.out.println(e.getMessage());
}
}
/**
* 根据单元格类型获取单元格内容,将内容转为字符串类型
* @param cell 单元格
* @return
*/
public static String getCellValue(Cell cell) {
if (cell == null) {
return "";
}
switch (cell.getCellType()) {
case STRING:
return cell.getStringCellValue();
case NUMERIC:
return cell.getNumericCellValue() + "";
case BOOLEAN:
return cell.getBooleanCellValue() + "";
case FORMULA:
return cell.getCellFormula() + "";
default:
return "";
}
}
}知识补充
除了上文的内容,小编还为大家整理了Java利用poi解析xlsx和xls文件的相关代码,希望对大家有所帮助
遇到过处理excel的问题,在网上找了很久,感觉他们的代码太乱太复杂,这是我精简版的excel处理代码,简单暴力。
首先,为什么使用poi?jxl只能处理03版之前的excel,也就是xls结尾的,不能处理xlsx。poi兼容两种格式,poi解析两种格式的文件时,唯一的不同就是xls时,为HSSF;xlsx时,为XSSF。
首先是处理Cell对象的小方法:
//这个方法对cell进行处理,传入cell对象,返回cell中内容,String类型。
public static String getCellFormatValue(Cell cell) {
String cellValue = "";
if (cell != null) {
// 判断cell类型
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC: {
cellValue = String.valueOf(cell.getNumericCellValue());
break;
}
case Cell.CELL_TYPE_STRING: {
cellValue = cell.getRichStringCellValue().getString();
break;
}
default:
cellValue = "";
}
}
return cellValue;
}
两种处理excel的代码:
xls时:
InputStream inputStream = new FileInputStream("绝对路径.xls");
HSSFWorkbook workbook = new HSSFWorkbook(inputStream);
HSSFSheet s = workbook.getSheetAt(0);
for (int j = 0; j < s.getPhysicalNumberOfRows(); j++) {//获取总行数
Row row = s.getRow(j); // 取出第i行 getRow(index) 获取第(j)行
for (int k = 0; k < row.getPhysicalNumberOfCells(); k++) { // getPhysicalNumberOfCells() 获取当前行的总列数
String value1 = getCellFormatValue(row.getCell(k));//取出第j行第k列的值
System.out.println(value1);
}
}
workbook.close();
xlsx时:
InputStream inputStream = new FileInputStream("绝对路径.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
XSSFSheet s = workbook.getSheetAt(0);
for (int j = 0; j < s.getPhysicalNumberOfRows(); j++) {//获取总行数
Row row = s.getRow(j); // 取出第i行 getRow(index) 获取第(j)行
for (int k = 0; k < row.getPhysicalNumberOfCells(); k++) { // getPhysicalNumberOfCells() 获取当前行的总列数
String value1 = getCellFormatValue(row.getCell(k));//取出第j行第k列的值
System.out.println(value1);
}
}
workbook.close();
以上就是java poi判断excel是xlsx还是xls类型的详细内容,更多关于java判断excel是xlsx还是xls的资料请关注脚本之家其它相关文章!
