一、.xls和.xlsx兩種字尾的差別
excel文檔有.xls和.xlsx兩種字尾,那麼這兩種字尾有什麼差別呢?
1.1 檔案核心結構不同
xls 是一個特有的二進制格式,其核心結構是複合文檔類型的結構。
xlsx 的核心結構是 XML 類型的結構,并且基于XML進行壓縮(占用空間更小),是以也可以看做zip檔案,将一個“.xlsx”檔案的字尾改為ZIP後,用解壓軟體解壓,可以看到裡面有一個xml檔案和檔案的主要内容。
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLicmbw5SOhRDZxUzNyMjNiRzMhJjMxAjMilTM3YGOjRWO0MjZ58CX0JXZ252bj91Ztl2Lc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
1.2 版本不同
xls是excel2003及以前版本所生成的檔案格式
xlsx是excel2007及以後版本所生成的檔案格式
(excel 2007之後版本可以打開上述兩種格式,但是excel2013隻能打開xls格式)
1.3 最大行列的數量不同
xls最大隻有65536行、256列
xlsx可以有1048576行、16384列
二、java中對Excel檔案的解析
2.1 引入所需的jar包
java解析問Excel需要使用poi,在pom檔案中引入下面兩個poi的jar包(因為現在手中項目用的是3.15版本,我就按照這個版本來寫啦。新版本4.0+的和這個版本會有一些沖突,有些方法變化了)
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poiartifactId>
<version>3.15version>
dependency>
<dependency>
<groupId>org.apache.poigroupId>
<artifactId>poi-ooxmlartifactId>
<version>3.15version>
dependency>
那麼這兩個jar包都是做什麼用的呢?
解析.xls類型的檔案需要用到HSSFWorkbook類,這個類就在poi的jar包内;
解析.xlsx類型的檔案需要用到XSSFWorkbook類,這個類就在poi-ooxml的jar包内。
2.2常用的api
org.apache.poi.ss.usermodel.Workbook : 對應Excel文檔;
org.apache.poi.hssf.usermodel.HSSFWorkbook : 對應xls格式的Excel文檔;
org.apache.poi.xssf.usermodel.XSSFWorkbook : 對應xlsx格式的Excel文檔;
org.apache.poi.ss.usermodel.Sheet : 對應Excel文檔中的一個sheet;
org.apache.poi.ss.usermodel.Row : 對應一個sheet中的一行;
org.apache.poi.ss.usermodel.Cell : 對應一個單元格。
2.3單元格類型介紹
cell.getCellType()可以擷取單元格的類型,傳回值為int,類型如下:
CELL_TYPE_NUMERIC:數值型
當type是NUMERIC的時候,又分為數字格式和日期格式。使用org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(Cell cell),能夠判斷是否為日期格式。
CELL_TYPE_STRING:字元串型
cell.getRichStringCellValue():以XSSFRichTextString形式擷取單元格的值。對于數字單元格,我們抛出異常。對于空白單元格,我們傳回一個空字元串。對于公式單元格,如果傳回字元串,則傳回預先計算的值,否則傳回異常。
cell.getStringCellValue():以字元串形式擷取單元格的值。對于數字單元格,我們抛出異常。對于空白單元格,我們傳回一個空字元串。對于不是字元串公式的FormulaCells,我們抛出異常。
CELL_TYPE_BOOLEAN:布爾型
cell.getBooleanCellValue():以布爾值形式擷取單元格的值。對于字元串,數字和錯誤,我們抛出異常。對于空白單元格,我們傳回false。
CELL_TYPE_FORMULA:公式型
cell.getCellFormula():傳回單元格的公式。
CELL_TYPE_BLANK:空值型
CELL_TYPE_ERROR:錯誤型
2.4準備要解析的文檔
第三個sheet沒有值
2.5解析這個表格:ExcelUtils
這個Util主要做的是解析文檔,輸出單元格中的内容。
package com.xuexue.firstproject.utils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ExcelUtils {
public static void main(String[] args) {
Workbook workbook = readExcel("F:\\work\\other\\測試xls文檔解析.xls");
//Workbook workbook = readExcel("F:\\work\\other\\測試xlsx文檔解析.xlsx");
//獲得sheet的數量(sheet的index是從0開始的)
int sheetCount = workbook.getNumberOfSheets();
System.out.println("文檔一共有"+sheetCount+"個Sheet");
//周遊Sheet
for(int i = 0;i < sheetCount;i++){
System.out.println("開始周遊第"+i+"個sheet_________________________________________________________________________");
//得到Sheet
Sheet sheet = workbook.getSheetAt(i);
//得到每個Sheet的行數,此工作表中包含的最後一行(Row的index是從0開始的)
int rowCount = sheet.getLastRowNum();
System.out.println("第"+i+"個sheet中一共有"+rowCount+"行");
//周遊Row
for(int j = 0 ;j <= rowCount;j++){
System.out.println("開始周遊第"+j+"行~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
//得到Row
Row row = sheet.getRow(j);
if(row == null){
System.out.println("第"+j+"行為空,跳出本行");
continue;
}
//得到每個Row的單元格數
int cellCount = row.getLastCellNum();
System.out.println("第"+i+"個sheet中的第"+j+"行有"+cellCount+"個單元格");
for(int k = 0 ;k < cellCount;k++){
System.out.println("開始周遊第"+k+"個單元格+++++++");
Cell cell = row.getCell(k);
//輸出單元格裡的值
System.out.println(getCellFormatValue(cell));
}
System.out.println("第"+j+"行周遊結束");
}
System.out.println("第"+i+"個sheet周遊結束");
}
}
public static Workbook readExcel(String filePath){
if(filePath == null || filePath.equals("")){
return null;
}
//得到檔案字尾
String suffix = filePath.substring(filePath.lastIndexOf("."));
System.out.println(suffix);
try {
InputStream is = new FileInputStream(filePath);
if(".xls".equals(suffix)){
System.out.println("檔案類型是.xls");
return new HSSFWorkbook(is);
}
if(".xlsx".equals(suffix)){
System.out.println("檔案類型是.xlsx");
return new XSSFWorkbook(is);
}
return null;
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("檔案沒有找到");
} catch (IOException e) {
e.printStackTrace();
System.out.println("發生io異常");
}
return null;
}
public static Object getCellFormatValue(Cell cell){
Object cellValue;
if(cell!=null){
//判斷cell類型
switch(cell.getCellType()){
//空值單元格
case Cell.CELL_TYPE_BLANK:{
System.out.println("空單元格");
cellValue = "";
break;
}
//數值型單元格 getNumericCellValue()以數字形式擷取單元格的值。
case Cell.CELL_TYPE_NUMERIC:{
//判斷cell是否為日期格式
if(DateUtil.isCellDateFormatted(cell)){
System.out.println("日期格式");
//轉換為日期格式YYYY-mm-dd
//cellValue = cell.getDateCellValue();
Date date = cell.getDateCellValue();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
cellValue = dateFormat.format(date);
}else{
//數字
System.out.println("數字格式");
cellValue = cell.getNumericCellValue();
}
break;
}
//公式型單元格getCellFormula()傳回單元格的公式
case Cell.CELL_TYPE_FORMULA:{
System.out.println("公式型");
System.out.println(cell.getCellFormula());
cellValue = String.valueOf(cell.getNumericCellValue());
break;
}
//字元串單元格
case Cell.CELL_TYPE_STRING:{
System.out.println("字元串格式");
cellValue = cell.getRichStringCellValue().getString();
break;
}
//布爾值型單元格
case Cell.CELL_TYPE_BOOLEAN:{
System.out.println("布爾值");
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
}
default:
cellValue = "";
}
}else{
cellValue = "";
}
return cellValue;
}
}
2.6 一些其他方法簡記
workbook.getNumberOfSheets():擷取工作簿中的電子表格數量。傳回int類型的值。
workbook.getSheetAt(int index):擷取給定索引處的Sheet對象。傳回Sheet對象。
workbook.getActiveSheetIndex():友善的方法來擷取活動表。活動工作表是在Excel中檢視工作簿時目前顯示的工作表。“標明”工作表是一個獨特的概念。
sheet.getLastRowNum():擷取工作表的最後一行。(從0開始)
sheet.getRow(int rownum):根據傳參數值獲得行對象。
row.getLastCellNum():擷取一行中最後一個單元格。(從0開始)
row.getCell(k):根據傳參數值獲得單元格對象。
這篇文章隻是一個簡單的使用,會有一些小bug,更多的内容,還要檢視poi的官方網站。