Hi all,
Want to write some data from C# to Excel.
Code enough I thought, here at DANI's and on the web.
So I managed to come up with this :
private void DoExcel(string Fname)
{
Microsoft.Office.Interop.Excel.ApplicationClass excel = null;
Microsoft.Office.Interop.Excel.Workbook wb = null;
Microsoft.Office.Interop.Excel.Worksheet ws = null;
Microsoft.Office.Interop.Excel.Range rng = null;
object missing = Type.Missing;
bool ReadOnly = false; //true or missing gives the same
try
{
//Excel.Application is abstract class so I use this
excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
//If I use Open or _Open it gives the same
wb = excel.Workbooks.Open(Fname, missing, ReadOnly, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing);
ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.ActiveSheet;
rng = ws.get_Range("A1", missing);
rng.Value2 = "Some string";
//All is well until here, Save thinks the excelfile is readonly
excel.Save(Fname);
excel.Quit();
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.ToString());
}
}
Whatever I do with ReadOnly , Save always thinks the excel file is read only. I even used Quit but then I get a dialog to save a copy.
What is wrong here and is there a solution?
Using Excel 2002 and Object Library 10.0.
Any suggestiions are welcome.