一直想着使用java操作excel,但有時各種原因一直沒有實現。由於工作無意間做了個其他demo,為了進一步發散就涉及到了使用excel,為此開始正式接觸POI,雖然限制不是很了解POI,但是通過查閱各種資料,現在終於實現了excel 的簡單讀取。以下是實現的代碼。並帶有詳細注釋,方便自己也方便他人。
package net.oschina.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ReadExcel {
public static void read(InputStream inputStream) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
/*
獲取單個sheet 頁,單個單元格的數據。
XSSFSheet sheet = workbook.getSheetAt(0);
XSSFRow row0 = sheet.getRow(0);
XSSFCell cell = row0.getCell(0);
System.out.println(cell.getRichStringCellValue().getString());*/
//獲取excel表格中的所有數據
//workbook.getNumberOfSheets(); 獲取sheet 頁個數。
int sheetNum = workbook.getNumberOfSheets();
//System.out.println(sheetNum);
//for循環遍歷單元格內容
for (int sheetIndex = 0; sheetIndex < sheetNum; sheetIndex++) {
//根據下標獲取sheet
XSSFSheet sheet = workbook.getSheetAt(sheetIndex);
//workbook.getSheetName(sheetIndex) 根據下標獲取sheet 名稱
System.out.println("sheet序號:"+sheetIndex+",sheet名稱:"+workbook.getSheetName(sheetIndex));
//循環該sheet頁中的有數據的每一行
//打印行號,某人起始是0 System.out.println(sheet.getLastRowNum());
//打印行數
System.out.println(sheet.getPhysicalNumberOfRows());
//遍歷每行內容從行號為0開始
for (int rowIndex = 0; rowIndex < sheet.getPhysicalNumberOfRows(); rowIndex++) {
//System.out.println(rowIndex);打印遍歷行號
//根據行號,遍歷該行
XSSFRow row = sheet.getRow(rowIndex);
//如果該行為空,則結束本次循環
if (row == null) {
continue;
}
//num 為該行 有效單元格個數,取 num的話,取值會不全。 lastnum為 有效單元格最后各個單元格的列號,這樣可以遍歷取到該行所有的單元格
//System.out.println("num " + row.getPhysicalNumberOfCells());
//System.out.println("lastnum " + row.getLastCellNum());
for(int cellnum = 0;cellnum<row.getLastCellNum(); cellnum++){
XSSFCell cell = row.getCell(cellnum);
if (cell != null) {
cell.setCellType(Cell.CELL_TYPE_STRING);
//cell.setCellType(Cell.CELL_TYPE_STRING); 是為了修改數據類型,因為我的單元格中有數字類型。如果不這樣寫會出現下面的錯誤。
/* Exception in thread "main" java.lang.IllegalStateException:
Cannot get a text value from a numeric cell
at org.apache.poi.xssf.usermodel.XSSFCell.typeMismatch(XSSFCell.java:991)
at org.apache.poi.xssf.usermodel.XSSFCell.getRichStringCellValue(XSSFCell.java:399)
at net.oschina.excel.ReadExcel.read(ReadExcel.java:55)
at net.oschina.excel.ReadExcel.main(ReadExcel.java:68)
POI操作Excel時數據Cell有不同的類型,當我們試圖從一個數字類型的Cell讀取出一個字符串並寫入數據庫時,
就會出現Cannot get a text value from a numeric cell的異常錯誤,解決辦法就是先設置Cell的類型,
然后就可以把純數字作為String類型讀進來了:
*/
//打印出讀出的數據。
System.out.println("第"+rowIndex+"行 第"+cellnum+"列 內容為:"+cell.getRichStringCellValue().getString());
}
}
}
System.out.println("------------------+++++++++++++++++++--------------------");
}
}
public static void main(String[] args) {
InputStream inputStream = null;
try {
//獲取文件標識符。
inputStream = new FileInputStream(new File("E:\\ReadDemo.xlsx"));
//System.out.println(inputStream);
read(inputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}