java 如何使用 Apache POI 使用来自数组列表的数据填充 Excel 工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17468567/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:05:13  来源:igfitidea点击:

How to populate an Excel sheet with the data from an arraylist using Apache POI

javaexcelarraylistapache-poi

提问by user2550165

How to populate an Excel sheet with the data from an arraylist using Apache POI?

如何使用 Apache POI 使用来自数组列表的数据填充 Excel 工作表?

public String exporttoexcel(UserDetails user) throws Exception {

    System.out.print("Inside serviceimpl.ExportToExcel method");
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");
    HSSFRow row = sheet.createRow((short)0);

        List<UserDetails> lstUserDetail = getUserDetailList();

        for (int i=0; i<lstUserDetail.size(); i++) {

            UserDetails lstUserDetail1 = lstUserDetail.get(i);

            String a=lstUserDetail1.getStrUserName();
             System.out.print("useridddd is"+a);

             HSSFCell cell = row.createCell((short) 0);
                cell.setCellValue(lstUserDetail1.getStrUserId());
                cell.setCellValue(lstUserDetail1.getStrUserName());
                cell.setCellValue(lstUserDetail1.getStrEMail());
                cell.setCellValue(lstUserDetail1.getStrUserStatus());
                cell.setCellValue(lstUserDetail1.getStrUserRole());

         }
         FileOutputStream fileOut = new FileOutputStream("workbook.xls");
        wb.write(fileOut);
        fileOut.close();

       System.out.print("file created");

       return null;
        }

回答by Sankumarsingh

You have created one single cell and entering all the values in a same single cell each time.

您已经创建了一个单元格并每次都在同一个单元格中输入所有值。

You need to take two loops. One for iterating through row and another for iterating through column. Though I have not tested... use code like below.

您需要进行两个循环。一个用于遍历行,另一个用于遍历列。虽然我没有测试过......使用如下代码。

 for(int RowNum=0; RowNum<MaxArrayLength;RowNum++){
    HSSFRow row = sheet.createRow(RowNum);
    for(int ColNum=0; ColNum<ArrayWidth;ColNum++){
        HSSFCell cell = row.createCell(ColNum);
        cell.setCellValue(ArrayList[RowNum][ColNum]);
     }
 }

回答by Nani

    FileOutputStream fileOut = new FileOutputStream("your file path");
            XSSFWorkbook workbook = new XSSFWorkbook();
            XSSFSheet sheet = workbook.createSheet("sheet name");
            try {

    //creating the headers          
                Row row = sheet.createRow(0);
                row.createCell(0).setCellValue("name");
                row.createCell(1).setCellValue("Name1");                    
                row.createCell(2).setCellValue("name2");

    //get the list which u want
                List<String> list = getNamesList();                         
            int rowNum = 1;
            for (Name name : list ) {
                Row row1 = sheet.createRow(rowNum++);
        row1.createCell(0).setCellValue((name.get..));

}
                workbook.write(fileOut);
}