根据 java 中的列名从 excel 中获取列值

get column value from excel based on column names in java

我想根据 java 中的列名读取 excel sheet。 我想获取与列名称相对应的列值,例如 get TXT_Policy_no in policy no string 等。 由于 excel sheet 值可能会受到影响,我想读取基于列名的值,以便更安全。

我想获取与列名称相对应的列值,例如 get TXT_Policy_no in policy no string 等。 由于 excel sheet 值可能会受到影响,我想读取基于列名的值以便更安全。

我的java代码:

for (int i = 0; i < listofexcel.length; i++)
            {
              if (listofexcel[i].isFile()) 
              {
                //System.out.println("File " + listOfCsv[i].getName());
                Csv_list.add(listofexcel[i].getName()); 
              } else if (listofexcel[i].isDirectory()) 
              {
                System.out.println("Directory " + listofexcel[i].getName());
              }
            }
           
        
            Iterator itr = Csv_list.iterator();  
            while(itr.hasNext())
            {   
                String CsvName = (String) itr.next();
                
                 
                 System.out.println(CsvName);
                 ProcessMain worker = new ProcessMain(CsvName);
                
                 //    Runnable worker = new UploadFiles(CsvName);
                // System.out.println("^^^^^^^");
                 String csvpath= GlobalVariable.getCsv_path()+CsvName;
                 WFLogger.printOut(csvpath);
                 first(csvpath);
                 final File f2 = new File(csvpath);
                 if(f2.exists())
                 {
                     f2.delete();
                 }
                //executor.execute(worker);
            }
            
        
        
    }
        catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    
}
   
 
    //Returns the Headings used inside the excel sheet
    public void getHeadingFromXlsFile(Sheet sheet) {
        int columnCount = sheet.getColumns();
        for (int i = 0; i < columnCount; i++) {
            System.out.println(sheet.getCell(i, 0).getContents());
        }
    }
 



private void first(String csvName) {
    // TODO Auto-generated method stub
     FileInputStream fs = null;
        try {
            fs = new FileInputStream(new File(csvName));
            contentReading(fs);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
}






private void contentReading(FileInputStream fs) throws ClassNotFoundException, BiffException {
    // TODO Auto-generated method stub
    

    WorkbookSettings ws = null;
    Workbook workbook = null;
    Sheet s = null;
    jxl.Cell[] rowData=null;
    int rowCount = '0';
    int columnCount = '0';
    DateCell dc = null;
    int totalSheet = 0;
         
        ws = new WorkbookSettings();
        ws.setLocale(new Locale("en", "EN"));
        workbook = Workbook.getWorkbook(fs, ws);

        totalSheet = workbook.getNumberOfSheets();
        if(totalSheet > 0) {
            System.out.println("Total Sheet Found:" + totalSheet);
            String sheetName="";
            for(int jj=0;jj<totalSheet ;jj++) {
                sheetName=workbook.getSheet(jj).getName();
                System.out.println("Sheet Name:" + sheetName);

                s = workbook.getSheet(jj);

                //Reading Individual Cell
                //getHeadingFromXlsFile(s);

                System.out.println("Total Rows inside Sheet:" + s.getRows());
                rowCount = s.getRows();

                System.out.println("Total Column inside Sheet:" + s.getColumns());
                columnCount = s.getColumns();

                //Reading Individual Row Content
                for (int i = 1; i < rowCount; i++) {
                    //Get Individual Row
                    rowData = s.getRow(i);                      
                    if (rowData[0].getContents().length() != 0) {
                        int j=0;
                        String Claimno="";
                        String Product="";
                        String Mobileno="";
                        String Omniflag="N";
                        String Policyno="";
                        String Customername="";
                        String Certno="";
                        String Department="";
                        String lossdate="";
                        String Srno="";
                        
                        



                        for ( j = 0; j < columnCount; j++) {                 
                            try{
                                switch (j) {
                                case 0:
                                    Srno= rowData[j].getContents();
                                    break;
                                case 1:
                                    Policyno=rowData[j].getContents();                                   
                                    break;
                                case 2:
                                    Certno=rowData[j].getContents();
                                    break;
                                case 3:
                                    lossdate= rowData[j].getContents();
                                    break;
                                case 4:
                                    Department=rowData[j].getContents();                                   
                                    break;
                                case 5:
                                    Product=rowData[j].getContents();
                                    break;
                                case 6:
                                    Claimno= rowData[j].getContents();
                                    break;
                                case 7:
                                    Customername=rowData[j].getContents();                                   
                                    break;
                                
                                default:
                                    break;
                                }
                            }catch (Exception e) {
                                System.out.print(e);
                            }
                        }
                       

我假设我们知道哪一行包含 headers。然后我们可以遍历它的单元格并检查它们的值以收集有关列的信息。喜欢

int headerRow = sheet.getRow(n); 
Map<String, Integer> columnIndexes = new HashMap();
 for ( j = 0; j < columnCount; j++) {                 
    switch (rowData[j].getContents()) {
       case "TXT_Policy_no":
            columnIndexes.put("TXT_Policy_no", j); break;
        ...
        case "CLAIMNO":
            columnIndexes.put("CLAIMNO", j); break;
    }
}

地图填满后,您可以访问像

这样的单元格
int columnIndex = columnIndexes.get("CLAIMNO")
var cell = row.getCell(columnIndex);