2009-01-23 11 views
20

Tôi đang cố mở một tệp Excel và điền các ô của nó bằng dữ liệu? Tôi đã thực hiện các mã sau đây cho đến nay.C#: Cách truy cập một ô Excel?

Hiện nay tôi đang ở giai đoạn này với đoạn mã sau nhưng vẫn Tôi nhận được lỗi:

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

Đây là 2 lỗi đầu Tôi nhận:

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 

Trả lời

20

Nếu bạn đang cố gắng để tự động hoá Excel, có thể bạn không nên mở tài liệu Word và sử dụng Tự động hóa từ;)

Kiểm tra điều này, nó phải là g et bạn bắt đầu,

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

Và đây là một số mã. Nó được lấy từ một số mã của tôi và có rất nhiều thứ bị xóa, do đó, nó không làm bất cứ điều gì và có thể không biên dịch hoặc làm việc chính xác, nhưng nó sẽ giúp bạn đi. Nó được định hướng để đọc, nhưng nên chỉ cho bạn đi đúng hướng.

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

Bạn cũng có thể làm

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

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

Và nếu bạn cần phải chèn dòng/cột

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

xin lỗi mã sai đã được đăng, tôi đã sửa nó – tksy

3

Tôi nghĩ rằng, bạn phải khai báo tờ liên quan!

Hãy thử một cái gì đó như thế này

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

bạn đã tìm thấy ở đâu? Và hai dấu chấm là một thực tế xấu .. – nawfal

1

Làm thế nào tôi làm việc để tự động hóa văn phòng/Excel:

  1. Ghi macro, điều này sẽ tạo ra một mẫu VBA
  2. Sửa mẫu VBA nên nó sẽ phù hợp với nhu cầu của tôi
  3. Chuyển đổi sang VB.Net (Một bước nhỏ cho nam giới)
  4. Hãy để nó trong VB.Net, Hơn thế nữa dễ dàng như làm việc đó bằng C#
+1

từ câu hỏi, anh ta muốn nó trong C#, nhưng có đó sẽ là tốt cho VB.Net. –

1

Hãy thử:

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

điều này không chính xác, OP đang yêu cầu cách mở sổ làm việc, không phải cách tạo sổ làm việc mới ... (bạn đang sử dụng phương thức .add). –

1

đơn giản.

Để mở sổ làm việc. Sử dụng xlapp.workbooks.Open()

nơi bạn đã khai báo trước và instanitated xlapp như vậy .. Excel.Application xlapp = new Excel.Applicaton(); Các thông số

là chính xác.

Tiếp theo hãy đảm bảo bạn sử dụng thuộc tính Value2 khi gán giá trị cho ô bằng cách sử dụng thuộc tính ô hoặc đối tượng phạm vi.

1

này hoạt động tốt đối với tôi

 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

Bạn có thể sử dụng mã dưới đây; nó hoạt động tốt cho tôi:

newWorkbook = appExcel.Workbooks.Add();