0

I have multiple gridviews on one page some with textboxes, others with labels/text. The labels are calculated from other textbox values using javaScript and are placed in the gridview that way. The problem is, the labels are not exporting to excel. I'm wondering if it's because they are created dynamically with javaScript. However, I need these values to export as well. Any help is appreciated!

I've tried data binding again on the export click but no go.

protected void export_Click(object sender, EventArgs e)
    {
        Response.ClearContent();
        Response.AppendHeader("content-disposition", "attachment; filename=AvailableFundsCosts.xls");
        Response.ContentType = "application/excel";

        System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        gridSFS.RenderControl(htw);
        gridTLS.RenderControl(htw);
        gridPCOTotals.RenderControl(htw);
        gridPTA.RenderControl(htw);
        gridPC.RenderControl(htw);
        gridTL.RenderControl(htw);
        gridFinal.RenderControl(htw);


        Response.Write(sw.ToString());
        Response.End();
    }

Here is example of javascript using the grid to place an label value in another part of the gridview:

function calculateGTASemestersOtherActivity()
        {
            var GTASemestersOtherActivity = parseFloat(document.getElementById("cphMain_gridPTA_iTxtAmount_4").value);
            var GTASemesterStipend = parseFloat(document.getElementById("cphMain_gridTLS_iTxtAmount_5").value);

            if(isNaN(GTASemestersOtherActivity))
            {GTASemestersOtherActivity = 0.00;}
            if(isNaN(GTASemesterStipend))
            {GTASemesterStipend = 0.00;}

            var OtherActivityCost = parseFloat(GTASemestersOtherActivity * GTASemesterStipend);

            var cell = document.getElementById("cphMain_gridPC").rows[8].cells;

            cell[1].innerHTML = OtherActivityCost.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
        }

I need the label values to export excel as well as the textboxes.

Juan
  • 75
  • 9
  • 1
    Start using a specialized library for creating Excel files, like [EPPlus](https://github.com/JanKallman/EPPlus). [Example here](https://stackoverflow.com/a/47293207/5836671) and [here](https://stackoverflow.com/a/39513057/5836671). All you are doing now is creating a HTML page with an .xls extension. And javascript data is not available in code behind so you cannot export it. Better get the data in code behind also and bind it to the GridView, then you can also export it. – VDWWD Oct 30 '19 at 18:57
  • Turns out I had these values created in the code behind as well, my solution was reload the values to the gridview. – Juan Oct 30 '19 at 19:52

1 Answers1

0

Need to reload the values to the GridView in the code behind. JavaScript does not store the values to the grid directly.

Juan
  • 75
  • 9