獲取簡單的readxml方法需要幫助。 這裏是我所採取的步驟:獲取簡單的readxml方法需要幫助
- 添加引用從Microsoft Excel 12.0對象庫COM。
- 複製代碼完全從另一源 http://csharp.net-informations.com/excel/csharp-read-excel.htm
- 我會要求這是一個方法的唯一區別和Not onClick事件。
- 我得到的錯誤是錯誤10引用需要非靜態字段,方法或屬性的代碼
releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp);
有什麼需要的步驟的對象?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace projectName
{
class frmReadXml
{
public static void executeRead()
{
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet = new Excel.Worksheet();
Excel.Range range;
string str;
int rCnt = 0;
int cCnt = 0;
xlWorkBook = xlApp.Workbooks.Open("csharp.net-informations.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
range = xlWorkSheet.UsedRange;
for (rCnt = 1; rCnt <= range.Rows.Count; rCnt++)
{
for (cCnt = 1; cCnt <= range.Columns.Count; cCnt++)
{
str = (string)(range.Cells[rCnt, cCnt] as Excel.Range).Value2;
MessageBox.Show(str);
}
}
xlWorkBook.Close(true, null, null);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
MessageBox.Show("Unable to release the Object " + ex.ToString());
}
finally
{
GC.Collect();
}
}
}
}
感謝您的幫助。非常感激 – user2552331