E小cel 异常 “object”未包含“get_Range”的定义 解决方案

来源:互联网 发布:大学生就业情况数据 编辑:程序博客网 时间:2024/04/19 17:55
public void ExportExcel(DataTable table, string Title, string FullPath)
        {
            //文件存在时先删除文件后再进行下一步操作
            System.IO.FileInfo file = new System.IO.FileInfo(FullPath);
            if (file.Exists)
            {
                file.Delete();
            }
            int rowIndex = 1;      //开始写入数据的单元格行
            int colIndex = 0;      //开始写入数据的单元格列
            int DTCols = table.Columns.Count;
            int DTRows = table.Rows.Count;
            System.Reflection.Missing miss = System.Reflection.Missing.Value;
            Excel.Application mExcel = new Excel.Application();
            mExcel.Visible = false;
            Excel.Workbooks mBooks = (Excel.Workbooks)mExcel.Workbooks;
            Excel.Workbook mBook = (Excel.Workbook)(mBooks.Add(miss));
            Excel.Worksheet mSheet = (Excel.Worksheet)mBook.ActiveSheet;

            //创建标题 
            Excel.Range range = mExcel.get_Range(mSheet.Cells[rowIndex, 1], mSheet.Cells[rowIndex, DTCols]);
            range.Merge(false);
            range.RowHeight = 40;
            range.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            range.VerticalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
            range.Value2 = new string[] { Title };
            rowIndex++;
            //设置数据单元格高度
            range = mSheet.get_Range(mSheet.Cells[rowIndex, 1],mSheet.Cells[DTRows + rowIndex, DTCols]);
            range.RowHeight = 25;

            try
            {

                foreach (DataColumn col in table.Columns)    //将所得到的表的列名,赋值给单元格
                {
                    colIndex++;
                    mSheet.Cells[rowIndex, colIndex] = col.ColumnName;
                }

                foreach (DataRow row in table.Rows)    //同样方法处理数据
                {
                    rowIndex++;
                    colIndex = 0;
                    foreach (DataColumn col in table.Columns)
                    {
                        colIndex++;

                        mSheet.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();

                    }

                }
                //保存工作已写入数据的工作表
                mBook.SaveAs(FullPath, miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss);

            }
            catch (Exception ee)
            {
                throw new Exception(ee.Message);
            }
        finally    //finally中的代码主要用来释放内存和中止进程()
            {
                mBook.Close(false, miss, miss);
                mBooks.Close();
                mExcel.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(mSheet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(mBook);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(mBooks);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(mExcel);
                GC.Collect();
            }
        }

导出excel代码,总是提示“object”未包含“get_Range”的定义,查了很多资料,解决方法其实很简单
  Excel.Range range = mExcel.get_Range(mSheet.Cells[rowIndex, 1], mSheet.Cells[rowIndex, DTCols]);
修改为:Excel.Range range = mExcel.get_Range((object)mSheet.Cells[rowIndex, 1], (object)mSheet.Cells[rowIndex, DTCols]); 
就可以了,这是在.net 4下面。
.net2下面这样写:Excel.Range range = mExcel.get_Range(mSheet.Cells[rowIndex, 1], mSheet.Cells[rowIndex, DTCols]); 是没有问题。

转载:http://lanyue52011.blog.163.com/blog/static/357282892012727112044234/
0 0