How to read and write excel file

后端 未结 22 2982
北荒
北荒 2020-11-22 04:49

I want to read and write an Excel file from Java with 3 columns and N rows, printing one string in each cell. Can anyone give me simple code snippet for this? Do I need to

22条回答
  •  余生分开走
    2020-11-22 05:16

    If you go for third party library option, try using Aspose.Cells API that enables Java Applications to create (read/write) and manage Excel spreadsheets efficiently without requiring Microsoft Excel.

    e.g

    Sample code:

    1.

    //Load sample workbook
    Workbook wb = new Workbook(dirPath + "sample.xlsx");
    
    //Access first worksheet
    Worksheet ws = wb.getWorksheets().get(0);
    
    //Access cells iterator
    Iterator itrat = ws.getCells().iterator();
    
    //Print cells name in iterator
    while(itrat.hasNext())
    {
        Cell cell = (Cell)itrat.next();
    
        System.out.println(cell.getName() + ": " + cell.getStringValue().trim());
    }
    
    Workbook book = new Workbook("sample.xlsx");
    Worksheet sheet = book.getWorksheets().get(0);
    Range range = sheet.getCells().getMaxDisplayRange();//You may also create your desired range (in the worksheet) using, e.g sheet.getCells().createRange("A1", "J11");
    Iterator rangeIterator = range.iterator();
    while(rangeIterator.hasNext())
    {
    Cell cell = (Cell)rangeIterator.next();
    //your code goes here.
    }
    

    Hope, this helps a bit.

    PS. I am working as Support developer/ Evangelist at Aspose.

提交回复
热议问题