开始之前的准备
- 命名空间
- 参考包(reference packages)
Using System.IO; Using OfficeOpenXml; Using OfficeOpenXml.ExcelPackage; Using OfficeOpenXml.ExcelRange;
除了基本的包,我们还需要参考包:Directory
第一部分代码
在这部分代码中,创建了一个导入对话框,在这里我们可以选择需要导入的文件,文件上传之后会被存储在临时的空间中
第二部分代码
在这部分代码中,我们从导入的Excel中获取数据
完整的代码
Using System.IO; Using OfficeOpenXml; Using OfficeOpenXml.ExcelPackage; Using OfficeOpenXml.ExcelRange; class RunnableClass1 { /// <summary> /// Runs the class with the specified arguments. /// </summary> /// <param name = "_args">The specified arguments.</param> public static void main(Args _args) { /*---------part 1 ------------------*/ System.IO.Stream stream; ExcelSpreadsheetName sheet; FileUploadBuild fileUpload,fileUploadBuild; DialogGroup dialogUploadGroup; FormBuildControl formBuildControl; Dialog dialog=new Dialog("Excel Import Example"); dialogUploadGroup=dialog.addGroup("@SYS54759"); formBuildControl=dialog.formBuildDesign().control(dialogUploadGroup.name()); fileUploadBuild=formBuildControl.addControlEx(classStr(fileUpload),"UploadExcel"); fileUploadBuild.style(FileUploadStyle::MinimalWithFilename); fileUploadBuild.fileTypesAccepted(".xlsx"); if(dialog.run() && dialog.closedOk()) { FileUpload fileUploadControl=dialog.formRun().control(dialog.formRun().controlId("Upload")); FileUploadTemporaryStorageResult fileUploadResult=file::GetFileFromUser(classStr(FileUploadTemporaryStorageStrategy)); //fileUploadResult=fileUploadControl.getFileUploadResult(); /*------------------part 1 end---------------------*/ /*------------------part 2---------------------*/ if(fileUploadResult!= null && fileUploadResult.getUploadStatus()) { stream=fileUploadResult.openResult(); using(ExcelPackage package= new ExcelPackage(stream)) { int rowCount, iterator; package.Load(stream); ExcelWorksheet worksheet= package.get_workbook().get_worksheets().get_Item(1); OfficeOpenXml.ExcelRange range=worksheet.Cells; rowCount = worksheet.Dimension.End.Row - worksheet.Dimension.Start.Row + 1; for(iterator=2;iterator<=rowCount;iterator++) { Info(range.get_Item(iterator,1).Value); Info(range.get_Item(iterator,2).Value); Info(range.get_Item(iterator,3).Value); } } } /*------------------part 2 end---------------------*/ } else { Error("error occured."); } } }
导入
运行上面的Job之后,系统会弹出导入对话框,然后选择下面的文件。
以下是点击“OK”之后的结果: