关键脚本(C#语言)
1 /// <summary> 2 /// This method is called when this script task executes in the control flow. 3 /// Before returning from this method, set the value of Dts.TaskResult to indicate success or failure. 4 /// To open Help, press F1. 5 /// </summary> 6 public void Main() 7 { 8 // TODO: Add your code here 9 //string ssis_file_folder = @"d:\ssis_demo_workspace"; 10 11 string ddl_script = Dts.Variables["User::Var_ExcelDDL"].Value.ToString();//创建工作表的脚本 12 string excel_Folder = Dts.Variables["User::Var_ExcelFolder"].Value.ToString(); //Excel文件的存放目录 13 string excel_fileName_template = Dts.Variables["User::Var_ExcelFileName"].Value.ToString();//Excel文件名称模板 14 string ddl_script_file = Dts.Variables["User::Var_ExcelDDL_File"].Value.ToString();//创建工作表的脚本文件 15 string Param_ExcelDDL = Dts.Variables["$Package::Param_ExcelDDL"].Value.ToString(); 16 17 if (string.IsNullOrWhiteSpace(excel_Folder)) 18 { 19 Dts.TaskResult = (int)ScriptResults.Failure; 20 return; 21 } 22 23 if (string.IsNullOrWhiteSpace(excel_fileName_template)) 24 { 25 Dts.TaskResult = (int)ScriptResults.Failure; 26 return; 27 } 28 29 #region if excel folder not exists then create it 30 try 31 { 32 if (!System.IO.Directory.Exists(excel_Folder)) 33 System.IO.Directory.CreateDirectory(excel_Folder); 34 } 35 catch (Exception ex) 36 { 37 System.IO.File.WriteAllText(excel_Folder + "\\create_excel_log.txt", ex.Message, System.Text.Encoding.UTF8); 38 Dts.TaskResult = (int)ScriptResults.Failure; 39 } 40 #endregion 41 42 if (string.IsNullOrWhiteSpace(ddl_script)) 43 { 44 Dts.TaskResult = (int)ScriptResults.Failure; 45 return; 46 } 47 else 48 { 49 System.IO.File.WriteAllText(excel_Folder + "\\ddl_log.txt", ddl_script,System.Text.Encoding.UTF8); 50 } 51 //读取脚本文件中的脚本 52 if (System.IO.File.Exists(ddl_script_file)) 53 { 54 ddl_script = System.IO.File.ReadAllText(ddl_script_file, System.Text.Encoding.UTF8); 55 } 56 57 string file_name = this.GetFileName(excel_Folder +"\\"+ excel_fileName_template);// 合成Excel文件的绝对路径 58 System.IO.File.WriteAllText(excel_Folder + "\\file_name.txt", file_name, System.Text.Encoding.UTF8); 59 60 try 61 { 62 // TODO: Add your code here 63 if (System.IO.File.Exists(file_name)) 64 System.IO.File.Delete(file_name); 65 66 this.CreateExcelFile(file_name, ddl_script); 67 Dts.TaskResult = (int)ScriptResults.Success; 68 } 69 catch (Exception ex) 70 { 71 System.IO.File.WriteAllText(excel_Folder + "\\log.txt", ex.GetType() + Environment.NewLine + ex.Message + Environment.NewLine+ ex.StackTrace, System.Text.Encoding.UTF8); 72 Dts.TaskResult = (int)ScriptResults.Failure; 73 return; 74 } 75 Dts.TaskResult = (int)ScriptResults.Success; 76 System.IO.File.WriteAllText(excel_Folder + "\\Param_ExcelDDL.txt", Param_ExcelDDL, System.Text.Encoding.UTF8); 77 78 }