smith willy
smith willy

Reputation: 73

Edit Text in Excel

I want to add text to excel file. I am able to add it once. But, when I try second time, only the last value is updated. I tried few other things, but I am not getting the answer.

while (true) {
            try {

                 clientSocket = serverSocket.accept();   //accept the client connection
                inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
                bufferedReader = new BufferedReader(inputStreamReader); //get the client message
                message = bufferedReader.readLine();

                System.out.println("Java Server1 " +  message);
writeInExcel(message);

}

 public static void writeInExcel(String stock_list) throws WriteException, IOException, BiffException
    {


       Workbook existingWorkbook = Workbook.getWorkbook(new File("try.xls"));
        WritableWorkbook workbookCopy = Workbook.createWorkbook(new File("output.xls"), existingWorkbook);
        WritableSheet sheetToEdit = workbookCopy.getSheet(0);

         Label l = new Label(0, i, String.valueOf( i));
         sheetToEdit.addCell(l);

        workbookCopy.write();
        workbookCopy.close();
        existingWorkbook.close();
    }

Here, as soon as I get something on inputstream, I need to add it to the excel file. If some one can help me or can explain me a code bit, it would be great. After the writable cell, I am not able to understand the code fully

Upvotes: 2

Views: 114

Answers (1)

Nidhish Krishnan
Nidhish Krishnan

Reputation: 20751

Since in your code you have put the following within the for loop, please bring that outside the loop

workbookCopy.write();
workbookCopy.close();
existingWorkbook.close();

Try this one

    Workbook existingWorkbook = Workbook.getWorkbook(new File("try.xls"));
    WritableWorkbook workbookCopy = Workbook.createWorkbook(new File("output.xls"), existingWorkbook);
    WritableSheet sheetToEdit = workbookCopy.getSheet(0);
    for(int i=0;i<5;i++)  
    {
     Label l = new Label(0, i, String.valueOf( i));
     sheetToEdit.addCell(l);
     }
    workbookCopy.write();
    workbookCopy.close();
    existingWorkbook.close();

output.xls when i put limit = 5

enter image description here

Upvotes: 1

Related Questions