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
245
exporting to excel in backgroundworker
posted

I am trying to do the export in a background worker.

Does anyone have this working? What are the tricks to get it to work.

I implemented it the simple way with a backgroundworker, DoWoker,,,and I get an exception associated with TSA...

Any clues would be appriciated.

Parents
No Data
Reply
  • 69686
    posted

    Hello,

    This is because the background worker will try to execute this on a background thread and the exporting process will try to access the XamDataGrid, which is created on the UI thread. What you need to do is to use Dispatcher.BeginInvoke() when exporting :

     BackgroundWorker  worker = new  BackgroundWorker ();
             public  MainWindow()
             {
                 InitializeComponent();
                 worker.DoWork += new  DoWorkEventHandler (worker_DoWork);
             }
     
             void  worker_DoWork(object  sender, DoWorkEventArgs  e)
             {
                 DataPresenterExcelExporter  exporter = new  DataPresenterExcelExporter ();
                 Dispatcher.BeginInvoke(
                     new  Action (() =>
                     {
                         exporter.Export(this .xamDataGrid1, "exported.xlsx" , WorkbookFormat .Excel2007);
                     }), null );
             }
     
             private  void  Button_Click(object  sender, RoutedEventArgs  e)
             {
                 worker.RunWorkerAsync();
             }
     

     

Children