Hello I am trying to export gridview data into excel here are parts of my code , and well it creates the file but when i open the file exel says its not exl file and when i force open it shows the info as needed ( altough with blank area all around the grid ) beside this i followed the code and i noticed it catches an exeption ex which says something like this :
"'ctl00_ContentPlaceHolder1_approvalGrid' 'GridView' runat=server."
and it talks about tag type and putting ( its kinda messy because it shows it in my language )
Code-behind:
Protected Sub exportExelBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exportExelBtn.Click
If approvalGrid.Rows.Count > 0 Then
Try
Response.ClearContent()
Response.Buffer = True
Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", "poView.xls"))
Response.ContentEncoding = Encoding.UTF8
Response.ContentType = "application/ms-excel"
' Dim sw As New stringwriter()
Dim tw As New IO.StringWriter()
Dim htw As New HtmlTextWriter(tw)
approvalGrid.RenderControl(htw)
Response.Write(tw.ToString())
Response.[End]()
Catch ex As Exception
End Try
END IF
END SUB
Markup:
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="PoViewTable.aspx.vb" Inherits="PoViewTable" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:GridView ID="approvalGrid" runat="server" AllowSorting="True"
AutoGenerateColumns="False" DataSourceID="poViewSql" Visible="False">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="poID" HeaderText="poID" SortExpression="poID" visible = "false"/>
<asp:BoundField DataField="companyID" HeaderText="companyID"
SortExpression="companyID" />
<asp:BoundField DataField="requesterID" HeaderText="requesterID"
SortExpression="requesterID" />
<asp:BoundField DataField="departmentID" HeaderText="departmentID"
SortExpression="departmentID" />
<asp:BoundField DataField="subDepartmentID" HeaderText="subDepartmentID"
SortExpression="subDepartmentID" />
<asp:BoundField DataField="date" HeaderText="date" SortExpression="date" />
<asp:BoundField DataField="amount" HeaderText="amount"
SortExpression="amount" />
<asp:BoundField DataField="supplierID" HeaderText="supplierID"
SortExpression="supplierID" />
<asp:BoundField DataField="comments" HeaderText="comments"
SortExpression="comments" />
<asp:BoundField DataField="managerID1" HeaderText="managerID1"
SortExpression="managerID1" />
<asp:BoundField DataField="managerStatus1" HeaderText="managerStatus1"
SortExpression="managerStatus1" />
<asp:BoundField DataField="managerID2" HeaderText="managerID2"
SortExpression="managerID2" />
<asp:BoundField DataField="managerStatus2" HeaderText="managerStatus2"
SortExpression="managerStatus2" />
<asp:BoundField DataField="poStatus" HeaderText="poStatus"
SortExpression="poStatus" />
</Columns>
</asp:GridView>
<br />
...
...
</asp:Content>