In an ASP.NET site, I am rendering a GridView control to export it's details in a excel file
Response.ContentType = "application/vnd.ms-excel"
Response.AppendHeader("Content-Type", "application/vnd.ms-excel")
Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter
Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite)
gridviewObject.RenderControl(htmlWrite)
Response.Write(stringWrite.ToString())
Response.End()
Now, it's getting rendered,but the gridview uses few images to beautify the data.
While exporting the data I don't want to show images in excel.
So I want to find the tag and want to replace it by blank string.
Can anybody tell me how I can do that?
Related
We want to create a CSV file from data .It is showing HTML tags .Same working fine for xlx file.
Below is code.
Dim response As System.Web.HttpResponse = System.Web.HttpContext.Current.Response
response.Clear()
response.ClearHeaders()
response.Write("<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8""/>")
'Chose what type of file needed ie.e CSV /XLS
If _exportType = ExportTypeEnum.CSV Then
response.ContentType = "application/vnd.xls"
Else
response.ContentType = "application/vnd.xls"
End If
response.AddHeader("Content-Disposition", "attachment;filename=" & FileNameToExport)
Dim sw As System.IO.StringWriter = New StringWriter()
Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
Dim dg As DataGrid = New DataGrid()
Dim dv1 As DataView = data
dg.DataSource = dv1
dg.BorderStyle = BorderStyle.None
dg.DataBind()
dg.RenderControl(htw)
response.Write(sw.ToString())
response.End()
Please suggest WHY it is not working for CSV file , although it is fine for xls file.
Many thanks in advance
You're explicitly writing the output in HTML format. Notice this line:
response.Write("<meta http-equiv=""Content-Type"" content=""text/html; charset=UTF-8""/>")
And notice the object you're using to write output:
Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
This is all emitting HTML to the output. It "works fine" for an Excel file because Excel understands HTML structures when rendering a file. CSV, however, is a much "flatter" format and only knows plain text (delimited by a comma). You'll also see the HTML tags if, for example, you use this code to write to a .txt file.
If you want a CSV file without HTML, you shouldn't write HTML content to it.
Im using the following code in order to export a gridview to excel
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
HtmlForm frm = new HtmlForm();
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
EnableViewState = false;
Controls.Add(frm);
frm.Controls.Add(this.myGridView);
frm.RenderControl(hw);
Source for above exporting code
Everything works like a charm.
Except I now have to make changes to the gridview before it gets exported to Excel.
E.g. One change is that I needed to remove a column.
Yet I cant do this:
//I can't remove columns here, since it has not been rendered yet and has 0 columns
frm.Controls.Add(this.myGridView);
frm.RenderControl(hw);
Is there a way to edit the Gridview before it is exported? e.g .Remove a column
string attachment = "attachment; filename=Memberslist.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
completeGV.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(completeGV);
frm.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
I'm using the above code for exporting the excel sheet but i need to freeze the header row while exporting the excel sheet. please help me out... this is killing me...
I am developing an application where i want i am displaying a dataset in the datagrid view for the user.Now the user wants to download the data in the datagridview in an excel format.How can i do it ?
1) should i write the dataset in the excel and save it the server before the user download the file ?
2) Can i use a hyper link and set the path of the file that is saved in the server to the hyper link hRef property , so that the user can click and download the file ?
I am using C# ASP.net 2.0
Please help !
no need to save the file to the sever use the following code, will create the file on the fly:
Public Shared Sub DataTableToExcel(ByVal dt As DataTable, ByVal FileName As String
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.Write(Environment.NewLine)
For Each row As DataRow In dt.Rows
For i As Integer = 0 To dt.Columns.Count - 1
HttpContext.Current.Response.Write(row(i).ToString().Replace(";", String.Empty) + ";")
Next
HttpContext.Current.Response.Write(Environment.NewLine)
Next
HttpContext.Current.Response.ContentType = "application/ms-excel"
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName + ".xls")
HttpContext.Current.Response.[End]()
End Sub
you can use this simple code to accomplish
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
string attachment = "attachment; filename=SummaryReport" + DateTime.Now.ToString() + ".xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
grd.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
SpreadsheetGear for .NET will do it.
You can see ASP.NET (C# and VB) samples here and download a free trial here.
Disclaimer: I own SpreadsheetGear LLC
I have an ASP.NET page like this:
http://farm4.static.flickr.com/3631/3690714302_c17b259863.jpg
My table is Gridview and some Label, anybody can tell me how to create a button to convert my page to image or PDF file and save it to desktop.
It can be done by code on the backend that renders the html into a memory Graphic object and then serves that resultant bitmap back, or by printing the page (on the server) through a PDF writer and then capturing the result.
Javascript, no.
Or google "online convert html pdf" and use one of those services.
Hai use iTextSharp to convert your webpage to pdf.
It looks very nice.
Just download the dll file and add reference to your application.
Then in button click give the coding as follows.
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.AddHeader("content-disposition", attachment; filename="jaison.pdf")
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Dim sw As New StringWriter()
Dim hw As New HtmlTextWriter(sw)
Me.Page.RenderControl(hw)
Dim sr As New StringReader(sw.ToString())
Dim pdfDoc As New Document(iTextSharp.text.PageSize.A2, 10, 10, 10, 10)
Dim htmlparser As New HTMLWorker(pdfDoc)
PdfWriter.GetInstance(pdfDoc, Response.OutputStream)
pdfDoc.Open()
htmlparser.Parse(sr)
pdfDoc.Close()
Response.Write(pdfDoc)
Response.End()
Surely this code will help you.
All the best!