在c#中导出到excel中的说明

关于在c#中导出到excel中的说明

在c#中导出到excel可以使用两种方式,在导出少量数据时采用方式一,在导出大量数据时采用方式二。
方式一:使用创建excel对象,直接对excel进行读写操作,不过写数据时是一个单元格一个单元格的方式写的,效率很低。
代码如下:
先在项目中添加Microsoft Excel 
9.0 Object Library的引用,然后在代码中添加引用:using Excel;
ApplicationClass myApp
=null;
            Workbook myBook
=null;
            Worksheet mySheet1
=null;
        myApp
= new ApplicationClass();
            myApp.Visible
=false;
                
            
object oMissiong=System.Reflection.Missing.Value;            myApp.Workbooks.Open(path,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong,oMissiong);
            myBook
=myApp.Workbooks[1];
            mySheet1
=(Worksheet)myBook.Worksheets[1];
int iSheet=BusinessFacade.DepreciateSystem.GetpaSheet1Count(this.drpMonth.SelectedItem.Value,this.drpAnual.SelectedItem.Value)/65000;
            
for(int i=1;i<iSheet+1;i++)
            
{
                mySheet1.Copy(Type.Missing,mySheet1);
            }

            
for(int i=1;i<iSheet+1;i++)
            
{
                mySheet1
=(Worksheet)myBook.Worksheets[i+1];
                mySheet1.Name
=""Sheet1""+i.ToString();
            }

forint i=1;i<dt.Columns.Count;i++ )
            
{
                mySheet1.Cells[
1,i] = dt.Columns[i].ColumnName.ToString();
}

            
try
            
{
                myBook.Save();
            }

            
catch
            
{
                
this.lblMessage.Visible = true;
                
this.lblMessage.Text = ""出现错误,请重试!"";
            }

            myBook.Close( 
true,path,true);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(mySheet1);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(myApp);

方式二:使用存储过程。
存储过程中提供四个基本参数:
@Excel:要写入的excel文件的路径及名称
@SheetName:要写入excel的sheet页的名称
@TableName:数据源的表的名称,是临时创建的表,其中存放要写入excel的数据
@iSheet:表示是第几个sheet页,也就是导入的excel中的记录超过65535时会出错的,因为excel的行的限制是65535,所以可以在程序中判断需要导入到几个sheet中,就需要把这个存储过程执行几次,每次传入不同的参数即可。一般数据小于65535时默认传入0
需要注意的是:
1、    该存储过程是遍历表中的字段,导出到excel中的,就会把表中的所有字段都会导入到excel中,如果不希望导入所有的字段,需要手动加工select语句。
2、    因为在导入的字段超过26列则会出现错位的现象,即插入的列的顺序和select语句中的列的顺序不一制,所以需要在程序中先使用方式一把列名写入按顺序excel中,然后在存储过程中的insert into语句中限制列名。
3、    导出到excel时,excel中每列的值的类型都会默认和上一行该列的类型一直,所以导入的所有数据都是以文本形式存储的。

存储过程内容如下:
CREATE          PROCEDURE ExportExcel
@excel nvarchar(
1000),@sheetname nvarchar(100)
,@tbname sysname,@strMonth nvarchar(
50),@strYear nvarchar(50),@iSheet integer
 AS
declare @s nvarchar(
4000)
declare @iTemp integer
begin
    
set @s=''
    select @s
=@s+','+name+'' from syscolumns
    where id
=object_id(@tbname)        
        
--and colid between @i*10+1 and (@i+1)*10
    order by colid
select @iTemp
=65000*@iSheet
    select @s
=stuff(@s,1,4,'')
    exec(
'insert into OPENROWSET(''MICROSOFT.JET.OLEDB.4.0''
        ,''Excel 8.0;HDR=YES;DATABASE='+@excel+''',['
        +@sheetname+'$])
('+@s+')
select top 
65000  '+@s+' from ['+@tbname+'] where  id not in (select top '+@iTemp+' id from ['+@tbname+'] ) and year([date])='+@strYear+' and month([date])='+@strMonth+''
    )
end
GO
posted @ 2006-08-28 16:23  快乐的老毛驴  阅读(376)  评论(0编辑  收藏  举报