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
如果您尝试使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 );
我想,你必须申报关联表!
尝试这样的事情
objsheet(1).Cells[i,j].Value;
我如何使Office / Excel自动化:
- logging一个macros,这将生成一个VBA模板
- 编辑VBA模板,使其符合我的需求
- 转换为VB.Net(男人的一小步)
- 留在VB.Net,使用C#
尝试:
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"
简单。
打开工作簿。 使用xlapp.workbooks.Open()
在那里你以前已经宣布和instanitated xlapp这样.. Excel.Application xlapp = new Excel.Applicaton();
参数是正确的。
接下来,确保在使用单元格属性或范围对象将值赋给单元格时使用属性Value2。
这对我来说很好
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";
你可以使用下面的代码; 它对我来说工作的很好:
newWorkbook = appExcel.Workbooks.Add();