2

I want to write a excel file. all writing details consist with a list. and set it as input parameter for the method. Please tell me the best way to do this.

public static void writeExcelFile(List<String> combineLists){
          XSSFWorkbook workbook = new XSSFWorkbook(); 
          XSSFSheet sheet = workbook.createSheet("Employee Data");
|
|

}

Input list:

1

1 Answer 1

3

Your Images says you have a list of string inside List where as your Input parameter has only list as a parameter. So I'll assume it is List<List<String>>

   FileOutputStream out = new FileOutputStream(new File("demo.xlsx"));
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("Employee Data");
//I am creating and adding list just for illustration purpose only
            List<String> l1 = new ArrayList<String>();
            l1.add(" l1 1");
            l1.add("l1 2");
            List<String> l2 = new ArrayList<String>();
            l2.add(" l2 1");
            l2.add("l2 2");
            List<List<String>> list = new ArrayList<List<String>>();
            list.add(l1);
            list.add(l2);
            Iterator <List<String>>i = list.iterator();
            int rownum=0;
            int cellnum = 0;
            while (i.hasNext()) {
                List<String> templist = (List<String>) i.next();
                Iterator<String> tempIterator= templist.iterator();
                Row row = sheet.createRow(rownum++);
                cellnum = 0;
                while (tempIterator.hasNext()) {
                    String temp = (String) tempIterator.next();
                        Cell cell = row.createCell(cellnum++);
                                cell.setCellValue(temp);

                    }

                }
            workbook.write(out);
            out.close();
            workbook.close();

I hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

how to write the same in column wise?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.