简体   繁体   中英

How to read an excel file, sheet by sheet using jxl

I'm working on a utility to convert excel file from one format to another. I was suggested to use jxl library to meet the requirements which are as follows.
read the file sheet by sheet and do following

get the sheet name, make it key of a map and then get its column headers and make them value.

it will result in following

   Map<String, List<String>> result = result<key=sheetName, value=list of column headers> 

Do this for all sheets in the given file

I did this in following way

public Map> function(String filePath ) throws IOException, BiffException{

       Map<String, List<String>> map = new HashMap<String, List<String>>(); 
    Workbook workBook=Workbook.getWorkbook(new File (filePath));
      String [] sheetNames = workBook.getSheetNames();
      Sheet sheet=null;
      List<String > fields = new ArrayList<String>();
      for (int sheetNumber =0; sheetNumber < sheetNames.length; sheetNumber++){
         sheet=workBook.getSheet(sheetNames[sheetNumber]);
         for (int columns=0;columns < sheet.getColumns();columns++){
             fields.add(sheet.getCell(columns, 0).getContents());


         }
         map.put(sheetNames[sheetNumber],fields);

      }
      return map;
}

I did this with a hope of getting desired result but what it does is that it stores column headers of all sheets against each key as value. ie if there are two sheets in file named as

  1. sheet1
  2. sheet2

and following are their column headers

Sheet1 -> id, name
sheet2 -> category, price

then map will be like

result<sheet1,<id, name, caegory, price>>
result<sheet2,<id, name, caegory, price>>

Can't figure out what I'm doing wrong? Help please, as my project has a lot of backend calculations and I don't want to spend a lot of time in this thing.

Any help will be highly appreciated

I solved the issue after posting it here, but forgot to give solution here.
The problem in above code is location of list declaration.
It should be declared inside the for , so that it gets refreshed (emptied) after each iteration of loop and stores data of only one sheet at a time.
The correct code is below.

      Map<String, List<String>> map = new HashMap<String, List<String>>(); 
      Workbook workBook=Workbook.getWorkbook(new File (filePath));
      String [] sheetNames = workBook.getSheetNames();
      Sheet sheet=null;
      for (int sheetNumber =0; sheetNumber < sheetNames.length; sheetNumber++){
         List<String > fields = new ArrayList<String>();
         sheet=workBook.getSheet(sheetNames[sheetNumber]);
         for (int columns=0;columns < sheet.getColumns();columns++){
             fields.add(sheet.getCell(columns, 0).getContents());                 
         }
         map.put(sheetNames[sheetNumber],fields); 
      }
      return map;
}

Hope It'll help someone

public String ExcelValueGetFn(String FilePath, int column, int row) throws BiffException, IOException  {
    Workbook workbook = null;
    final String EXCEL_FILE_LOCATION = FilePath;
    workbook = Workbook.getWorkbook(new File(EXCEL_FILE_LOCATION));

    Sheet sheet = workbook.getSheet(0);
    Cell cell1 = sheet.getCell(column, row);
    System.out.print(cell1.getContents() + ":");

    String textToSendFromExcel = cell1.getContents();
    return textToSendFromExcel;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM