export DataGrid and textbox to Excel

Originally Posted 14 May 2015, 5:55 am EST

  • Originally Posted 14 May 2015, 5:55 am EST

    Hello
    I need your help please i need to export datagid and textbox to the same Excel Sheet so the textBox containt must be before as a title and then the datagid. Any suggestions please . i attached a picture as example
    thank you

  • Marked as Answer

    Reply

    Hello,

    As per my understanding, you are having some TextBoxes and a C1DataGrid in your main window and you want to export all of them in to a Excel Sheet. Let me know you that there is no inbuilt functionality provided by .Net to export all the components of a container control into a Excel sheet.

    As a workaround, you are suggest to use C1XLBook and add all the information into it and then export it into a Excel sheet.
    Here is the suggested code:


    private void btn_Export_Click(object sender, RoutedEventArgs e)
    {
    C1XLBook book = new C1XLBook();
    XLSheet sheet = book.Sheets[0];

    sheet[0,0].Value = txt.Text;

    XLStyle style1 = new XLStyle(book);
    style1.Font = new XLFont("Times New Roman", 18, true, false);
    style1.AlignHorz = XLAlignHorzEnum.Center;
    sheet.Rows[0].Style = style1;

    XLCellRange _ColRange = new XLCellRange(0, 0, 0, 5);
    book.Sheets[0].MergedCells.Add(_ColRange);

    for (int i = 1; i <= c1DataGrid.Rows.Count-1; i++)
    {
    sheet[i, 0].Value = c1DataGrid[i, 0].Value;
    sheet[i, 1].Value = c1DataGrid[i, 1].Value;
    sheet[i, 2].Value = c1DataGrid[i, 2].Value;
    sheet[i, 3].Value = c1DataGrid[i, 3].Value;
    }
    book.Save(@"D:\mybook.xls");
    }


    Please find the attached sample implementing the above and let me know if I missed something.

    Thanks,
    Prashant
    2015/05/Wpf_C1DataGrid_Export.zip
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels