zoukankan      html  css  js  c++  java
  • 使用SSIS生成数据导出为Excel文件的模板

    关键脚本(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         }
    View Code

    创建excel文件和工作表

     1  /// <summary>
     2         /// 创建excel文件和工作表
     3         /// </summary>
     4         /// <param name="excelFileName">Excel文件存放目录</param>
     5         /// <param name="sql_script_ddl">ddl建表脚本</param>
     6         private void CreateExcelFile(string excelFileName,string sql_script_ddl)
     7         {
     8             String sConnectionString = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=YES'",excelFileName);
     9             System.IO.File.WriteAllText(System.IO.Path.GetDirectoryName(excelFileName) + "\sConnectionString.txt", sConnectionString, System.Text.Encoding.UTF8);
    10 
    11             OleDbConnection cn = new OleDbConnection(sConnectionString);
    12             string sqlCreate = sql_script_ddl;
    13             OleDbCommand cmd = new OleDbCommand(sqlCreate, cn);
    14             //创建Excel文件
    15             cn.Open();
    16             cmd.ExecuteNonQuery();
    17             //cmd.CommandText = "INSERT INTO TestSheet VALUES(1,'elmer','password')";
    18             //cmd.ExecuteNonQuery();
    19             cn.Close();
    20             cn.Dispose();
    21         }
    View Code

    生成新的文件名称:

    1         /// <summary>
    2         /// (d:exportData20170201.xlsx
    3         /// </summary>
    4         /// <param name="filePattern">格式化前的文件名称模板</param>
    5         /// <returns>新的文件名称</returns>
    6         private string GetFileName(string filePattern)
    7         {
    8             return string.Format(filePattern, DateTime.Now.ToString("yyyyMMddHHmm"));
    9         }
    View Code
  • 相关阅读:
    SpringBoot整合Ehcache
    SpringCache整合Redis
    SpringBoot使用mongo搭建RESTful风格
    SpringBoot使用JPA搭建RESTful风格
    SpringBoot操作mongo的两种方法
    Nginx负载均衡
    【Java杂货铺】用Security做权限极简入门
    【Java杂货铺】JVM#虚拟机加载机制
    【Java杂货铺】JVM#Class类结构
    【Java杂货铺】JVM#Java高墙之GC与内存分配策略
  • 原文地址:https://www.cnblogs.com/xakml/p/6393543.html
Copyright © 2011-2022 走看看