private void button1_Click(object sender, EventArgs e)
{
try
{
string path;
path = @"d:\succeccful.xls";
Excel.Application xlapp;
Excel.Workbook xlbook;
Excel.Worksheet xlsheet;
xlapp = new Excel.ApplicationClass();
xlbook = xlapp.Workbooks._Open(path,Missing.Value,Missing.Value,Missing.Value,Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
xlapp.DisplayAlerts = false;
xlapp.Cells[2, 2] = "123";
MessageBox.Show((xlapp.Cells[2, 2]).ToString ());
//上面這句話的結果顯示system._comobject,這個需要進行強制轉換一下,比如下面這句話
MessageBox.Show(((Excel.Range)xlapp.Cells[2, 2]).Value2.ToString());
xlbook.SaveAs(path, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
xlapp.Quit();
xlapp = null;
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
************************************************************
這些是從網上找到的http://dotnet.wgets.com/thread/400640
需要強制轉換一下
((Excel.Range)xlapp.Cells[2,2]).Text//(唯讀)
((Excel.Range)xlapp.Cells[2,2]).Value//(讀寫)
((Excel.Range)xlapp.Cells[2,2]).Value2//(讀寫)
************************************************************
http://dotnet.wgets.com/thread/400640