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
No Data
Reply
  • 34690
    Offline posted

    Hello WpfDeveloper,

    I followed the steps you suggested and was unable to reproduce the behavior you're describing. Using a XamDataGrid with 4 columns and 2500 records, I exported to the grid using the code you provided with and without Excel open. I did not see this read-only behavior you are describing.

    I have attached the sample project I used to test this.  Please test this project on your PC; whether or not it works correctly may help indicate the nature of this problem.
    If the project does not work correctly, this indicates either a problem possibly specific to your environment, or a difference in the DLL versions we are using.  My test was performed using version 14.2.20142.2081 in Infragistics for WPF 2014 Volume 2. Is this the version you are using as well?

    If the project does show the product feature working correctly, this indicates a possible problem in the code of your application.  It will help if you can provide a small, isolated sample application that demonstrates the behavior you are seeing.

    Or, if this sample project is not an accurate demonstration of what you're trying to do, please feel free to modify it and send it back, or send a small sample project of your own if you have one.

    Please let me know if I can provide any further assistance.

    Sincerely,
    Andrew
    Associate Developer
    Infragistics Inc.
    www.infragistics.com/support

    XDGExportToExcelCase.zip
Children