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

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

问题描述

我试图打开一个Excel文件并用数据填充它的单元格?目前为止,我已经完成了以下编码。



目前,我正在使用以下代码进行此阶段,但仍然出现错误:

  Microsoft.Office.Interop.Excel.ApplicationClass appExcel = 
new Microsoft.Office.Interop.Excel.ApplicationClass();
尝试
{
//是否已经有这样的文件?
if(System.IO.File.Exists(C:\\\csharp\\\errorreport1.xls))
{
//然后去加载到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
{
//如果没有去创建一个工作簿:
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 =更新为888;

这些是我获得的前2个错误:

 错误1非静态字段,方法或
属性Microsoft.Office.Interop.Excel.Workbooks.Open(string,object,
对象,对象,对象,对象,对象,对象,对象,对象,
对象,对象,对象,对象)'

错误2名称'newWorkbook'在当前上下文中不存在


解决方案

如果您正在尝试自动化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(usedRange.Rows中的Microsoft.Office.Interop.Excel.Range行)
{
string value = row.Cells [0] .FormattedValue作为字符串;
}
}
}

你也可以做



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

if(sheets!= null)
{
foreach(Microsoft.Office.Interop.Excel.Worksheet sheet in sheet)
{
//做东西
}
}

如果需要插入行/列

  //在工作表的开头插入一个新行
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天全站免登陆