I'm trying to export a GridView
into a xls file, I insert the GridView
into a Modal
and I'm supposing that because of that it can't be exported.
This is my BackEnd
of the action from the Button
:
protected void Btn_download_Click(object sender, EventArgs e)
{
Response.ClearContent();
Response.AppendHeader("content-disposition", "attachment; flename=MainReport.xls");
Response.ContentType = "application/excel";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
Grv_main.RenderControl(htmlTextWriter);
Response.Write(stringWriter.ToString());
Response.End();
}
and this is the HTML
:
<form id="form1" runat="server">
<div class="container">
<div class="modal fade" id="History-Maint" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header" style="padding: 35px 50px;">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3><span class="glyphicon glyphicon-list-alt"></span>  Maintenance Report</h3>
</div>
<div class="modal-body">
<asp:GridView ID="Grv_main"
CssClass="table table-striped table-bordered table-hover"
runat="server"
AutoGenerateColumns="False"
Width="940px"
HorizontalAlign="Center"
DataKeyNames="id"
AllowPaging="True" OnRowEditing="GridView1_RowEditing">
<Columns>
<asp:TemplateField HeaderText="id" Visible="false">
<ItemTemplate>
<asp:Label ID="Lbl_id" runat="server" Text='<%# Bind("id") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="status">
<ItemTemplate>
<asp:Label ID="Lbl_stats" runat="server" Text='<%# Bind("STATUS") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="tool">
<ItemTemplate>
<asp:Label ID="Lbl_tool" runat="server" Text='<%# Bind("TOOL") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Area Change">
<ItemTemplate>
<asp:Label ID="Lbl_areach" runat="server" Text='<%# Bind("AREA") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="responsable">
<ItemTemplate>
<asp:Label ID="Lbl_resp" runat="server" Text='<%# Bind("RESPONSABLE") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="nota">
<ItemTemplate>
<asp:Label ID="Lbl_note" runat="server" Text='<%# Bind("NOTA") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="fecha de modificacion">
<ItemTemplate>
<asp:Label ID="Lbl_mod" runat="server" Text='<%# Bind("MODIFY") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div class="modal-footer">
<asp:Button ID="Btn_download" runat="server" CssClass="btn btn-success" Text='Guardar' OnClick="Btn_download_Click" />
</div>
</div>
</div>
</div>
</div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server">
</asp:ContentPlaceHolder>
</form>
all of this, is on a MasterPage
and into a form
with the runat="server"
The best for me has been to use ClosedXML
https://github.com/closedxml/closedxml
It's very easy and you can add sorting, and filtering built in.
Here's an example from the documentation:
var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add("Sample Sheet");
worksheet.Cell("A1").Value = "Hello World!";
workbook.SaveAs("HelloWorld.xlsx");
private void ExportGridToExcel() {
Response.Clear();
//You had a typo on the line below in your code for (filename)
Response.AddHeader("content-disposition", "attachment;filename=MainReport.xls");
Response.ContentType = "File/Data.xls";
StringWriter StringWriter = new System.IO.StringWriter();
HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);
GridEmpData.RenderControl(HtmlTextWriter);
Response.Write(StringWriter.ToString());
Response.End();
}
Made reference to this website http://www.c-sharpcorner.com/UploadFile/b926a6/gridview-data-export-into-excel-file-format/
Maybe take a look? There is databases there although the answer on how to convert a gridview to an Excel file is there and the code is above.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.