c#怎么调用SSIS Package将数据库数据导入

45次阅读
没有评论

共计 2551 个字符,预计需要花费 7 分钟才能阅读完成。

本篇内容主要讲解“c# 怎么调用 SSIS Package 将数据库数据导入”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让丸趣 TV 小编来带大家学习“c# 怎么调用 SSIS Package 将数据库数据导入”吧!

(一)背景

  如何将数据库中的数据导入到 EXCEL 文件中,我们经常会碰到。本文将比较常用的几种方法,并且将详细讲解基于 SSIS 的用法。笔者认为,基于 SSIS 的方法,对于海量数据来说,应该是效率最好的一种方法。个人认为,这是一种值得推荐的方法,因此,本人决定将本人所知道的、以及自己总结的完整的写出来,一是提高一下自己的写作以及表达能力,二是让更多的读者能够在具体的应用中如何解决将海量数据导入到 Excel 中的效率问题。

(二)方法的比较

  方案一:SSIS(SQL Server 数据集成服务),追求效率,Package 制作过程复杂一点(容易出错)。

  方案二:采用 COM.Excel 组件。一般,对于操作能够基本满足,但对于数据量大时可能会慢点。下面的代码,本人稍微修改了下,如下所示:该方法主要是对单元格一个一个的循环写入,基本方法为 excel.WriteValue(ref vt, ref cf, ref ca, ref chl, ref rowIndex, ref colIndex, ref str, ref cellformat)。当数据量大时,肯定效率还是有影响的。

 
 public string DataExcels(System.Data.DataTable[] dts, string strTitle, string FilePath, Hashtable nameList,string[] titles)
  {
  COM.Excel.cExcelFile excel = new COM.Excel.cExcelFile();
  // 当文件大于 10 的时候   清空所有文件!!!
  ClearFile(FilePath);
  // 文件名
  string filename = strTitle+ DateTime.Now.ToString(yyyyMMddHHmmssff) + .xls
  // 生成相应的文件
  excel.CreateFile(FilePath + filename);
  // 设置 margin
  COM.Excel.cExcelFile.MarginTypes mt1 = COM.Excel.cExcelFile.MarginTypes.xlsTopMargin;
  COM.Excel.cExcelFile.MarginTypes mt2 = COM.Excel.cExcelFile.MarginTypes.xlsLeftMargin;
  COM.Excel.cExcelFile.MarginTypes mt3 = COM.Excel.cExcelFile.MarginTypes.xlsRightMargin;
  COM.Excel.cExcelFile.MarginTypes mt4 = COM.Excel.cExcelFile.MarginTypes.xlsBottomMargin;
  double height = 2.2;
  excel.SetMargin(ref mt1, ref height);
  excel.SetMargin(ref mt2, ref height);
  excel.SetMargin(ref mt3, ref height);
  excel.SetMargin(ref mt4, ref height);
  // 设置字体!!
  COM.Excel.cExcelFile.FontFormatting ff = COM.Excel.cExcelFile.FontFormatting.xlsNoFormat;
  string font = 宋体
  short fontsize = 14;
  excel.SetFont(ref font, ref fontsize, ref ff);
  byte b1 = 1, b2 = 12;
  short s3 = 12;
  excel.SetColumnWidth(ref b1, ref b2, ref s3);

  string header = 页眉
  string footer = 页脚
  excel.SetHeader(ref header);
  excel.SetFooter(ref footer);

  COM.Excel.cExcelFile.ValueTypes vt = COM.Excel.cExcelFile.ValueTypes.xlsText;
  COM.Excel.cExcelFile.CellFont cf = COM.Excel.cExcelFile.CellFont.xlsFont0;
  COM.Excel.cExcelFile.CellAlignment ca = COM.Excel.cExcelFile.CellAlignment.xlsCentreAlign;
  COM.Excel.cExcelFile.CellHiddenLocked chl = COM.Excel.cExcelFile.CellHiddenLocked.xlsNormal;
  // 报表标题
  int cellformat = 1; 
  int rowIndex = 1;// 起始行
  int colIndex = 0;
  foreach (System.Data.DataTable dt in dts)
  { 
  colIndex = 0;
  // 取得列标题  
  foreach (DataColumn colhead in dt.Columns)
  {
  colIndex++;
  string name = colhead.ColumnName.Trim();
  object namestr = (object)name;
  excel.WriteValue(ref vt, ref cf, ref ca, ref chl, ref rowIndex, ref colIndex, ref namestr, ref cellformat);
  }
  // 取得表格中的数据  
  foreach (DataRow row in dt.Rows)
  {       

到此,相信大家对“c# 怎么调用 SSIS Package 将数据库数据导入”有了更深的了解,不妨来实际操作一番吧!这里是丸趣 TV 网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-19发表,共计2551字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)