2016-03-10 73 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