How list has map values write to excel file using Apache poi

前端 未结 2 1335
难免孤独
难免孤独 2021-01-16 02:52

I\'m getting list hash map key and values like:{1=[ACSS Description1, ACSS Description2, ACSS Description3, SACSS Description4], 2=[11, 1, 4, 12]}

I wou

2条回答
  •  抹茶落季
    2021-01-16 03:23

    I create a class contains methods that will help you to:

    Put your data from Criteria criteria=hibernateTemplate.getSessionFactory().openSession().createCriteria(ExportReport.‌​class); List list = criteria.list(); to a map

    Create a Xlsx file and initialize it with the number of the rows and cells needed for writing the data from map.

    Write the data from map to Xlsx file

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    import java.util.TreeMap;
    
    import org.apache.poi.ss.usermodel.Cell;
    import org.apache.poi.xssf.usermodel.XSSFRow;
    import org.apache.poi.xssf.usermodel.XSSFSheet;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    
    public class UtilsMethod {
    
        private XSSFWorkbook workbook = new XSSFWorkbook();
    
        /*
         * Take a Map of integer and List of string and
         *Create a Xlsx file on given path with number of row equal to size of nameList And number of Cell equal to keyset size
         */
        public void initializeExcelFile(Map>hashmap,String path) throws IOException{
    
            FileOutputStream out = new FileOutputStream( new File(path));
            Set keyset = hashmap.keySet();
            XSSFSheet sheet = workbook.createSheet();
            XSSFRow row = null;
            List nameList = hashmap.get(keyset.toArray()[0]);
    
            for(int j=1; j>hashmap,String path) throws IOException{
    
            Set keyset = hashmap.keySet();
            InputStream inp = new FileInputStream( new File(path));
            FileOutputStream out = new FileOutputStream( new File(path));
            int rownum = 1;
            int cellnum = 0;
    
            initializeExcelFile(hashmap,path);
    
            workbook = new XSSFWorkbook(inp);
    
            XSSFSheet sheet = workbook.getSheetAt(0);
    
            for(Integer key : keyset){
                List nameList = hashmap.get(key);
                for(String s : nameList){
                    XSSFRow row = sheet.getRow(rownum++);
                    Cell cell = row.getCell(cellnum);
                    if(null!=cell){
                        cell.setCellValue(s);
                    }
                }
                cellnum++;
                rownum=1;
            }
    
            workbook.write(out);
            out.close();
            inp.close();
        }
    
        public Map> putListIntoMap(ListexportReports) {
    
            Map> exportRep = new TreeMap>();
            List descriptions = new ArrayList();
            List exportIntervalIds = new ArrayList();
    
            for(ExportReport report:exportReports){
                descriptions.add(report.getDescription());
                exportIntervalIds.add(report.getExportIntervalId().toString());
            }
    
            exportRep.put(1, descriptions);
            exportRep.put(2, exportIntervalIds);
    
            return exportRep;
        }
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
    
        }
    
    }
    

    And test Class for testing all UtilsMethod class' method

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    
    public class Test {
    
    
        public static void main(String[] args) throws IOException {
    
            ExportReport exportReport = new ExportReport();
            exportReport.setExportIntervalId(11);
            exportReport.setDescription("ACCSDESCRIPTION1");
    
            ExportReport exportReport2 = new ExportReport();
            exportReport2.setExportIntervalId(1);
            exportReport2.setDescription("ACCSDESCRIPTION2");
    
            ExportReport exportReport3 = new ExportReport();
            exportReport3.setExportIntervalId(4);
            exportReport3.setDescription("ACCSDESCRIPTION3");
    
            ExportReport exportReport4 = new ExportReport();
            exportReport4.setExportIntervalId(12);
            exportReport4.setDescription("ACCSDESCRIPTION4");
    
            List exportReports = new ArrayList();
    
            exportReports.add(exportReport);
            exportReports.add(exportReport2);
            exportReports.add(exportReport3);
            exportReports.add(exportReport4);
    
            UtilsMethod utilsMethod = new UtilsMethod();
    
            Map> map = utilsMethod.putListIntoMap(exportReports);
            System.out.println(map);
    
    
            utilsMethod.writeToExcelfile(map, "Writesheet.xlsx");
    
            System.out.println("Writesheet.xlsx written successfully" );
    
        }
    
    }
    

    After running Test class, you will get this Xlsx file

    enter image description here

提交回复
热议问题