【问题标题】:C#: How to access an Excel cell?C#:如何访问 Excel 单元格?
【发布时间】:2009-01-23 13:56:24
【问题描述】:

我正在尝试打开一个 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

【问题讨论】:

    标签: c# excel


    【解决方案1】:

    如果您尝试自动化 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 );
    

    【讨论】:

      【解决方案2】:

      我认为,您必须声明相关的工作表!

      试试这样的

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

      【讨论】:

        【解决方案3】:

        我如何实现 Office / Excel 自动化:

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

        【讨论】:

        • 从问题中,他想要在 C# 中使用它,但是是的,这对 VB.Net 来说很好。
        【解决方案4】:

        试试:

        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"
        

        【讨论】:

        • 这不正确,OP 是在询问如何打开工作簿,而不是如何创建新工作簿...(您正在使用 .add 方法)。
        【解决方案5】:

        简单。

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

        您之前已声明并实例化 xlapp 的地方...... Excel.Application xlapp = new Excel.Applicaton();

        参数正确。

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

        【讨论】:

          【解决方案6】:

          这对我来说很好用

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

          【讨论】:

            【解决方案7】:

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

            newWorkbook = appExcel.Workbooks.Add();
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2011-12-05
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-09-01
              • 1970-01-01
              • 1970-01-01
              • 2015-10-22
              相关资源
              最近更新 更多