我的开发环境是Visual Studio 2003,C#,WinForm下。
前提是都需要引用EXCEL2003的什么组件,在哪里有?在程序里都需要引用什么?求大家帮帮忙,谢谢了!

解决方案 »

  1.   

    搜索一下帖子~~
    倒BAIDU去搜索一下~
    这个案例很多的~我以前也写过
      

  2.   

    不用任何组件, 自己生成excel文档就可以了.可以通过很多方式耐操作excel给你转贴一个吧
    首先将excel.exe copy 到 ..\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin目录下
    利用.net 中带的工具在命令提示符下执行tlbimp excel.exe.这样就不会因为你的Excel是xp或2000的不同要去找不同的*.olb文件,还有一点就是因为在2000以后的版本中没有了excel9.olb这个文件了。通过执行tlbimp excel.exe后我们会得到excel.dll文件。只要有了这个Excel.dll,现在我们就能使用Excel的各种操作函数了。 
    下面就让我们具体看看C#是如何使用这些东东吧。 
    1. 创建一个新Excel的Application: Application exc = new Application();
    if (exc == null) {
    Console.WriteLine("ERROR: EXCEL couldn't be started");
    return 0;
    }2. 让这个工程可见: 
    exc.set_Visible(0, true); 
    3. 获取WorkBooks集合: 
    Workbooks workbooks = exc.Workbooks; 
    4. 加入新的WorkBook: 
    _Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0); 
    5. 获取WorkSheets集合: _Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
    if (worksheet == null) {
    Console.WriteLine ("ERROR in worksheet == null");
    }
    6. 给单元格设置变量:  Range range1 = worksheet.get_Range("C1",Missing.Value);
    if (range1 == null) 
    {
    Console.WriteLine ("ERROR: range == null");
    }
    const int nCells = 1;
    Object[] args1 = new Object[1];
    args1[0] = nCells;
    range1.GetType().InvokeMember("Value",BindingFlags.SetProperty, null, range1, args1);
     例程:  using System;
    using System.Reflection; 
    using System.Runtime.InteropServices; 
    using Excel;
    class Excel {
    public static int Main() {
    Application exc = new Application();
    if (exc == null) {
    Console.WriteLine("ERROR: EXCEL couldn't be started!");
    return 0;
    }
    exc.set_Visible(0, true); 
    Workbooks workbooks = exc.Workbooks;
    _Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet, 0); 
    Sheets sheets = workbook.Worksheets;
    _Worksheet worksheet = (_Worksheet) sheets.get_Item(1);
    if (worksheet == null) {
    Console.WriteLine ("ERROR: worksheet == null");
    }
    Range range1 = worksheet.get_Range("C1", Missing.Value);
    if (range1 == null) {
    Console.WriteLine ("ERROR: range == null");
    }
    const int nCells = 1;
    Object[] args1 = new Object[1];
    args1[0] = nCells;
    range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null,range1, args1);
    return 100;
    }
    }现在我们来看看如何使用数组,他有些类似于设置单元格。仅仅需要的改变只是args2[0] = array2; 
    const int nCell = 5;
    Range range2 = worksheet.get_Range("A1", "E1");
    int[] array2 = new int [nCell];
    for (int i=0; i < array2.GetLength(0); i++) 
    {
    array2[i] = i+1;
    }
    Object[] args2 = new Object[1];
    args2[0] = array2;
    range2.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range2, args2);  大家需要了解Tlbimp这个工具的使用啊:)这个东东很有用,可以将普通Win32程序移植到.Net下面来:) 
    如果操作的excel的格式很简单,就是一般的表的结构,那么其实操作EXCEL文件跟操作ACCESS数据库文件的方法几乎一样。
    (需要注意的地方就是,1、程序会把EXCLE表中的第一行记录作为列名;2、在使用EXCLE表的时候, 要在表名后面加上符号$)下面,我给你帖一段如何连接和读取EXCEL文件的代码吧:
     DataSet ds = new DataSet();
    OleDbDataAdapter ad;
    string strDbPath = "./code.xls";
    string strConn = "Provider=Microsoft.Jet.OleDb.4.0; Data Source="+Server.MapPath(strDbPath)+"; Extended Properties=Excel 8.0;";OleDbConnection Conn = new OleDbConnection(strConn);Conn.Open();string strSQL = "select * from [股票代码$]";
    ad = new OleDbDataAdapter(strSQL, Conn);
    ad.Fill(ds);dg1.DataSource = ds.Tables[0].DefaultView;  //dg1是一个DataGrid控件
    dg1.DataBind();  //将EXCLE中股票代码中的记录棒定到DataGrid控件上
    如果是在asp.net 下使用的话,要记得在  <system.web>中添加<identity impersonate="true"/>
    否则就会出现 “异常详细信息: System.UnauthorizedAccessException: 拒绝访问“。
      

  3.   

    //// <summary>
    /// 执行导出 ,请在项目中先引用Mircorsoft Excel library 11.0
    /// </summary>
    /// <param name="ds">要导出的DataSet</param>
    /// <param name="strExcelFileName">要导出的文件名</param>
    public void ExportExcel()
    {
    DataTable xslTable=(DataTable)this.dgrd_Show.DataSource;//取得dataGrid绑定的DataSet
    if(xslTable==null) return; string saveFileName="";
    bool fileSaved=false;
    SaveFileDialog saveDialog=new SaveFileDialog();
    saveDialog.DefaultExt ="xls";
    saveDialog.Filter="Excel文件|*.xls";
    saveDialog.FileName ="Sheet1";
    saveDialog.ShowDialog();
    saveFileName=saveDialog.FileName;
    if(saveFileName.IndexOf(":")<0) return; //被点了取消
       
    Excel.Application xlApp=new Excel.Application(); if(xlApp==null)
    {
    MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
    return;
    } Excel.Workbooks workbooks=xlApp.Workbooks;
    Excel.Workbook workbook=workbooks.Add(Excel.XlWBATemplate.xlWBATWorksheet);
    Excel.Worksheet worksheet=(Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
    Excel.Range range; string oldCaption=this.dgrd_Show.CaptionText;
    long totalCount=xslTable.Rows.Count;
    long rowRead=0;
    float percent=0; worksheet.Cells[1,1]=this.dgrd_Show.CaptionText;
    //写入字段
    for(int i=0;i<xslTable.Columns.Count;i++)
    {
    if(i==0)
    worksheet.Cells[2,i+1]="物料类别";//xslTable.Columns[i].ColumnName; 
    if(i>0)
    {
    if(i%3==1)
    worksheet.Cells[2,i+1]="销售额";
    if(i%3==2)
    worksheet.Cells[2,i+1]="成本";
    if(i%3==0)
    worksheet.Cells[2,i+1]="毛利";
    }
    range=(Excel.Range)worksheet.Cells[2,i+1];
    range.Interior.ColorIndex = 15;
    range.Font.Bold = true;
      
    }
    //写入数值
    this.lblpro.Visible=true;
    for(int r=0;r<xslTable.Rows.Count;r++)
    {
    for(int i=0;i<ds.Tables[0].Columns.Count;i++)
    {
    if(i==0)
    worksheet.Cells[r+3,i+1]=xslTable.Rows[r][i];
    else
        worksheet.Cells[r+3,i+1]=Convert.ToDouble(xslTable.Rows[r][i]).ToString("0.00");     
    }
    rowRead++;
    percent=((float)(100*rowRead))/totalCount;    
    this.lblpro.Text= "正在导出数据["+ percent.ToString("0.00")  +"%]...";
    Application.DoEvents();
    }
    this.lblpro.Visible=false;
    // this.dgrd_Show.CaptionText = oldCaption; range=worksheet.get_Range(worksheet.Cells[2,1],worksheet.Cells[ds.Tables[0].Rows.Count+2,ds.Tables[0].Columns.Count]);
    range.BorderAround(Excel.XlLineStyle.xlContinuous,Excel.XlBorderWeight.xlThin,Excel.XlColorIndex.xlColorIndexAutomatic,null);
       
    range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].ColorIndex = Excel.XlColorIndex.xlColorIndexAutomatic;
    range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].LineStyle =Excel.XlLineStyle.xlContinuous;
    range.Borders[Excel.XlBordersIndex.xlInsideHorizontal].Weight =Excel.XlBorderWeight.xlThin; if(xslTable.Columns.Count>1)
    {
    range.Borders[Excel.XlBordersIndex.xlInsideVertical].ColorIndex =Excel.XlColorIndex.xlColorIndexAutomatic;
    range.Borders[Excel.XlBordersIndex.xlInsideVertical].LineStyle = Excel.XlLineStyle.xlContinuous;
    range.Borders[Excel.XlBordersIndex.xlInsideVertical].Weight = Excel.XlBorderWeight.xlThin;
    } if(saveFileName!="")
    {    
    try
    {
    workbook.Saved =true;   
    workbook.SaveCopyAs(saveFileName);
    fileSaved=true;
    }
    catch(Exception ex)
    {
    fileSaved=false;
    MessageBox.Show("导出文件时出错,文件可能正被打开!\n"+ex.Message);
    }
    }
    else
    {
    fileSaved=false;
    }   
    xlApp.Quit();   
    GC.Collect();//强行销毁
    if(fileSaved && File.Exists(saveFileName)) System.Diagnostics.Process.Start(saveFileName); }