0

I am having output results in console need to import in to excel any advise.

for (String available : list1) {

            System.out.println("Available hotels: -------"+available);

My results will print in available as like above those o/p need to export into excel.

Tried using below,not working

for(int m = 0; i < available.length(); m++) {
```          
File fExcel = new File("C:/Users/GoQuo MY/Desktop/Hotel.xls");
WritableWorkbook writableBook = Workbook.createWorkbook(fExcel); 
 writableBook.createSheet("Data", 0);
WritableSheet writableSheet = writableBook.getSheet(0);
Thread.sleep(5000);
Label data1 = new Label(0, m, available);
0

2 Answers 2

1

The "available" variable is local to the "for" loop. You can use below code to get the values of "list1" (which I assume is a array of strings):

    //Create object of File type
    File fExcel = new File("xxxxxx\\Hotel.xls");

    //Create excel file with 1 sheet
    WritableWorkbook writableBook = Workbook.createWorkbook(fExcel); 
    writableBook.createSheet("Data", 0);
    WritableSheet writableSheet = writableBook.getSheet(0);
    Thread.sleep(1000);

    for(int m = 0; m < list1.length; m++) {

        //Create label
        Label data1 = new Label(0, m, list1[m]);
        //Write the label value to cell in excel
        writableSheet.addCell(data1);

    }

    //Complete writing to excel and close excel process
    writableBook.write();
    writableBook.close();
0
0

Thanks it was working fine.. I also found one more solutions similar to this.

for (String available : list1) {
            System.out.println("Available hotels: -------"+available);

            XSSFWorkbook book = new XSSFWorkbook();     
            XSSFSheet sheet = book.createSheet("newsheet");     
            int rowno = 0;
            for (String add : list1) {          
                Row row = sheet.createRow(rowno++);
                createlist(add,row);            
            }

            FileOutputStream out = new FileOutputStream(new File("location/aaa.xlsx")); // file name with path
            book.write(out);
            out.close();
        }
        }
        private static void createlist(String add, Row row) {       
            Cell cell = row.createCell(0);
            cell.setCellValue(add);
5
  • Cool. But why to use 2 for loops. The first "for" loop in your code is reluctant. Use the same code with outer "for" loop and you will get the same output.
    – Ranjeet
    Commented Feb 20, 2019 at 7:07
  • If you want the result in console as well, write below line in inner loop: System.out.println("Available hotels: -------"+available);
    – Ranjeet
    Commented Feb 20, 2019 at 7:08
  • Yes,i am printing in console first using 1st for loop later,i am exporting using second loop
    – Sinu Reddy
    Commented Feb 20, 2019 at 7:16
  • Ok,i will remove the first for loop and add in the inner loop,thanks for the response
    – Sinu Reddy
    Commented Feb 20, 2019 at 7:21
  • Yep. But by using two "For" loops, you are actually creating and deleting and again creating the excel sheet n number times (where n is the number of values in "list1") :). For example, if you have 3 values in "list1", your code is actually creating and deleting the excelsheet 2 times and then creating the sheet for third time.
    – Ranjeet
    Commented Feb 20, 2019 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.