2016-03-10 93 views
1

下午好專家有問題擅長,我需要閱讀整列名,而不是它的指數,例如Excel文件:讀取列名的Java POI

Column1 | Column2 | Column3 
data1  data 2 data 3 

POI讓我讀列索引通過方法getColumnIndex()返回爲Column1 = 0 , Column2= 1 etc,,但我需要閱讀它的列名Column1,Column2等, 有沒有辦法做到這一點?

我需要按列名讀取行和列的所有字段。 重視,我看我的文件中的代碼:

更新的代碼:

此代碼的工作,但我需要驗證列名,因爲我的項目列可能改變位置,這就是我的目標

回答

3

爲什麼不讀取第一行(0)單元格值(0-n)(又名列名稱)並將(columnName,columnIndex)放入String/int映射中。然後,您可以按名稱引用列索引。

下面是一個例子:

Map<String, Integer> map = new HashMap<String,Integer>(); //Create map 
HSSFRow row = sheet.getRow(0); //Get first row 
//following is boilerplate from the java doc 
short minColIx = row.getFirstCellNum(); //get the first column index for a row 
short maxColIx = row.getLastCellNum(); //get the last column index for a row 
for(short colIx=minColIx; colIx<maxColIx; colIx++) { //loop from first to last index 
    HSSFCell cell = row.getCell(colIx); //get the cell 
    map.put(cell.getStringCellValue(),cell.getColumnIndex()) //add the cell contents (name of column) and cell index to the map 
} 

在此之後,你會從COLUMNNAME有地圖--->指數。那麼你可以這樣做:

int idx = map.get("ColumnName"); 

....你可以在row.getCell(idx)中使用它來獲取所有其他行中的單元格。

閱讀下面的代碼中的評論。除此之外,我無法幫到你。你需要閱讀文檔並找出如何去做。

Workbook workbook = WorkbookFactory.create(new FileInputStream("C:\\file.xlsx")); 

Sheet sheet = workbook.getSheetAt(0); 
totalRows = sheet.getPhysicalNumberOfRows(); 

Map<String, Integer> map = new HashMap<String,Integer>(); //Create map 
HSSFRow row = sheet.getRow(0); //Get first row 
//following is boilerplate from the java doc 
short minColIx = row.getFirstCellNum(); //get the first column index for a row 
short maxColIx = row.getLastCellNum(); //get the last column index for a row 
for(short colIx=minColIx; colIx<maxColIx; colIx++) { //loop from first to last index 
HSSFCell cell = row.getCell(colIx); //get the cell 
map.put(cell.getStringCellValue(),cell.getColumnIndex()) //add the cell contents (name of column) and cell index to the map 
} 

List<ReportRow> listOfDataFromReport = new ArrayList<ReportRow>(); 
for(int x = 1; x<=totalRows; x++){ 
ReportRow rr = new ReportRow(); //Data structure to hold the data from the xls file. 
HSSFRow dataRow = sheet.getRow(x); //get row 1 to row n (rows containing data) 

int idxForColumn1 = map.get("Column1"); //get the column index for the column with header name = "Column1" 
int idxForColumn2 = map.get("Column2"); //get the column index for the column with header name = "Column2" 
int idxForColumn3 = map.get("Column3"); //get the column index for the column with header name = "Column3" 

HSSFCell cell1 = dataRow.getCell(idxForColumn1) //Get the cells for each of the indexes 
HSSFCell cell2 = dataRow.getCell(idxForColumn2) 
HSSFCell cell3 = dataRow.getCell(idxForColumn3) 

//NOTE THAT YOU HAVE TO KNOW THE DATA TYPES OF THE DATA YOU'RE EXTRACTING. 
//FOR EXAMPLE I DON'T THINK YOU CAN USE cell.getStringCellValue IF YOU'RE TRYING TO GET A NUMBER 
rr.setColumn1(cell1.getStringCellValue()); //Get the values out of those cells and put them into the report row object 
rr.setColumn2(cell2.getStringCellValue()); 
rr.setColumn3(cell3.getStringCellValue()); 

listOfDataFromReport.add(rr); 

} 

//Now you have a list of report rows 
for(int j = 0; j< listOfDataFromReport.size();j++){ 
    System.out.println("Column 1 Value: " + listOfDataFromReport.get(j).getColumn1()) 
//etc...  
} 

//This class holds the values from the xls file. You may not need it 
// I have no idea what you're doing with the data. If you simply wanted to 
//print the data to console you wouldn't need it. 
public static class ReportRow{ 
private String column1; 
private String column2; 
private String column3; 

public String getColumn1(){ 
    return this.column1; 
} 
public void setColumn1(String column1){ 
    this.column1 = column1; 
} 

public String getColumn2(){ 
    return this.column2; 
} 
public void setColumn2(String column2){ 
    this.column2 = column2; 
}  
public String getColumn3(){ 
    return this.column3; 
} 
public void setColumn3(String column3){ 
    this.column3 = column3; 
} 
} 
+0

非常感謝您的回答,但仍然無法適應我的代碼,我絕望,你能幫我一個非常簡單的例子1列PLZ! :( – user2091725

+0

非常感謝您的幫助,我嘗試了代碼,但只重複獲取第一列的名稱,但我需要它的值,我對值做的是使用正則表達式應用驗證,我分享我的最初的代碼,但爲此我需要通過名稱而不是其索引來驗證列(我和這個成功), 更新初始代碼 – user2091725

+0

用dataRow.getCell(idxForColumn1) – neal