Dynamic 365中读取CSV文件

Posted sunny-technology

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Dynamic 365中读取CSV文件相关的知识,希望对你有一定的参考价值。

 

 

 

Dynamic 365开发中对于读取CSV文件与2012略有不同。Dynamic 365中,对于文件的处理是先上传,后下载的过程。需要通过FileUpload control Upload strategy class FileUploadTemporaryStorageStrategy类来实现对于文件的读取和下载。

以下是一个简单的例子可供参考:

Dilaog窗体,读取文件上传到本地服务器中,以URL方式可以查看

 

 Public Object dialog()
    {
        DialogGroup      dialogGroup;
        FormBuildControl formBuildControl;
        FileUploadBuild  dialogFileUpload;
        #file
        ;
       
        dialog = super();
        dialogGroup = dialog.addGroup("File path");
        formBuildControl = dialog.formBuildDesign().control(dialogGroup.name());
        dialogFileUpload = formBuildControl.addControlEx(classstr(FileUpload), "dialogFileUpload");
        dialogFileUpload.style(FileUploadStyle::MinimalWithFilename);
        dialogFileUpload.baseFileUploadStrategyClassName(classstr(FileUploadTemporaryStorageStrategy));
        dialogFileUpload.fileTypesAccepted(".csv");
        dialogFileUpload.fileNameLabel("Select a file to upload");
       
        return dialog;
    }

 

得到控件名:

   protected FormControl getFormControl(DialogRunbase _dialog, str _controlName)
     {
          return _dialog.formRun().control(_dialog.formRun().controlId( _controlName));
     }

获取文件:

public boolean getFromDialog(  
    {
        boolean ret;
        ;
        ret = super();
        FileUpload     fileupload;
        fileupload  =   this.getFormControl(dialog, "dialogFileUpload");

        fileName      = fileupload.fileName();
        if (fileName == ‘‘)
        {
            checkFailed("File name empty");
        }

        return ret;
    }

读取数据:

 private void readData()
    {

  int                               startpos;
        int                              fieldCSVPos;
        container                   conData ;
        SysDictTable             sysDictTable;
        container                   currentLine;
        int                              totalOfLines;

        CommaTextStreamIo           localStream;


        FileUpload fileUploadControl = this.getFormControl(dialog,  “dialogFileUpload”);
       
        FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult();
       
        if (fileUploadResult != null && fileUploadResult.getUploadStatus())
        {
            fileName = fileUploadResult.getDownloadUrl();
        }
       
        localStream = CommaTextStreamIo::constructForRead(File::UseFileFromURL(fileName));
      
         
        if (localStream.status() != IO_Status::Ok)
        {
            throw error(strfmt(‘Is not possible to open the file. Error %1‘,enum2str(localStream.status())));
        }


        localStream.inFieldDelimiter(",");
        localStream.inRecordDelimiter(" ");
   
        currentLine = localStream.read();

  startpos = 1;//定义读取数据的开始行标

        ttsbegin;
      
        while (currentLine)
        {

            if ( currentLine == connull() ) continue ;

            row++ ;

            if ( row  < startpos ) continue ;

           this.writeData(currentLine) ;
           //  info(strFmt(‘%1‘,conPeek(currentLine,2)));

            currentLine = localStream.read();
        }
        ttscommit;

    }

  欢迎大家讨论,一起学习。

想了解更多,可参考 https://community.dynamics.com/ax/b/365operationswithsukrut/archive/2017/09/28/d365fo-use-of-fileupload-control-to-read-csv-file

 















































以上是关于Dynamic 365中读取CSV文件的主要内容,如果未能解决你的问题,请参考以下文章

nzSQLException 读取超时错误

在Microsoft Dynamic 365/2016环境使用LinqPad查询数据(不使用linqpad Microsoft Dynamic 365 Driver)

通过脚本使用 Office365 Excel 打开 CSV

Dynamic 365中的附件

在python中,如何更新CSV文件中超过5K行的值?

python中怎么读取csv文件