我正在尝试导入CSV文件(如此图像)。

这个图像的意思是..

当我导入此文件..行1与读取并保存到表SeniorHighSchool
那么它将得到:

  • 名称:Alexander
  • 年龄:15
  • muchcourse:3

  • 之后,我要创造一个条件,当“类(class)结束”时,它将读取下一行...
    例如:
    在这种情况下,“muchcourse”为“3”,然后“看图像”。行2,3,4(3行)将插入到其他表..中,因为“muchcourse”为“3”

    这是我尝试的编码。
    def upload = {
                withForm{
                    def f = request.getFile('filecsv')
                    def orifilename = f.getOriginalFilename()
                    def homeDir = new File(System.getProperty("user.home"))
                    def homeurl = "Documents/Uploads/"
                    File fileDest = new File(homeDir,homeurl+orifilename)
                    f.transferTo(fileDest)
    
                    request.getFile(new File(fileDest)).InputStream.splitEachLine(',') {fields ->
                    def student= new SeniorHighSchool(
                        name: fields[0].trim(),
                        age: fields[1].trim(),
                        muchcourse: fields[2].trim()
    
                        )
                    if (student.hasErrors() || student.save(flush: true) == null)
                    {
                        log.error("Could not import domainObject  ${student.errors}")
                    }
                    }
    
                    redirect(action:"list")
    
                }
                    }
    

    我很困惑地提出条件。
      def upload = {
                        withForm{
                            def f = request.getFile('filecsv')
                            def orifilename = f.getOriginalFilename()
                            def homeDir = new File(System.getProperty("user.home"))
                            def homeurl = "Documents/Uploads/"
                            File fileDest = new File(homeDir,homeurl+orifilename)
                            f.transferTo(fileDest)
    
                            request.getFile(new File(fileDest)).InputStream.splitEachLine(',') {fields ->
                            def student= new SeniorHighSchool(
                                name: fields[0].trim(),
                                age: fields[1].trim(),
                                muchcourse: fields[2].trim()
    
                                )
                            if (student.hasErrors() || student.save(flush: true) == null)
                            {
                                log.error("Could not import domainObject  ${student.errors}")
                            }
    
                           if(fields[2]) {
                           def score = new Score(
                           course: //the problem at this line..how?
                                   //it will insert 3 times then back to the row 5 to insert into "Student" again
                            )
    
    
    
        }
                            }
    
                            redirect(action:"list")
    
                        }
    
    
     }
    

    最佳答案

    if(fields.size()>2){
      store 3 values in one table(student)
    }
    else{
      store 2 values in another table(score)
    }
    

    如果出现多字段字段,则字段大小为3,然后将三个数据保存在一张表中。否则大小为2,然后将这两个数据保存在另一个表中。我认为它将解决您的问题。

    10-07 13:41