Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
590
DataPresenterExcelExporter opening file causes read-only error in Excel
posted

When exporting data from a XamDataGrid I get a error when opening the file in Excel that says the file is locked by another application. The filename is new each time I export and is based on the current time as shown below.

string filename = "MyGrid" + DateTime.Now.ToString("MMM-dd-yyyy hh-mm-ss") + ".xlsx";
 
DataPresenterExcelExporter exporter = new DataPresenterExcelExporter();
exporter.Export(grid, filename, WorkbookFormat.Excel2007);
 
Process p = new Process();
p.StartInfo.FileName = filename;
p.Start();

If I call Thread.Sleep(1000) right after the .Export call, everything works fine but I'd like to find a better solution than that. And just to be clear, I'm very confident there are no other threads or applications that are locking the file, I believe it's the DataPresentExcelExporter that hasn't let go of the file yet.

There's some more odd behavior...

- This only happens the FIRST time I export from a grid. Every time after that for the same grid works. However, if I export data from a different grid, I get the read-only thing the first time.

- This read-only issue only happens if I already have Excel running before I export.

Is there are better way to implement the opening of the file or any ideas on how I could prevent the read-only lock?

Parents Reply Children
No Data