C#中:如何访问Excel单元格? [英] C#: How to access an Excel cell?

查看:126
本文介绍了C#中:如何访问Excel单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图打开一个Excel文件,填充它的细胞的数据?到目前为止,我已经做了以下的编码。

目前我在这个阶段有以下code,但仍然我得到错误:

  Microsoft.Office.Interop.Excel.ApplicationClass appExcel =
                新Microsoft.Office.Interop.Excel.ApplicationClass();
尝试
{
    //是已经有这样的文件吗?
    如果(System.IO.File.Exists(C:\\\\ \\\\的csharp errorreport1.xls))
    {
        //然后去和这个加载到Excel
        Microsoft.Office.Interop.Excel.Workbooks.Open(
            C:\\\\ \\\\的csharp errorreport1.xls,真的,假的,
            Missing.Value,Missing.Value,Missing.Value,Missing.Value,
            Missing.Value,Missing.Value,Missing.Value,Missing.Value,
            Missing.Value,Missing.Value,Missing.Value,Missing.Value);
    }
    其他
    {
        //如果不是去创建一个工作簿:
        newWorkbook = appExcel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
        Microsoft.Office.Interop.Excel._Worksheet excelWorksheet =
            (Microsoft.Office.Interop.Excel._Worksheet)
                newWorkBook.Worksheets.get_Item(1);
    }
我++;
J = 1;J ++;
objsheet.Cells(I,J)。价值=Tabelle:+ rs.Fields [表格名];
J ++;
objsheet.Cells(I,J).value的=kombinationsschluessel:FALL
                                + rs3.Fields [1]。价值;
J ++;
objsheet.Cells(I,J).value的=NULL值;
J ++;
objsheet.Cells(I,J).value的=更新与888;

这是前2个错误,我越来越:

 错误1是必需的非静态字段的对象引用,方法或
        财产Microsoft.Office.Interop.Excel.Workbooks.Open(字符串,对象,
        对象,对象,对象,对象,对象,对象,对象,对象,对象,
        对象,对象,目标,对象)错误2'newWorkbook'这个名字并不在目前的情况下存在


解决方案

<击>如果你想自动化Excel,你可能不应该打开一个Word文档,并使用Word自动化;)

检查了这一点,它应该让你开始,

<一个href=\"http://www.$c$cproject.com/KB/office/package.aspx\">http://www.$c$cproject.com/KB/office/package.aspx

和这里是一些code。它是从我的一些code的拍摄,有很多东西删除,所以它不会做任何事情,不得编译或正好工作,但它应该让你去。它是面向对阅读,而是应该指向你在正确的方向。

  Microsoft.Office.Interop.Excel.Worksheet表= newWorkbook.ActiveSheet;如果(表!= NULL)
{
    Microsoft.Office.Interop.Excel.Range范围= sheet.UsedRange;
    如果(范围!= NULL)
    {
        INT NROWS = usedRange.Rows.Count;
        INT NCOLS = usedRange.Columns.Count;
        的foreach(在usedRange.Rows Microsoft.Office.Interop.Excel.Range行)
        {
            字符串值= row.Cells [0] .FormattedValue为字符串;
        }
    }
 }

您也可以做

  Microsoft.Office.Interop.Excel.Sheets表= newWorkbook.ExcelSheets;如果(张!= NULL)
{
     的foreach(Microsoft.Office.Interop.Excel.Worksheet片表)
     {
          // 做东西
     }
}

如果你需要插入的行/列

  //在片的开头插入一个新行
Microsoft.Office.Interop.Excel.Range A1 = sheet.get_Range(A1,Type.Missing);
a1.EntireRow.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftDown,Type.Missing);

I am trying to open an Excel file and populate its cells with data? I have done the following coding so far.

Currently I am at this stage with the following code but still I am getting errors:

Microsoft.Office.Interop.Excel.ApplicationClass appExcel =
                new Microsoft.Office.Interop.Excel.ApplicationClass();
try
{
    // is there already such a file ?
    if (System.IO.File.Exists("C:\\csharp\\errorreport1.xls"))
    {
        // then go and load this into excel
        Microsoft.Office.Interop.Excel.Workbooks.Open(
            "C:\\csharp\\errorreport1.xls", true, false, 
            Missing.Value, Missing.Value, Missing.Value, Missing.Value,
            Missing.Value, Missing.Value, Missing.Value, Missing.Value, 
            Missing.Value, Missing.Value, Missing.Value, Missing.Value);
    }
    else
    {
        // if not go and create a workbook:
        newWorkbook = appExcel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
        Microsoft.Office.Interop.Excel._Worksheet excelWorksheet = 
            (Microsoft.Office.Interop.Excel._Worksheet)
                newWorkBook.Worksheets.get_Item(1);
    } 
i++;
j = 1;

j++;
objsheet.Cells(i, j).Value = "Tabelle: " + rs.Fields["Table_Name"];
j++;
objsheet.Cells(i, j).Value = "kombinationsschluessel:FALL " 
                                + rs3.Fields[1].Value;
j++;
objsheet.Cells(i, j).Value = "Null Value: ";
j++;
objsheet.Cells(i, j).Value = "Updated with 888";

These are the top 2 errors I am getting:

Error 1 An object reference is required for the nonstatic field, method, or
        property 'Microsoft.Office.Interop.Excel.Workbooks.Open(string, object,
        object, object, object, object, object, object, object, object, object,
        object, object, object, object)'

Error 2 The name 'newWorkbook' does not exist in the current context

解决方案

If you are trying to automate Excel, you probably shouldn't be opening a Word document and using the Word automation ;)

Check this out, it should get you started,

http://www.codeproject.com/KB/office/package.aspx

And here is some code. It is taken from some of my code and has a lot of stuff deleted, so it doesn't do anything and may not compile or work exactly, but it should get you going. It is oriented toward reading, but should point you in the right direction.

Microsoft.Office.Interop.Excel.Worksheet sheet = newWorkbook.ActiveSheet;

if ( sheet != null )
{
    Microsoft.Office.Interop.Excel.Range range = sheet.UsedRange;
    if ( range != null )
    {
        int nRows = usedRange.Rows.Count;
        int nCols = usedRange.Columns.Count;
        foreach ( Microsoft.Office.Interop.Excel.Range row in usedRange.Rows )
        {
            string value = row.Cells[0].FormattedValue as string;
        }
    }
 }

You can also do

Microsoft.Office.Interop.Excel.Sheets sheets = newWorkbook.ExcelSheets;

if ( sheets != null )
{
     foreach ( Microsoft.Office.Interop.Excel.Worksheet sheet in sheets )
     {
          // Do Stuff
     }
}

And if you need to insert rows/columns

// Inserts a new row at the beginning of the sheet
Microsoft.Office.Interop.Excel.Range a1 = sheet.get_Range( "A1", Type.Missing );
a1.EntireRow.Insert( Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftDown, Type.Missing );

这篇关于C#中:如何访问Excel单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆