循环2个列表并将结果打印到csv

时间:2015-07-14 21:04:00

标签: java spring apache csv apache-commons

我有这个问题,我试图循环2个列表(不同长度)并将结果打印到csv。

下面是我的代码:(我已经标记了我需要帮助的行)

@RequestMapping(value = "/do.download")
    public String download(Form form, Model model,HttpServletRequest request, HttpServletResponse response)throws IOException {
        PrintWriter pw = null;
        try {
            logger.debug("+++++++++++++++++++++++++++++++++++++++");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-disposition","attachment; filename=" + "Backup"+ ".csv");
            response.setHeader("Expires", "0");
            response.setHeader("Cache-Control","must-revalidate, post-csheck=0, pre-check=0");
            response.setHeader("Pragma", "public");
            pw = response.getWriter();

            pw.println("carid,carname,carmodel,carcolor");

            car CarFromDB = new car(); 
            List<carDetails> CarDetailsFromDB = Collections.emptyList();
            List<carColor> carColorFromDB = Collections.empltyList();


            CarDetailsFromDB = Service.getcarDetails(CarFromDB);
            carColorFromDB =  Service.getcarColor(CarFromDB);


            for (carDetails cardetails : CarDetailsFromDB) {

                pw.println(String.format("%s,%s,%s,%s", 
                        cardetails.getcarid(),
                        cardetails.getcarname(),
                        cardetails.getcarmodel(),
                        //get car color from the listcarColorFromDB which also has carid <--- this is where i need help 

                        ));
            }
            pw.flush();

        } catch (Exception ex) {
            ex.printStacktrace();
        }
        return null;
    }

2 个答案:

答案 0 :(得分:0)

我不确定每辆车是否可以有多种颜色。如果是这样,在下面你可以看到你可以遍历另一个循环中的颜色并比较carId&#39; s。如果匹配,则将颜色添加到字符串中。最后使用创建的字符串作为println的最后一个参数。我修改了一些格式问题以及名称和大小写。类应为大写,变量为小写。

@RequestMapping(value = "/do.download")
public String download(Form form, Model model,HttpServletRequest request, HttpServletResponse response)throws IOException {
    PrintWriter pw = null;
    try {
        logger.debug("+++++++++++++++++++++++++++++++++++++++");
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition","attachment; filename=" + "Backup"+ ".csv");
        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control","must-revalidate, post-csheck=0, pre-check=0");
        response.setHeader("Pragma", "public");
        pw = response.getWriter();

        pw.println("carid,carname,carmodel,carcolor");

        Car carFromDB = new Car(); 
        List<CarDetails> carDetailsFromDB = new ArrayList<CarDetails>();
        List<CarColor> carColorFromDB = new ArrayList<CarColor>();


        carDetailsFromDB = Service.getCarDetails(carFromDB);
        carColorFromDB =  Service.getCarColor(carFromDB);


        for (CarDetails carDetails : carDetailsFromDB) {

            //loop through all the colors and find ones that match the carId for this carDetails
            // then concatenate them together with a "|"
            String colorsStr = "";
            for(CarColor color : carColorFromDB){
               if(color.getCarId() == carDetails.getCarId()){
                   colorsStr = colorsStr + "|" + color.toString();
               }
            }
            //chop off the first character which should be a "|" 
            colorsStr = colorsString.substring(1);


            pw.println(String.format("%s,%s,%s,%s", 
                    carDetails.getCarId(),
                    carDetails.getCarName(),
                    carDetails.getCarModel(),
                    //get car color from the listcarColorFromDB which also has carid <--- this is where i need help 
                    colorsStr
                    ));
        }
        pw.flush();

    } catch (Exception ex) {
        ex.printStacktrace();
    }
    return null;
}

答案 1 :(得分:0)

有两种方法可以处理它......

  1. 在第一个循环中运行第二个循环...

       for (carDetails cardetails : CarDetailsFromDB) {
               for (carColor carColor : carColorFromDB ) {
                  //match the car id from 2 objects and act   
               }
    
        }
    
  2. 创建一个地图,其中key为car id,值为carColor。访问里面的地图for循环以获得匹配的汽车颜色。