Hey everyone,
I have an excel file I'm trying to work with. I can open it, write to it, and save it fine. But now I'm trying to work with adding sheets, and modifying the names of those sheets. For some reason though, my app keeps locking my excel files. I've had to create about 5 test.xlsx files because all of the other test files are permanently read-only (until I log off) for some reason. Here's my code - it looks like I should be closing the files after each run, no matter what, but for whatever reason I'm still getting locked messages.
object oMissing = System.Reflection.Missing.Value;
Excel.Application xl = new Excel.Application();
Excel.Workbook xlBook;
Excel.Worksheet xlSheet;
string laPath = "C:\\Users\\ctote.REG\\Documents\\My Dropbox\\test3.xlsx";
try
{
xlBook = xl.Workbooks.Open(laPath, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing);
xlSheet = xlBook.Worksheets.get_Item(1);
xlSheet.Name = "108";
xl.Worksheets.Add();
xl.Worksheets[0].name = "Testing";
xlBook.Save();
}
catch (Exception)
{
MessageBox.Show("locked");
}
finally
{
xl.Application.Workbooks.Close();
xl.Workbooks.Close();
}
Can someone tell me what I'm doing wrong?