0
我想在C#中創建一個excel工具。該工具必須打開文件夾中的每個Excel文檔,並在單元格E1中查找值。如果單元格保存我搜索的值,它將被刪除,文檔將保存。然後應用程序將轉到下一個excel文件。如何在C#中讀取單元格?
我可以打開文檔,但無法查看單元格中的值。
這裏我的代碼:
using Microsoft.Office.Interop.Excel;
//Preparing the required items
Microsoft.Office.Interop.Excel.Application excel = null;
Workbook wb = null;
//Start Excel
excel = new Microsoft.Office.Interop.Excel.Application();
excel.Visible = false;
try
{
//Open file
wb = excel.Workbooks.Open(
@"C:\Users\....",
ExcelKonstanten.UpdateLinks.DontUpdate,
ExcelKonstanten.ReadOnly,
ExcelKonstanten.Format.Nothing,
"", //Password
"", //WriteResPasswort
ExcelKonstanten.IgnoreReadOnlyRecommended,
XlPlatform.xlWindows,
"", //Separator
ExcelKonstanten.Editable,
ExcelKonstanten.DontNotifiy,
ExcelKonstanten.Converter.Default,
ExcelKonstanten.DontAddToMru,
ExcelKonstanten.Local,
ExcelKonstanten.CorruptLoad.NormalLoad);
//Read sheets
Sheets sheets = wb.Worksheets;
//Select a sheet…
Worksheet ws = (Worksheet)sheets.get_Item("Tabelle1");
//…or a cell
Range range = (Range)ws.get_Range("E", "1");
//Read out the value
string zellwert = range.Value2.ToString(); // <--- This is where I get the error!
string zellwert = range.Value2;
Console.WriteLine(zellwert);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
wb.Close(false, null, null);
excel.Quit();
}
Console.WriteLine("Prüfung abgeschlossen...");
我嘗試在此頁面同樣的事情:
http://blog.stefan-macke.com/2006/06/28/c-projekt-zugriff-auf-excel-dateien/
看到這個http://stackoverflow.com/questions/910400/reading-from-excel-range-into-multidimensional-array-c-sharp - 你可以閱讀範圍到一個數組對象中,然後獲取數組中的第一項。 – 2013-04-22 08:02:52
你有什麼錯誤? – MoonKnight 2013-04-22 08:33:05