Search code examples
javaapache-poijxl

Is this possible to read and write on same xls sheet?


i am using JXl for xls reading is there any way to read and write in same excel sheet, i am using in selenium project my test data is in xls sheet after reading each row i want to write in that excel sheet.


Solution

  • You can not read & write same file in parallel(Read-write lock). But, we can do parallel operations on temporary data(i.e. Input/output stream). Write the data to file only after closing the input stream. Below steps should be followed.

    • Open the file to Input stream
    • Open the same file to an Output Stream
    • Read and do the processing
    • Write contents to output stream.
    • Close the read/input stream, close file
    • Close output stream, close file.

    Sample code:

    File inputFile = new File("D://"+file_name);
    File outputFile = new File("D://"+file_name);
    Workbook readCopy = Workbook.getWorkbook(inputFile);
    WritableWorkbook writeCopy = Workbook.createWorkbook(outputFile,readCopy);
    
    // instructions to put content in specific rows, specific columns
    
    readCopy.close();    
    inputFile.close();
    writeCopy.write();
    writeCopy.close();
    outputFile.close();
    

    Apache POI - read/write same excel example

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.sql.Date;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;
    
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    
    public class XLSXReaderWriter {
    
        public static void main(String[] args) {
    
            try {
                File excel = new File("D://raju.xlsx");
                FileInputStream fis = new FileInputStream(excel);
                XSSFWorkbook book = new XSSFWorkbook(fis);
                XSSFSheet sheet = book.getSheetAt(0);
    
                Iterator<Row> itr = sheet.iterator();
    
                // Iterating over Excel file in Java
                while (itr.hasNext()) {
                    Row row = itr.next();
    
                    // Iterating over each column of Excel file
                    Iterator<Cell> cellIterator = row.cellIterator();
                    while (cellIterator.hasNext()) {
    
                        Cell cell = cellIterator.next();
    
                        switch (cell.getCellType()) {
                        case Cell.CELL_TYPE_STRING:
                            System.out.print(cell.getStringCellValue() + "\t");
                            break;
                        case Cell.CELL_TYPE_NUMERIC:
                            System.out.print(cell.getNumericCellValue() + "\t");
                            break;
                        case Cell.CELL_TYPE_BOOLEAN:
                            System.out.print(cell.getBooleanCellValue() + "\t");
                            break;
                        default:
    
                        }
                    }
                    System.out.println("");
                }
    
                // writing data into XLSX file
                Map<String, Object[]> newData = new HashMap<String, Object[]>();
                newData.put("1", new Object[] { 1d, "Raju", "75K", "dev",
                        "SGD" });
                newData.put("2", new Object[] { 2d, "Ramesh", "58K", "test",
                        "USD" });
                newData.put("3", new Object[] { 3d, "Ravi", "90K", "PMO",
                        "INR" });
    
                Set<String> newRows = newData.keySet();
                int rownum = sheet.getLastRowNum();
    
                for (String key : newRows) {
                    Row row = sheet.createRow(rownum++);
                    Object[] objArr = newData.get(key);
                    int cellnum = 0;
                    for (Object obj : objArr) {
                        Cell cell = row.createCell(cellnum++);
                        if (obj instanceof String) {
                            cell.setCellValue((String) obj);
                        } else if (obj instanceof Boolean) {
                            cell.setCellValue((Boolean) obj);
                        } else if (obj instanceof Date) {
                            cell.setCellValue((Date) obj);
                        } else if (obj instanceof Double) {
                            cell.setCellValue((Double) obj);
                        }
                    }
                }
    
                // open an OutputStream to save written data into Excel file
                FileOutputStream os = new FileOutputStream(excel);
                book.write(os);
                System.out.println("Writing on Excel file Finished ...");
    
                // Close workbook, OutputStream and Excel file to prevent leak
                os.close();
                book.close();
                fis.close();
    
            } catch (FileNotFoundException fe) {
                fe.printStackTrace();
            } catch (IOException ie) {
                ie.printStackTrace();
            }
        }
    }