C#导出Excel几个例子

工程-〉引用   -〉选中microsoft   excel   X.0   object   library  
   
  其中X代表数字,一般9.0以上,根据你的操作系统以及office版本而定

C#将DataSet中的数据写入Excel中

#region 导出 Excel 文件
///<summary>
/// 导出 Excel 文件
/// </summary>
/// <param name="ds">要导出的DataSet</param>
/// <param name="strExcelFileName">要导出的文件名</param>

private void ExportExcel(DataSet ds,string strExcelFileName)
{
    
object objOpt = Missing.Value;
    Application excel 
= new Application();
    excel.Visible 
= true;
    _Workbook wkb 
= excel.Workbooks.Add(objOpt);
    _Worksheet wks 
= (_Worksheet)wkb.ActiveSheet;

    wks.Visible 
= XlSheetVisibility.xlSheetVisible;
    
    
int rowIndex=1;
    
int colIndex=0;

    DataTable table
=ds.Tables[0] ;
    
foreach(DataColumn col in table.Columns)
    
{
        colIndex
++;    
        excel.Cells[
1,colIndex]=col.ColumnName;                
    }


    
foreach(DataRow row in table.Rows)
    
{
        rowIndex
++;
        colIndex
=0;
        
foreach(DataColumn col in table.Columns)
        
{
            colIndex
++;
            excel.Cells[rowIndex,colIndex]
=row[col.ColumnName].ToString();
        }

    }

    
//excel.Sheets[0] = "sss";
    wkb.SaveAs(strExcelFileName,objOpt,null,null,false,false,XlSaveAsAccessMode.xlNoChange,null,null,null,null,null);
    wkb.Close(
false,objOpt,objOpt);
    excel.Quit();
}

#endregion

 

这是一个使用Excel中查询分析器(Ms通用查询分析器)完成从SQL Server 7.0以上版本(已通过测试)的数据快速导出到Excel中的示例。它由两个参数完成,其中的第一个是你所要进行查询分析时使用的Select查询语句。为了好看,我们给我们导出的数据加上一个名称。名称,由第二个参数传递进来:)好了,不说什么废话了。大家看代码吧。

using System;
using Excel;
namespace 类库
{
 public class Excel导出
 {
  public Excel导出(string 查询语句,string 标题)
  {
            Excel.Application excel;
            Excel._Workbook xBk;
            Excel._Worksheet xSt;
            Excel._QueryTable xQt;
            string Conn = "ODBC;DRIVER=SQL Server;SERVER=[服务器地址或者名称];UID=sa;PWD=[密码];APP=[应用程序名称(一般为操作系统名)];WSID=[工作站名称(客户端)];DATABASE=[数据库名称]";
            string Select = 查询语句;
            excel = new Excel.ApplicationClass();
            xBk = excel.Workbooks.Add(true);
            xSt = (Excel._Worksheet)xBk.ActiveSheet;
            excel.Cells[2,2] = 标题;
            xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Bold = true;
            xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Name = "黑体";
            xSt.get_Range(excel.Cells[2,2],excel.Cells[2,2]).Font.Size = 22;
            xQt = xSt.QueryTables.Add(Conn,xSt.get_Range(excel.Cells[4,2],excel.Cells[4,2]),Select);
            xQt.Name = "导出示例";
            xQt.FieldNames = true;
            xQt.RowNumbers = false;
            xQt.FillAdjacentFormulas = false;
            xQt.PreserveFormatting = false;
            xQt.BackgroundQuery = true;
            xQt.RefreshStyle = Excel.XlCellInsertionMode.xlInsertDeleteCells;
            xQt.AdjustColumnWidth = true;
            xQt.RefreshPeriod = 0;
            xQt.PreserveColumnInfo = true;
            xQt.Refresh(xQt.BackgroundQuery);
            excel.Visible = true;
  }
 }
}

全中文的,不用进行解释了吧?

原来进行数据导出操作(相关连接http://www.csdn.net/Develop/Read_Article.asp?Id=21391),三百条记录,用时十分钟以上。如果使用Excel自带的这一个查询工具,导出一万条记录,只需十秒钟以内的时间,而且,可以完成格式自动排版的功能。

可能有人会问:Excel里面的查询语句与SQL Server里面的查询语句是不是一样的?这里说明一点。使用这个类,可以直接使用SQL Server里面的查询语句,包括直接传递SQL Server的存储过程。

protected void DoTranExcel( System.Data.DataSet ExelDt)
{
int colIndex=1,rowIndex=1;
Excel.Application excel;
try
{
excel=new Excel.Application( ); 
excel.Application.Workbooks.Add (true) ;
excel.Visible = true;
}
catch
{
MessageBox.Show("您可能没有安装Office,请安装再使用该功能");
return;
}
//foreach(DataColumn col in this.ExelDt.Tables[0].Columns)
//{
//excel.Cells[1,colIndex]=col.ColumnName; colIndex++; 
try
{
//}
for(int i=0;i<this.xdg.TableStyles[0].GridColumnStyles.Count;i++)
{
excel.Cells[1,colIndex]=this.xdg.TableStyles[0].GridColumnStyles[i].HeaderText; colIndex++; 
}
foreach(DataRow row in ExelDt.Tables[0].Rows) 

rowIndex++; colIndex=0; 
foreach(DataColumn col in ExelDt.Tables[0].Columns) 

colIndex++;
if(colIndex ==1)
{
excel.Cells[rowIndex,colIndex]="'"+row[col.ColumnName].ToString();
}
else
{
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}

}
}
catch(System.Exception)
{
MessageBox.Show("输出Excel有错误,请确认没有关闭Excel");
return;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值