We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

How to Export a Gridview with ItemTemplate to Excel

How Can I export this gridView , to an Excel File. I would Like to do be able to do this when a user clicks a button… The gridview is populated with:

CS:
DataSet ds = new DataSet();
ds = Funcs.ds(sql);
well_updates.DataSource = ds;
well_updates.DataBind();

ASPX:

<asp:GridView ID=“well_updates” OnSorting=“well_updates_Sorting” AllowSorting=“true” AutoGenerateColumns=“false” runat=“server” OnRowDataBound=“Well_updates_OnRowDataBound” AllowPaging=“True” OnPageIndexChanging=“well_updates_OnPageIndexChanging”>

<asp:TemplateField HeaderText=“Provider Name” SortExpression=“ProviderName” HeaderStyle-HorizontalAlign=“center” ItemStyle-HorizontalAlign=“left” >

<asp:Label ID=“provider” runat=“server” />

</asp:TemplateField>
<asp:TemplateField HeaderText=“Patient Name” SortExpression=“PatientName” HeaderStyle-HorizontalAlign=“center” ItemStyle-HorizontalAlign=“left” >

<asp:Label ID=“pat_name” runat=“server” />

</asp:TemplateField>
<asp:TemplateField HeaderText=“Visit From/Admit” SortExpression=“Visit_From” HeaderStyle-HorizontalAlign=“center” ItemStyle-HorizontalAlign=“left” >

<asp:Label ID=“from” runat=“server” />

</asp:TemplateField>
<asp:TemplateField HeaderText=“Discharge” SortExpression=“Visit_Thru” HeaderStyle-HorizontalAlign=“center” ItemStyle-HorizontalAlign=“left” >

<asp:Label ID=“thru” runat=“server” />

</asp:TemplateField>
<asp:TemplateField HeaderText=“PCN” SortExpression=“PCN” HeaderStyle-HorizontalAlign=“center” ItemStyle-HorizontalAlign=“left” >

<asp:Label ID=“PCN” runat=“server” />

</asp:TemplateField>
<asp:TemplateField HeaderText=“Claim Reference Number” SortExpression=“ClaimReferenceNbr” HeaderStyle-HorizontalAlign=“center” ItemStyle-HorizontalAlign=“left” ItemStyle-Wrap=“false” >

<asp:Label ID=“claimref” runat=“server” />

</asp:TemplateField>
<asp:TemplateField HeaderText=“Update” SortExpression=“message” HeaderStyle-HorizontalAlign=“center” ItemStyle-HorizontalAlign=“left” >

<asp:Label ID=“msg” runat=“server” />

</asp:TemplateField>
<asp:TemplateField HeaderText=“Date Logged” SortExpression=“DateLogged” HeaderStyle-HorizontalAlign=“center” ItemStyle-HorizontalAlign=“left” >

<asp:Label ID=“date” runat=“server” />

</asp:TemplateField>

</asp:GridView>


Hi Ray,


Thank you for contacting Aspose support.

Please note, Aspose.Cells for .NET APIs provide a number of ways to import data onto the worksheet cells, where the final result can be stored in any of the supported spreadsheet formats. Please review the detailed article on Importing Data to Worksheet. Also note that Cells class has a specialized method to import data from a GridView, however, you may also try to use alternative approaches such as converting the GridView to DataTable and importing it as demonstrated below.

C#

Aspose.Cells.Workbook book = new Aspose.Cells.Workbook();
DataTable dt = new DataTable();
for (int i = 0; i < well_updates.Columns.Count; i++)
{
dt.Columns.Add(“column” + i.ToString());
}
foreach (GridViewRow row in well_updates.Rows)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < well_updates.Columns.Count; j++)
{
dr[“column” + j.ToString()] = row.Cells[j].Text;
}

dt.Rows.Add(dr);
}
book.Worksheets[0].Cells.ImportDataTable(dt, true, “A1”);
book.Save(“D:/output.xlsx”);