《导出Excel数据》doc版.doc_第1页
《导出Excel数据》doc版.doc_第2页
《导出Excel数据》doc版.doc_第3页
《导出Excel数据》doc版.doc_第4页
《导出Excel数据》doc版.doc_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

网站导航:免费论文 原创论文 论文搜索 作业答案 网学软件 学术大家 资料中心 会员中心 问题解答 定作论文 论文素材 设计下载 最新论文 下载排行 论文上传 在线投稿 联系我们 最新论文 推荐专题 热门论文 素材专题 网站首页设计下载关于网学程序设计设计资源论文模板网学软件网页素材网页模板精品课件游戏频道网学论坛原创论文网站设计交易代码C+ VB ASP VF DELPHI JSP 电气 计算机 经济 打包下载 查询工具 设计案例 教授专家 实用代码 网页配色 便捷工具 表单美化 免费论文 可用论文 论文培训 时间与效率 DIV技术 JS代码 百宝箱 设计师 AJAX代码 GIF素材 背景图片 网页图标 QQ表情 png图标 动画素材 PPT模板 DIVCSS模板 企业模板 韩国模板 日本模板 欧美模板 个人模板 节庆模板 实用工具 Psd素材 QQ专栏 CAJ文档 游戏评论 游戏攻略 游戏赏析 游戏前瞻 游戏新闻 游戏帮助 游戏秘籍 游戏补丁 国外课件 本科课件 网络课件 原创设计 原创下载 原创论文 网站维护(新) 虚拟实验,在线实验系统 口才培训 驾照考试 酒后代驾 精品书籍 反查论文 原创地图 设计定作 设计专题 计算机原创 网络教育 收录查询 治病救人 ASP,网站,C/S,设计等定作 栏目导航: 定作论文作品 网站策划 Silverlight 业界动态 实用代码 美工之家 .Net编程 数据库 Project MATLAB 当前位置: 网学 设计资源 .Net编程 正文 效率最高的Excel数据导入-(c#调用SSIS Package将数据库数据导入到Excel文件中【附源代码下载】来源:Http:/ 联系QQ: 作者: 佚名 来源: 网络 发布时间: 11/01/20 - 本文目录: (一)背景【源码下载】 (二)数据库数据导入到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(rowcol.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)rowcol.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; 方案三:采用Excel组件。一般,对于操作能够基本满足,但对于数据量大时可能会慢点。下面的代码,本人在原有基础上稍微修改了下,如下所示:public string OutputExceles(string strTitle, string FilePath, string typeName, System.Data.DataTable dtList, string smallTitleList) beforeTime = DateTime.Now; Excel.Application excel; Excel._Workbook xBk; Excel._Worksheet xSt; int rowIndex = 1; int colIndex = 1; excel = new Excel.ApplicationClass(); xBk = excel.Workbooks.Add(true); xSt = (Excel._Worksheet)xBk.ActiveSheet; int add=0; foreach (System.Data.DataTable dt in dtList) colIndex = 1; /取得整个报表的标题 excel.CellsrowIndex , 1 = smallTitleadd; add+; /设置整个报表的标题格式 xSt.get_Range(excel.CellsrowIndex, 1, excel.CellsrowIndex , dt.Columns.Count).Font.Bold = true; xSt.get_Range(excel.CellsrowIndex, 1, excel.CellsrowIndex , dt.Columns.Count).Font.Size = 22; /设置整个报表的标题为跨列居中 xSt.get_Range(excel.CellsrowIndex , 1, excel.CellsrowIndex , dt.Columns.Count).Select(); xSt.get_Range(excel.CellsrowIndex , 1, excel.CellsrowIndex, dt.Columns.Count).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenterAcrossSelection; rowIndex+; foreach (DataColumn col in dt.Columns) excel.CellsrowIndex, colIndex = col.ColumnName; /设置标题格式为居中对齐 xSt.get_Range(excel.CellsrowIndex, colIndex, excel.CellsrowIndex, colIndex).Font.Bold = true; xSt.get_Range(excel.CellsrowIndex, colIndex, excel.CellsrowIndex, colIndex).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter; xSt.get_Range(excel.CellsrowIndex, colIndex, excel.CellsrowIndex, colIndex).Select(); xSt.get_Range(excel.CellsrowIndex, colIndex, excel.CellsrowIndex, colIndex).Interior.ColorIndex = titleColorindex; colIndex+; /取得表格中的数据 foreach (DataRow row in dt.Rows) rowIndex+; colIndex = 1; foreach (DataColumn col in dt.Columns) if (col.DataType = System.Type.GetType(System.DateTime) if (!string.IsNullOrEmpty(rowcol.ColumnName.ToString() excel.CellsrowIndex, colIndex = (Convert.ToDateTime(rowcol.ColumnName.ToString().ToString(yyyy-MM-dd); xSt.get_Range(excel.CellsrowIndex, colIndex, excel.CellsrowIndex, colIndex).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter; else if (col.DataType = System.Type.GetType(System.String) excel.CellsrowIndex, colIndex = + rowcol.ColumnName.ToString(); xSt.get_Range(excel.CellsrowIndex, colIndex, excel.CellsrowIndex, colIndex).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;r; else excel.CellsrowIndex, colIndex = rowcol.ColumnName.ToString(); xSt.get_Range(excel.CellsrowIndex, colIndex, excel.CellsrowIndex, colIndex).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter; colIndex+; rowIndex +; afterTime = DateTime.Now; xSt.Name = strTitle; string filename = typeName + DateTime.Now.ToString(yyyyMMdd) + .xls; / excel.Save(FilePath+filename); excel.ActiveWorkbook.SaveCopyAs(FilePath + filename); #region 结束Excel进程 xBk.Close(null, null, null); excel.Workbooks.Close(); excel.Quit(); #endregion return filename; 方法四:采用DataGrid,GridView自带的属性。如下:private void ExportExcelFromDataGrid(string filename, System.Web.UI.WebControls.GridView ToExcelGrid) Response.ClearHeaders(); Response.Clear(); Response.Expires = 0; Response.Buffer = true; Response.HeaderEncoding = System.Text.Encoding.UTF8; / Response.Charset = utf-8; Response.AppendHeader(Content-Disposition, attachment;filename= + Server.UrlEncode(filename); Response.ContentEncoding = System.Text.Encoding.Default;/设置输出流为简体中文 / Response.ContentType = application/ms-excel;/设置输出文件类型为excel文件。 Response.ContentType = Application/octet-stream; this.EnableViewState = false; System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo(zh-CHS, true); System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); ToExcelGrid.RenderControl(oHtmlTextWriter); Response.Write(oStringWriter.ToString(); Response.End(); (三)SSIS的简介 SQL Server 2005 提供的一个集成化的商业智能开发平台,主要包括: SQL Server Analysis Services(SQL Server数据分析服务,简称SSAS) SQL Server Reporting Services(SQL Server报表服务,简称SSRS) SQL Server Integration Services(SQL Server数据集成服务,简称SSIS)SQL Server 2005 Integration Services (SSIS) 提供一系列支持业务应用程序开发的内置任务、容器、转换和数据适配器。可以创建 SSIS 解决方案来使用 ETL 和商业智能解决复杂的业务问题,管理 SQL Server 数据库以及在 SQL Server 实例之间复制 SQL Server 对象。(四)数据库中存储过程示例(SSIS应用过程中需要的,最好拿个本子把需要的内容记下) 在SQL SERVER 2005中,以SSISDataBase数据库作为应用,仅包括2张表City,Province.(主要是为了简单,便于讲解) 其中存储过程如下: ALTER PROCEDURE dbo.ProvinceSelectedCityInfo ( provinceId int=0 ) as begin select P.EName as 省份拼音,P.CName as 省份名,C.CName as 城市名 from City C left join Province P on C.ProvinceId = P.ProvinceId where C.ProvinceId =provinceId and provinceId is not null or provinceId is null or provinceId=0 end 其中,在这一步中我们必须要记住相关的内容,如上标识(红色);为什么这么做?主要是在制作SSIS包的时候很容易混淆,建议拿个本子把需要的内容写好。(五)Excel模板的制作(这步这么简单,稍微介绍一下) 因为SSIS中列映射对应的是Excel的标题,与数据是一对一的关系。先不管这么多,看下我们的模板,如下图所示。我们应该能够发现,省份拼音、省份名、城市名,还有ProvinceCityInfoExcel.xls,Sheet1都被笔者标识了,当然这一步与数据库中的存储过程取出的数据也是一对一的。(名称一致,可以减少很多不必要的麻烦,不然的话,嘿嘿.自己去想,那不是哥的事)等下,需要将创建的EXCEL模板放置到我们的项目文件目录中。(详见第七步)(六)SSIS操作过程(生成Package,用来调用) 这一步是最主要的过程,当然,也是很容易出错的一步。笔者会另外详细介绍制作Package包的过程,本文将直接将生成的包放到VS项目中进行运用。 利用SQL Server 2005数据库自带的SQL Server Business Intelligence Development Studio(SQL Server商业智能开发平台),最终生成的项目如下图所示: 然后,将在SSIS项目中生成的Package.dtsx包复制到自己的项目文件目录中。这就是我们马上进入的步骤了-(步骤七)(七)C#中调用SSIS创建的Package和Excel模板(可以自己编写逻辑代码进行重复利用),用来生成Excel数据 先看下我们的VS2008项目,如下图所示:大家会发现,笔者将(五)(六)步骤生成的模板和Package包放置在项目中的“Excel导出”目录下,当然这些文件随便你放在哪里,这是不用再废话的,哈哈。 另外,笔者简单的设计了如下很粗糙的界面,目的是根据省份来显示城市的相关信息(其实大家都是很熟悉这些的,很多项目都是有省-市-县数据库表的),添加一个导出按钮,点击的时候,我们可以参考页面显示的内容和我们生成的客户端Excel中的内容是否一致。现在我们的重头戏开始了,如下代码(点击将触发的代码内容):public string OutputExceles(string strTitle, string FilePath, string typeName, System.Data.DataTable dtList, string smallTitleList) beforeTime = DateTime.Now; Excel.Application excel; Excel._Workbook xBk; Excel._Worksheet xSt; int rowIndex = 1; int colIndex = 1; excel = new Excel.ApplicationClass(); xBk = excel.Workbooks.Add(true); xSt = (Excel._Worksheet)xBk.ActiveSheet; int add=0; foreach (System.Data.DataTable dt in dtList) colIndex = 1; /取得整个报表的标题 excel.CellsrowIndex , 1 = smallTitleadd; add+; /设置整个报表的标题格式 xSt.get_Range(excel.CellsrowIndex, 1, excel.CellsrowIndex , dt.Columns.Count).Font.Bold = true; xSt.get_Range(excel.CellsrowIndex, 1, excel.CellsrowIndex , dt.Columns.Count).Font.Size = 22; /设置整个报表的标题为跨列居中 xSt.get_Range(excel.CellsrowIndex , 1, excel.CellsrowIndex , dt.Columns.Count).Select(); xSt.get_Range(excel.CellsrowIndex , 1, excel.CellsrowIndex, dt.Columns.Count).HorizontalAlignment = Excel.XlHAlign.xlHAlignCenterAcrossSelection; rowIndex+; foreach (DataColumn col in dt.Columns) excel.CellsrowIndex, colIndex = col.ColumnName; /设置标题格式为居中对齐 xSt.get_Range(excel.CellsrowIndex, colIndex, excel.CellsrowIndex, colIndex).Font.Bold = true; xSt.get_Range(excel.CellsrowIndex, colIndex, excel.CellsrowIndex, colIndex).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter; xSt.get_Range(excel.CellsrowIndex, colIndex, excel.CellsrowIndex, colIndex).Select(); xSt.get_Range(excel.CellsrowIndex, colIndex, excel.CellsrowIndex, colIndex).Interior.ColorIndex = titleColorindex; colIndex+; /取得表格中的数据 foreach (DataRow row in dt.Rows) rowIndex+; colIndex = 1; foreach (DataColumn col in dt.Columns) if (col.DataType = System.Type.GetType(System.DateTime) if (!string.IsNullOrEmpty(rowcol.ColumnName.ToString() excel.CellsrowIndex, colIndex = (Convert.ToDateTime(rowcol.ColumnName.ToString().ToString(yyyy-MM-dd); xSt.get_Range(excel.CellsrowIndex, colIndex, excel.CellsrowIndex, colIndex).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter; else if (col.DataType = System.Type.GetType(System.String) excel.CellsrowIndex, colIndex = + rowcol.ColumnName.ToString(); xSt.get_Range(excel.CellsrowIndex, colIndex, excel.CellsrowIndex, colIndex).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;r; else excel.CellsrowIndex, colIndex = rowcol.ColumnName.ToString(); xSt.get_Range(excel.CellsrowIndex, colIndex, excel.CellsrowIndex, colIndex).HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter; colIndex+; rowIndex +; afterTime = DateTime.Now; xSt.Name = strTitle; string filename = typeName + DateTime.Now.ToString(yyyyMMdd) + .xls; / excel.Save(FilePath+filename); excel.ActiveWorkbook.SaveCopyAs(FilePath + filename); #region 结束Excel进程 xBk.Close(null, null, null); excel.Workbooks.Close(); excel.Quit(); #endregion return filename; 你肯定会说:“哥,你这个也太简单了吧?”。就是这么简单,不就是多写一个类给你调用就可以了吗。调用接口,这个你总会吧。不过你得了解各个参数才行。 首先,我们必须引用2个DLL,Microsoft.SQLServer.ManagedDTS.dll和Microsoft.SqlServer.DTSPipelineWrap.dll(系统自带的)。先看下我们生成Excel文件数据的步骤,如下:/ / 导出数据到EXCEL文件中 / / / 执行包的传入参数 / 生成的Excel的文件 / SSIS包名称 / SSIS EXCEL模板名称 / 生成的Excel的文件前缀 / public bool ExportDataBySsis(string rootPath, string sqlParams, out string tempExcelName, string packageName, string execlFileName, string createdExeclPreName) /数据包和EXCEL模板的存储路径 string path = rootPath + Excel导出; /强制生成目录 if (!System.IO.Directory.Exists(path) System.IO.Directory.CreateDirectory(path); /返回生成的文件名 string copyFile = this.SaveAndCopyExcel(path, execlFileName, createdExeclPreName); tempExcelName = copyFile; /SSIS包路径 string ssisFileName = path + packageName; /执行-把数据导入到Excel文件 return ExecuteSsisDataToFile(ssisFileName, tempExcelName, sqlParams); 代码注释如此清楚,想必也不需要再多做解释了吧,下面就是最最最重要的一步,需要看清楚了-private bool ExecuteSsisDataToFile(string ssisFileName, string tempExcelName, string sqlParams) Application app = new Applicatio

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论