首先要下載poi的jar包:下載地址
之後在項目中需要把poi下載目錄裏的所有jar包導入。
然後我自己封裝了一個xlsx讀取類,輸入文件地址,要獲取的列。輸出二維數組,第一維代表一行數據,第二維代表每一行的單元格數據。
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Properties;
import org.apache.poi.hssf.usermodel.HSSFCell;
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 Excel_reader {
//*************xlsx文件讀取函數************************
//excel_name爲文件名,arg爲需要查詢的列號
//返回二維字符串數組
@SuppressWarnings({ "resource", "unused" })
public ArrayList<ArrayList<String>> xlsx_reader(String excel_url,int ... args) throws IOException {
//讀取xlsx文件
XSSFWorkbook xssfWorkbook = null;
//尋找目錄讀取文件
File excelFile = new File(excel_url);
InputStream is = new FileInputStream(excelFile);
xssfWorkbook = new XSSFWorkbook(is);
if(xssfWorkbook==null){
System.out.println("未讀取到內容,請檢查路徑!");
return null;
}
ArrayList<ArrayList<String>> ans=new ArrayList<ArrayList<String>>();
//遍歷xlsx中的sheet
for (int numSheet = 0; numSheet < xssfWorkbook.getNumberOfSheets(); numSheet++) {
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(numSheet);
if (xssfSheet == null) {
continue;
}
// 對於每個sheet,讀取其中的每一行
for (int rowNum = 0; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
XSSFRow xssfRow = xssfSheet.getRow(rowNum);
if (xssfRow == null) continue;
ArrayList<String> curarr=new ArrayList<String>();
for(int columnNum = 0 ; columnNum<args.length ; columnNum++){
XSSFCell cell = xssfRow.getCell(args[columnNum]);
curarr.add( Trim_str( getValue(cell) ) );
}
ans.add(curarr);
}
}
return ans;
}
//判斷後綴爲xlsx的excel文件的數據類
@SuppressWarnings("deprecation")
private static String getValue(XSSFCell xssfRow) {
if(xssfRow==null){
return "---";
}
if (xssfRow.getCellType() == xssfRow.CELL_TYPE_BOOLEAN) {
return String.valueOf(xssfRow.getBooleanCellValue());
} else if (xssfRow.getCellType() == xssfRow.CELL_TYPE_NUMERIC) {
double cur=xssfRow.getNumericCellValue();
long longVal = Math.round(cur);
Object inputValue = null;
if(Double.parseDouble(longVal + ".0") == cur)
inputValue = longVal;
else
inputValue = cur;
return String.valueOf(inputValue);
} else if(xssfRow.getCellType() == xssfRow.CELL_TYPE_BLANK || xssfRow.getCellType() == xssfRow.CELL_TYPE_ERROR){
return "---";
}
else {
return String.valueOf(xssfRow.getStringCellValue());
}
}
//判斷後綴爲xls的excel文件的數據類型
@SuppressWarnings("deprecation")
private static String getValue(HSSFCell hssfCell) {
if(hssfCell==null){
return "---";
}
if (hssfCell.getCellType() == hssfCell.CELL_TYPE_BOOLEAN) {
return String.valueOf(hssfCell.getBooleanCellValue());
} else if (hssfCell.getCellType() == hssfCell.CELL_TYPE_NUMERIC) {
double cur=hssfCell.getNumericCellValue();
long longVal = Math.round(cur);
Object inputValue = null;
if(Double.parseDouble(longVal + ".0") == cur)
inputValue = longVal;
else
inputValue = cur;
return String.valueOf(inputValue);
} else if(hssfCell.getCellType() == hssfCell.CELL_TYPE_BLANK || hssfCell.getCellType() == hssfCell.CELL_TYPE_ERROR){
return "---";
}
else {
return String.valueOf(hssfCell.getStringCellValue());
}
}
//字符串修剪 去除所有空白符號 , 問號 , 中文空格
static private String Trim_str(String str){
if(str==null)
return null;
return str.replaceAll("[\\s\\?]", "").replace(" ", "");
}
}
下面是主方法中對該類的調用:
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Main {
public static void main(String[] args) throws IOException {
Excel_reader test= new Excel_reader();
ArrayList<ArrayList<String>> arr=test.xlsx_reader("/....../filename.xlsx",0,1,2,3,4,5); //後面的參數代表需要輸出哪些列,參數個數可以任意
for(int i=0;i<arr.size();i++){
ArrayList<String> row=arr.get(i);
for(int j=0;j<row.size();j++){
System.out.print(row.get(j)+" ");
}
System.out.println("");
}
}
}