2009-01-23 42 views
20

我想打開一個Excel文件並用數據填充它的單元格?到目前爲止,我已經完成了以下編碼。C#:如何訪問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 

回答

20

如果你想爲了使Excel自動化,您可能不應該打開Word文檔並使用Word自動化;)

檢查了一下,它應該是g等你開始,

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); 
+0

對不起錯誤的發佈貼,我已經改正了 – tksy 2009-01-23 14:10:21

3

我認爲,你必須申報相關的表!

嘗試是這樣的

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

你在哪裏找到這個?而兩個點是一個不好的做法.. – nawfal 2012-06-21 23:17:17

1

我是如何工作的自動化辦公/ Excel中:

  1. 錄製宏,這將產生一個VBA模板
  2. 編輯VBA模板,以便它會匹配我的需求
  3. 轉換爲VB.Net(男人的一小步)
  4. 讓它在VB.Net,更多爲方便使用C#
+1

這個問題,他希望它在C#中,但是,對VB.Net來說是沒問題的。 – 2010-01-31 22:37:10

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" 
+0

這是不正確的,OP是問如何打開工作簿,而不是如何創建一個新的...(您正在使用.add方法)。 – 2010-01-31 22:43:06

1

簡單。

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

您先前已經聲明和instanitated xlapp,因爲如此.. Excel.Application xlapp = new Excel.Applicaton();

參數是正確的。

接下來,請確保在使用單元格屬性或範圍對象將值指定給單元格時使用屬性值2。

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

您可以使用下面的代碼工作正常;它對我來說很好:

newWorkbook = appExcel.Workbooks.Add(); 
相關問題