本文目录:

   (一)背景

   (二)数据库数据导入到Excel的方法比较
   (三)SSIS的简介
   (四)数据库中存储过程示例(SSIS应用需要)

   (五)Excel模板的制作(这步这么简单,稍微介绍一下)
   (六)SSIS操作过程(生成Package,用来调用)(下一篇随笔将详细讲解制作Package包的过程,图片太多,篇幅过长,因此本文将直接采用生成的Package包进行应用)

   (七)C#中如何调用SSIS创建的Package和Excel模板(可以自己编写逻辑代码进行重复利用),用来生成Excel数据

   (八)总结

(一)背景

     如何将数据库中的数据导入到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)
                {
                    rowIndex
++;
                    colIndex 
= 0;
                    
foreach (DataColumn col in dt.Columns)
                    {
                        colIndex
++;
                        
if (col.DataType == System.Type.GetType("System.DateTime"))
                        {     
                            
object str = (object)(Convert.ToDateTime(row[col.ColumnName].ToString())).ToString("yyyy-MM-dd"); ;
                            excel.WriteValue(
ref vt, ref cf, ref ca, ref chl, ref rowIndex, ref colIndex, ref str, ref cellformat);
                        }
                        
else
                        {
                            
object str = (object)row[col.ColumnName].ToString();
                            excel.WriteValue(
ref vt, ref cf, ref ca, ref chl, ref rowIndex, ref colIndex, ref str, ref cellformat);
                        }
                    }
                }
                rowIndex 
+= 3 ;
            }
            
int ret = excel.CloseFile();          
            
return FilePath+filename;
        }

相关文章:

  • 2021-04-11
  • 2022-02-15
  • 2021-06-08
  • 2021-12-30
  • 2021-04-07
  • 2021-07-24
  • 2021-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-26
  • 2021-06-26
  • 2022-12-23
  • 2021-04-17
相关资源
相似解决方案