Reading and writing from xls and xlsx excel file in java using Apache POI

前端 未结 2 1919
不思量自难忘°
不思量自难忘° 2021-02-05 20:10

I am writing a program which needs to read and write from excel files, irrespective of the format(xls or xlsx).

I am aware of the Apache POI, but it seems it has differe

2条回答
  •  执笔经年
    2021-02-05 20:59

    It's very easy, just use the common SpreadSheet interfaces

    Your code would look something like:

     Workbook wb = WorkbookFactory.create(new File("myFile.xls")); // Or .xlsx
     Sheet s = wb.getSheet(0);
     Row r1 = s.getRow(0);
     r1.createCell(4).setCellValue(4.5);
     r1.createCell(5).setCellValue("Hello");
    
     FileOutputStream out = new FileOutputStream("newFile.xls"); // Or .xlsx
     wb.write(out);
     out.close();
    

    You can read, write, edit etc an existing file, both .xls and .xlsx, with exactly the same code as long as you use the common interfaces

提交回复
热议问题