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
385
DataPresenterExcelExporter in Code - getting error in Export method
posted

Hi,

Can anyone enlighten me on how to make this bit of code below work?  There is no .xaml, only code...

DataPresenterExcelExporter exporter = new DataPresenterExcelExporter();

XamDataPresenter dpresenter = new XamDataPresenter();

dpresenter.DataSource = _excelTable.DefaultView;

 

 

string filename = @"c:\testThis.xlsx";

exporter.Export(dpresenter, filename,

WorkbookFormat.Excel2007);

System.Diagnostics.

Process p = new System.Diagnostics.Process();

p.StartInfo.FileName = filename;

p.Start();

I am creating a new exporter and datapresenter in code and when I hit the export method after setting my DataSource for the DataPresenter I get this error: '

The given key was not present in the dictionary.'

_excelTable is a DataTable that contains the results of a query. I can view the records in the datapresenter datasource in the debugger so I'm sure my datasource is valid, but perhaps other parts of the datapresenter are not getting set when I just assign the datasource?  It doesn't seem to be enough b/c the DataPresenter has no understanding of many of the other properties when I look at it in the debbuger. 

Is this something that having it in xaml provides over creating in code?  Does it need to have records selected or something else? The docs didn't have a lot of info regarding how to do this in code or what else was needed in order to use the ExcelExporter for the DataPresenter.

Thanks in advance,

//Amanda

P.S. Some general feedback (and hopefully it's taken as constructive criticism) about the forums, the sizing/font/formatting of posts can make it pretty difficult to read. I would go so far as to say it is a barrier for me to even read through posts b/c of all the different font sizes and spacing inconsistencies...just hard on the eyes when you are already frustrated with some issue, error or question.  I just previewed this post and it looks like a formatting mess. 

  • 1990
    Verified Answer
    posted

    Hi Amanda,

    The actual problem is that the XamDataPresenter is not initialized (its IsInitialized is false) and you try to export the grid is not prepared to perform export.

    You should try calling BeginInit() and EndInit() to make your sample working.

    Try this:

    DataPresenterExcelExporter exporter = new DataPresenterExcelExporter();

     

     

    XamDataPresenter dpresenter = new XamDataPresenter();

    dpresenter.BeginInit();

    dpresenter.DataSource = _excelTable.DefaultView;

    dpresenter.EndInit();

    string filename = @"c:\testThis.xlsx";

    exporter.Export(dpresenter, filename,

    WorkbookFormat.Excel2007);

    System.Diagnostics.

    Process p = new System.Diagnostics.Process();

    p.StartInfo.FileName = filename;

    p.Start();

    Hope that this solves your problem

    Sincerely,

    Horen Kirazyan