将数据从SQLserver导出到Excel

以下都只是介绍操作的原理,具体要求要在应用中具体分析改变。

1.       此方法常用在form或者Console Application中,使用时须用要添加Reference,具体做法:

          右键点击项目添加“Add Reference”,在Tom组件下,选择“Microsoft Excel 14.0 Object Library”,然后在项目中使用     

         下面注释//it looks like excele table start with 1 not 1

         应该为//it looks like excele table start with 1 not 0    

         
[csharp]  view plain copy
  1. private static void exportToExcel(DataTable dt)  
  2.         {  
  3.             Excel.Application excel=new Excel.Application();  
  4.             excel.Application.Workbooks.Add(true);  
  5.             excel.Visible = true;  
  6.   
  7.             //get the columns  
  8.             for (int i = 0; i < dt.Columns.Count;i++ )  
  9.             {  
  10.                 //here is started with 1  
  11.                 //it looks like excele table start with 1 not 1  
  12.                 excel.Cells[1, i + 1] = dt.Columns[i].ColumnName.ToString();   
  13.             }  
  14.   
  15.             //get the data in rows  
  16.             for (int row = 0; row < dt.Rows.Count;row++ )  
  17.             {  
  18.                 for (int col = 0; col < dt.Columns.Count; col++)  
  19.                 {  
  20.                     excel.Cells[row+2, col+1] = dt.Rows[row][dt.Columns[col]].ToString();  
  21.                 }  
  22.             }  
  23.             //FolderBrowserDialog path = new FolderBrowserDialog();//打开文件对话框  
  24.             //path.ShowDialog();  
  25.             //textBox1.Text = path.SelectedPath;//选择文件夹  
  26.   
  27.             //save excel  
  28.             //excel.SaveWorkspace();  
  29.   
  30.             excel.Quit();  
  31.         }  


2. 在web应用中,可通过HttpContext.Response.write()来实现

[csharp]  view plain copy
  1. protected static void  toExcel(DataTable da){  
  2.         System.Web.HttpContext context = System.Web.HttpContext.Current;  
  3.         context.Response.Clear();  
  4.   
  5.         foreach( DataColumn colum in da.Columns){  
  6.             context.Response.Write(colum.ColumnName+"\t");  
  7.         }  
  8.   
  9.         context.Response.Write(System.Environment.NewLine);  
  10.   
  11.         foreach (DataRow row in da.Rows) {  
  12.             for (int i = 0; i < da.Rows.Count; i++)  
  13.             {  
  14.                 context.Response.Write(row[i].ToString()+"\t");  
  15.             }  
  16.             context.Response.Write(System.Environment.NewLine);  
  17.         }  
  18.         context.Response.ContentType = "application/vnd.ms-excel";  
  19.         context.Response.AppendHeader("Content-Disposition""attachment; filename=plan.xls");  
  20.         context.Response.End();  
  21.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值