DtToExcel

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

          右键点击项目添加“Add Reference”,在com组件下,选择“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  

   

?
private static void exportToExcel(DataTable dt)
         {
             Excel.Application excel= new Excel.Application();
             excel.Application.Workbooks.Add( true );
             excel.Visible = true ;
  
             //get the columns
             for ( int i = 0; i < dt.Columns.Count;i++ )
             {
                 //here is started with 1
                 //it looks like excele table start with 1 not 1
                 excel.Cells[1, i + 1] = dt.Columns[i].ColumnName.ToString(); 
             }
  
             //get the data in rows
             for ( int row = 0; row < dt.Rows.Count;row++ )
             {
                 for ( int col = 0; col < dt.Columns.Count; col++)
                 {
                     excel.Cells[row+2, col+1] = dt.Rows[row][dt.Columns[col]].ToString();
                 }
             }
             //FolderBrowserDialog path = new FolderBrowserDialog();//打开文件对话框
             //path.ShowDialog();
             //textBox1.Text = path.SelectedPath;//选择文件夹
  
             //save excel
             //excel.SaveWorkspace();
  
             excel.Quit();
         }

 

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

 

?
protected static void  toExcel(DataTable da){
         System.Web.HttpContext context = System.Web.HttpContext.Current;
         context.Response.Clear();
  
         foreach ( DataColumn colum in da.Columns){
             context.Response.Write(colum.ColumnName+ "\t" );
         }
  
         context.Response.Write(System.Environment.NewLine);
  
         foreach (DataRow row in da.Rows) {
             for ( int i = 0; i < da.Rows.Count; i++)
             {
                 context.Response.Write(row[i].ToString()+ "\t" );
             }
             context.Response.Write(System.Environment.NewLine);
         }
         context.Response.ContentType = "application/vnd.ms-excel" ;
         context.Response.AppendHeader( "Content-Disposition" , "attachment; filename=plan.xls" );
         context.Response.End();
     }

 

 3.bcp命令是SQL Server提供的一个快捷的数据导入导出工具。使用它不需要启动任何图形管理工具就能以高效的方式导入导出数据。bcp是SQL Server中负责导入导出数据的一个命令行工具,它是基于DB-Library的,并且能以并行的方式高效地导入导出大批量的数据。bcp可以将数据库的表或视图直接导出,也能通过SELECT FROM语句对表或视图进行过滤后导出。在导入导出数据时,可以使用默认值或是使用一个格式文件将文件中的数据导入到数据库或将数据库中的数据导出到文件中.

?
use swangtest
Go
  
SP_CONFIGURE 'show advanced options' ,1
RECONFIGURE
Go
SP_CONFIGURE 'xp_cmdshell' ,1
RECONFIGURE
Go
EXEC master..xp_cmdshell 'BCP  swangTest..userinfo OUT  D:\entryId.csv -c -t -T '

 

 

转载于:https://www.cnblogs.com/a1991322/archive/2012/09/05/2671869.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值