C#:如何访问Excel单元格?

时间:2009-01-23 13:56:25

标签: c# excel

我正在尝试打开Excel文件并使用数据填充其单元格?到目前为止,我已完成以下编码。

目前我处于以下代码的现阶段,但我仍然遇到错误:

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";

这是我得到的前2个错误:

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

7 个答案:

答案 0 :(得分:20)

如果您尝试自动化Excel,则可能不应该打开Word文档并使用Word自动化;)

检查一下,它应该让你开始,

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

这是一些代码。它来自我的一些代码并删除了很多东西,所以它没有做任何事情,可能无法编译或工作,但它应该让你去。它面向阅读,但应指向正确的方向。

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;
        }
    }
 }

您也可以

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

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

如果你需要插入行/列

// 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 );

答案 1 :(得分:3)

我认为,你必须声明相关的表格!

尝试这样的事情

objsheet(1).Cells[i,j].Value;

答案 2 :(得分:1)

我如何自动化Office / Excel:

  1. 录制宏,这将生成VBA模板
  2. 编辑VBA模板,使其符合我的需求
  3. 转换为VB.Net(男人的一小步)
  4. 把它留在VB.Net中,比使用C#
  5. 更容易

答案 3 :(得分:1)

尝试:

Excel.Application oXL;
Excel._Workbook oWB;
Excel._Worksheet oSheet;
Excel.Range oRng;

oXL = new Excel.Application();
oXL.Visible = true;
oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));

oSheet = (Excel._Worksheet)oWB.Worksheets;
oSheet.Activate();

oSheet.Cells[3, 9] = "Some Text"

答案 4 :(得分:1)

简单。

打开工作簿。 使用xlapp.workbooks.Open()

您之前已声明并实例化xlapp,如此... Excel.Application xlapp = new Excel.Applicaton();

参数正确。

接下来,确保在使用cells属性或范围对象为单元格赋值时使用属性Value2。

答案 5 :(得分:1)

这对我来说很好用

       Excel.Application oXL = null;
        Excel._Workbook oWB = null;
        Excel._Worksheet oSheet = null;

        try
        {
            oXL = new Excel.Application();
            string path = @"C:\Templates\NCRepTemplate.xlt";
            oWB = oXL.Workbooks.Open(path, 0, false, 5, "", "",
                false, Excel.XlPlatform.xlWindows, "", true, false,
                0, true, false, false);

            oSheet = (Excel._Worksheet)oWB.ActiveSheet;
            oSheet.Cells[2, 2] = "Text";

答案 6 :(得分:1)

您可以使用以下代码;它对我来说很好:

newWorkbook = appExcel.Workbooks.Add();