1. 程式人生 > >Excel 一行對多行的解析方法,核心程式碼如下:

Excel 一行對多行的解析方法,核心程式碼如下:

List<xxxxx> exprot = new xxxx<>();
            InputStream  inputStream = ClassLoader.getSystemResourceAsStream("xxxx.xls");
        try{
            POIFSFileSystem po  = new POIFSFileSystem(inputStream);
            HSSFWorkbook hssfWorkbook = new HSSFWorkbook(po);
            HSSFSheet sheetAt = hssfWorkbook.getSheetAt(0);
            //行
            HSSFRow row = null;
            HSSFCell cell = null;

            int indexa = 0;//下標

            for(int i =0 ;i< 1;i++){

                row = sheetAt.getRow(i+1);
                if(row == null){
                    row = sheetAt.createRow(i);
                }

                cell = row.getCell(0);
                if( cell == null){
                    cell = row.createCell(0);
                }
                cell.setCellValue("xxxxx:"+xxx);

                cell = row.getCell(2);
                if( cell == null){
                    cell = row.createCell(2);
                }
                cell.setCellValue("xxxx:"+xxx);

                cell = row.getCell(4);
                if( cell == null){
                    cell = row.createCell(4);
                }
                cell.setCellValue("xxxx:"+xxxx);
            }

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

                //indexa;
                row = sheetAt.getRow(indexa+3);
                if(row == null){
                    row = sheetAt.createRow(indexa+3);
                }

                cell = row.getCell(0);
                if( cell == null){
                    cell = row.createCell(0);
                }
                cell.setCellValue(xxxx);

                cell = row.getCell(1);
                if( cell == null){
                    cell = row.createCell(1);
                }
                cell.setCellValue(xxxxx);

                cell = row.getCell(2);
                if( cell == null){
                    cell = row.createCell(2);
                }
                cell.setCellValue(xxxxx);

                cell = row.getCell(3);
                if( cell == null){
                    cell = row.createCell(3);
                }
                cell.setCellValue(xxxxxx);//申請未就餐

                List<FtfyOvertimeMealModel> ii = exprot.get(i).getEatMealInfo();

                if(ii.size() > 0 ){
                    for(int a = 0 ;a < ii.size() ; a++){
                        if(a > 0){
                            row = sheetAt.createRow(indexa+3);
                        }
                        indexa ++;
                        cell = row.getCell(4);
                        if(cell == null){
                            cell = row.createCell(4);
                        }
                        cell.setCellValue(xxxx);


                        cell = row.getCell(5);
                        if( cell == null){
                            cell = row.createCell(5);
                        }
                        if(ii.get(a).getDinnerTime() != null){
                            cell.setCellValue(xxxx);
                        }



                        cell = row.getCell(6);
                        if( cell == null){
                            cell = row.createCell(6);
                        }
                        if(ii.get(a).getStatus() != null){
                            if(ii.get(a).getStatus() == 2){
                                cell.setCellValue(xxxx);
                            }

                            if(ii.get(a).getStatus() == 4){
                                cell.setCellValue(xxxx);
                            }

                            if(ii.get(a).getStatus() == 5){
                                cell.setCellValue(xxxx);
                            }
                        }
                    }
                }

                CellRangeAddress callRangeAddress = new CellRangeAddress(indexa,indexa+2,0,0);
                sheetAt.addMergedRegion(callRangeAddress);

                CellRangeAddress callRangeAddress1 = new CellRangeAddress(indexa,indexa+2,1,1);
                sheetAt.addMergedRegion(callRangeAddress1);

                CellRangeAddress callRangeAddress2 = new CellRangeAddress(indexa,indexa+2,2,2);
                sheetAt.addMergedRegion(callRangeAddress2);

                CellRangeAddress callRangeAddress3 = new CellRangeAddress(indexa,indexa+2,3,3);
                sheetAt.addMergedRegion(callRangeAddress3);

            }


            OutputStream outputStream = new ByteArrayOutputStream();
            hssfWorkbook.write(outputStream);

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String fileName = url+"/"+ format.format(new Date())+"xxxxxx.xls";

            File upload  = new File(fileName);
            FileOutputStream outputStream1 = new FileOutputStream(upload);
            outputStream1.write(((ByteArrayOutputStream) outputStream).toByteArray());