在网上看了一些例子,基本都是用类似下面的语句开始的。
Excel.Application excelApp= new Excel.ApplicationClass();请问需要引入什么程序集和命名空间呢。(using ......)我用的是VS2005。希望大家给我个完整的例子最好啊。谢谢大家!

解决方案 »

  1.   

    请问需要引入什么程序集和命名空间呢。(using ......) 
    引入Excel11.0
    引用---添加引用----COM-----Microsoft Excel 11.0 Object Library
      

  2.   


     public static void ExportToExcel(string[,] exportData, string exportFile, string templateFile, string templetFilePath, string downtemplatePath)
            {
                //解决excel bug
                System.Globalization.CultureInfo CurrentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
                ////////////////////////////////////////////////////////////////////////////////////////////////////////
                Excel._Application app;
                Excel.Workbook workBook;
                Excel.Worksheet workSheet;
                Excel.Range range;            app = new Excel.ApplicationClass();
                app.Visible = false;
                workBook = app.Workbooks.Open(templetFilePath + templateFile, Missing.Value, Missing.Value, Missing.Value,
                            Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                            Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(2); // Read parameters from the second worksheet of xls.            int datasheet = Convert.ToInt32((workSheet.get_Range("B1", Missing.Value)).Value2);
                int startRow = Convert.ToInt32((workSheet.get_Range("B2", Missing.Value)).Value2);
                int startCol = Convert.ToInt32((workSheet.get_Range("B3", Missing.Value)).Value2);            workSheet = (Excel.Worksheet)workBook.Sheets.get_Item(datasheet);
                int rowCount = exportData.GetLength(0);
                int colCount = exportData.GetLength(1);
                range = (Excel.Range)workSheet.Cells[startRow, startCol]; //写入Excel的坐标
                range = range.get_Resize(rowCount, colCount);
                range.Value2 = exportData;            workBook.SaveAs(downtemplatePath + exportFile, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                                Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value,
                                Missing.Value, Missing.Value, Missing.Value, Missing.Value);            workBook.Close(null, null, null);
                app.Workbooks.Close();
                app.Quit();            if (range != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
                    range = null;
                }            if (workSheet != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
                    workSheet = null;
                }
                if (workBook != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
                    workBook = null;
                }
                if (app != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
                    app = null;
                }            GC.Collect();
                ////////////////////////////////////////////////////////////////////////////////////////////////////////
                //恢复语言环境
                //System.Runtime.InteropServices.Marshal.ReleaseComObject(xlRang);
                System.Threading.Thread.CurrentThread.CurrentCulture = CurrentCI;
                ////////////////////////////////////////////////////////////////////////////////////////////////////////
                DownLoadFile(downtemplatePath, exportFile);
                DeleteFile(downtemplatePath + exportFile);        }Interop.Excel.dll
      

  3.   

    http://blog.csdn.net/cpp2017/archive/2008/04/02/2244368.aspx
    http://blog.csdn.net/cpp2017/archive/2008/04/02/2245276.aspx
    http://blog.csdn.net/cpp2017/archive/2008/04/02/2245396.aspxhttp://www.icgbbs.com
      

  4.   

    /////参数列表:DataGridView数据窗口、文件名、是否打开Excel文件
    public static bool ExportForDataGridview(DataGridView gridView, string fileName, bool isShowExcle)
            {            //建立Excel对象            Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                try
                {
                    if (app == null)
                    {
                        return false;
                    }
                    
                    app.Visible = isShowExcle;
                    Workbooks workbooks = app.Workbooks;
                    _Workbook workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);
                    Sheets sheets = workbook.Worksheets;
                    _Worksheet worksheet = (_Worksheet)sheets.get_Item(1);
                    if (worksheet == null)
                    {
                        return false;
                    }
                    string sLen = "";
                    //取得最后一列列名
                    char H = (char)(64 + gridView.ColumnCount / 26);
                    char L = (char)(64 + gridView.ColumnCount % 26);
                    if (gridView.ColumnCount < 26)
                    {
                        sLen = L.ToString();
                    }
                    else
                    {
                        sLen = H.ToString() + L.ToString();
                    }
                    //标题
                    string sTmp = sLen + "1";
                    Range ranCaption = worksheet.get_Range(sTmp, "A1");
                    string[] asCaption = new string[gridView.ColumnCount];
                    for (int i = 0; i < gridView.ColumnCount; i++)
                    {
                        asCaption[i] = gridView.Columns[i].HeaderText;
                    }
                    ranCaption.Value2 = asCaption;                //数据
                    object[] obj = new object[gridView.Columns.Count];
                    for (int r = 0; r < gridView.RowCount - 1; r++)
                    {
                        for (int l = 0; l < gridView.Columns.Count; l++)
                        {
                            if (gridView[l, r].ValueType == typeof(DateTime))
                            {
                                obj[l] = gridView[l, r].Value.ToString();
                            }
                            else
                            {
                                obj[l] = gridView[l, r].Value;
                            }
                        }
                        string cell1 = sLen + ((int)(r + 2)).ToString();
                        string cell2 = "A" + ((int)(r + 2)).ToString();
                        Range ran = worksheet.get_Range(cell1, cell2);
                        ran.Value2 = obj;
                    }
                    //保存
                    workbook.SaveCopyAs(fileName);
                    workbook.Saved = true;
                }
                finally
                {
                    //关闭
                    app.UserControl = false;
                    app.Quit();
                }
                return true;        }
      

  5.   

    也可以用OleDb对象把Excel当数据库表的方式写入。