Export To Excel Excel

Do we have any facility to export grid data in Excel with using Report Tool.

Just a question, Will Response.contenet Type will work here

Thanks,

0
Sandy1234
3/5/2009 12:20:10 PM
asp.net.getting-started 91979 articles. 3 followers. Follow

3 Replies
899 Views

Similar Articles

[PageSpeed] 25

private void btnExportToExcel_Click(object sender,System.EventArgs e)
{
ExportToExcel("Report.xls",DataGrid1);
}

private void ExportToExcel(string strFileName,DataGrid dg)
{
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("content-disposition",
"attachment;filename=" + strFileName);
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter sw = new StringWriter();
System.Web.UI.HtmlTextWriter htw = new HtmlTextWriter(sw);
dg.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
Chinna_sv...
0
getchinna_sv
3/5/2009 1:09:10 PM

dear, 

try this:

 gvExportedList is a Gridview

Response.ContentType = "application/ms-excel";

Response.AddHeader("content-disposition", "attachment;filename=" + FileName);

StringBuilder sb = new StringBuilder();

System.IO.StringWriter stringWrite = new System.IO.StringWriter(sb);

System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

gvExportedList.RenderControl(htmlWrite);

Response.Write(sb.ToString());

Response.End();


Kind Regards,
Saurabh Sharma

only dead fish swim along the flow of river, learn to make your own way.


Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread
0
saurabhsharma18
3/5/2009 1:28:35 PM

Sandy,

Saurabh's technique is the one that I have used for a while and it works very well.  I have found that instantiating a new GridView control and binding to the datasource of the original GridViews allows some simply formatting techniques:

Dim filename As String = "Report_" & String.Format("{0:MM-dd-yyy-hh-mm}", Now) & ".xls"

Dim gv As New GridView

gv.EnableViewState = False

gv.GridLines = GridLines.Both

gv.HeaderStyle.BackColor = System.Drawing.Color.FromArgb(208, 208, 208)

gv.HeaderStyle.Font.Bold = True

gv.DataSource = SQLDataSource1

gv.DataBind()

Response.Clear()

Response.Buffer = True

Response.AddHeader("content-disposition", "attachment; filename=" & filename)

Response.ContentType = "application/vnd.ms-excel"

Response.ContentEncoding = System.Text.Encoding.UTF8

Response.Charset = ""

Dim sw As New System.IO.StringWriterDim hw As New HtmlTextWriter(sw)

gv.RenderControl(hw)

Response.Write(sw)

Response.End()

 

        Dim filename As String = "Report_" & _
     String.Format("{0:MM-dd-yyy-hh-mm}", Now) & _
     ".xls"

        Dim gv As New GridView
        gv.EnableViewState = False
        gv.GridLines = GridLines.Both
        gv.HeaderStyle.BackColor = System.Drawing.Color.FromArgb(208, 208, 208)
        gv.HeaderStyle.Font.Bold = True
        gv.DataSource = SQLDataSource1
        gv.DataBind()

        Response.Clear()
        Response.Buffer = True

        Response.AddHeader("content-disposition", "attachment; filename=" & filename)
        Response.ContentType = "application/vnd.ms-excel"
        Response.ContentEncoding = System.Text.Encoding.UTF8
        Response.Charset = ""

        Dim sw As New System.IO.StringWriter
        Dim hw As New HtmlTextWriter(sw)

        gv.RenderControl(hw)
        Response.Write(sw)
        Response.End()

Hope it helps.  Grady Christie  

 

0
Grady
3/5/2009 4:00:29 PM
Reply:

Similar Artilces:

Exporting to Excel in Crystal Reports .NET
sir,   I want to export Crystal report to Excel and the following is the code i wrote in the button click.ReportDocument rptExcel = new ReportDocument() ;string strExportFile = "C:/Session_SessionID.xls" ;rptExcel = new RptJobOrderRecap();//report namerptExcel.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile ;rptExcel.ExportOptions.ExportFormatType = ExportFormatType.Excel;ExcelFormatOptions objExcelOptions = new ExcelFormatOptions();objExcelOptions.ExcelUseConstantColumnWidth = false ;rptExcel.ExportOptions.FormatOptions = objExcelOptions ;DiskFileDestinationOp...

In gridview export to excel how to get colors in excel sheet
 Hello All, I am struck with a small problem here.I am displaying a gridview with some colorful rows.But when I am exporting the gridview to excel these colors are not appearing.Please give me a solution.Below i am giving the class file i used to export gridview to excel. public void ExportGridToExcel(DataSet ds, HttpResponse response,string file_name)    {        //first let's clean up the response.object        response.Clear();        response.AddHeade...

when export datagrid to excel, excel start from second row
 as a titlei use this code : Private Sub StampaExcel() Response.ContentType = "application/vnd.ms-excel" Response.Charset = "" Response.AddHeader("content-disposition", "inline;filename=StampaCosti.xls") Me.EnableViewState = False Dim tw As New System.IO.StringWriter Dim hw = New System.Web.UI.HtmlTextWriter(tw) 'Me.Page.RenderControl(hw)'comment for render page, but do not work Me.DG_Lista.RenderControl(hw) Response.Write(tw.ToStrin...

exporting to excel
Hi all, I read some the threads on here regarding exporting a gridview to excel. particularly: http://forums.asp.net/thread/1488594.aspx I thought i had it working but i discovered a bug. If the datagrid is displayed with paging and sorting, and then i click the export button, it exports with all the paging and sorting stuff. Which means it displays the 'edit' link, and its only displaying 1-100 records (or whatever page # i was on). If i click the export button before the grid is displayed (i dont display the grid until they click a search button) then it exports correctly showing all r...

Exporting to Excel (No MS EXcel on the Server)
I am binding a dataset to my GridView. I want to Export it to Excel. Server does not have MS Excel and we cannot install it. All the Client Machines will have MS Excel. How to start about it? Any Article to start with? Thanks I think if you install the Microsoft Visual Studio 2005 Tools for the Microsoft Office System you will have access to the Microsoft.Office.Interop.Excel namespace to do this. There is also a runtime that must be installed (or setup with the deployment project) on the server. Here's a good starting pount from MSDN.  Check this article - http://mattberseth.c...

button click Export to Excel = Right Click Export to Excel
I have code which exports to Excel.  However, I would like to remove these pages and just call the event to "Export to Excel" similar to "Print".  You know how you can just add a Print button and the page is printed?  In IE 7.0 you can right click on the page and "Export to Excel", I would like to invoke that command and export the page to Excel exactly the way it looks in the browser.  Does anyone know how to do this? Yes, we are all using IE 7.0 and it is an internal application.  "From my point of view, the Jedi are...

EXPORT TO EXCEL NOT WORKING IN EXCEL 2007
Export to excel functionality is not working in excel 2007 but its working in earlier versions , code to export to excel contains changing mime type of response object . Is it a known bug ? Thx in advance...Giri Have a look at this thread. It will clear your doubts. http://forums.asp.net/t/1221467.aspxThanks,MaxLet Me Google That For You!...

button Export to Excel = Right Click "Export to Microsoft Excel"
Ok, I know how to export data to Excel, what I would like is similar to the print button.  You know how you can just add a Print button and the page is printed?  In IE 7.0 you can right click on the page and "Export to Excel", I would like to invoke that command and export the page to Excel exactly the way it looks in the browser.  Does anyone know how to do this? Yes, we are all using IE 7.0 and it is an internal app."From my point of view, the Jedi are evil!" Nobody knows how to do this?"From my point of view, the Jedi are evil!" IT's b...

Getting started with Excel
Hi everyone,  Can someone please provide me with a link that will tech me how to Create, Read/Write and deal with Excel files.  What I am doing right now is passing a SQL string to a class which is exporting values in .csv format. Then the user will open it with excel by default and save it as excel. I have Visual Studio 2008 and I am creating ASP.NET 2.0 webpages. (I guess I could do 3.5 but my server does not have 3.5 framework yet)  Oh Lord, you can image where it goes from here. He fixes the cable? Friend, For creating Excel File : http://www.eggheadcafe.com/a...

How to get Excel.Application and Excel.Workbook Classes
Hi AllBoth Application and Workbook are classes,can be got from Imports Microsoft.Office.Interop.Excel dll. I have written below using the above dll. But In production I cannot deploy the above dll due to some concerns.I my company not allowed to deploy the above dll. So, let me know if any other way present to implement this without using dll. And one more suggestion, is it possible to get Application and Workbook classes source code so that I can embed these classes directly into my application code. Please provide me solution for this problem.    Di...

Exporting data to Excel and protecting Excel worksheet
Hi All, In my application I have to export some data to excel and after exporting I have to protect specific range in excel worksheet from editing. I can export data without any problems but for some reason excelWorksheet.Protect () method is not working. Here is my code: Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); string workbookPath = ConfigurationManager.AppSettings["ExcelPath"] + @"\MyFile.xls"; Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, false, 5, "", "", false, Micr...

Grid Export to Excel with Excel Page Headers
Hi all, Using vb.net and this code: Response.Clear()Response.Charset = ""Me.EnableViewState = TrueResponse.ContentType = "application/vnd.ms-excel"Dim stringWrite As New System.IO.StringWriterDim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)Response.AddHeader("Content-Disposition", "attachment;filename=c:\membership.xls") DataGrid1.RenderControl(htmlWrite)Response.Write(stringWrite.ToString)Response.End() My DataGrid1 doesn't have a header.  What I need to be able to do is write something into the Excel file so Excel will trea...

Export to Excel
The below code is working at home. When i run this at my office, it does not show the excel when i click the button. It just does nothing. can anybody help me please. Thank you very much.Here is the code:Private Sub printToExcel(ByVal pCustomer As String)        Dim connString As String = ConfigurationManager.ConnectionStrings("QBRISC").ConnectionString        Dim conn As New SqlConnection(connString)        Dim command As SqlCommand        Dim...

Export Report to Excel using a Excel Template
 I looking for assistance with the following. I have a SQL Server Report which when exported to Excel is difficult to read. So, I created an Excel Template which I would like to use in place of the default Excel file Reporting Services exports to. How do modify my code so that my Excel template is used instead of the default when the user exports the report??? Below is my code. Any assistance is appreciated.  test.aspx <%@ Page Language="C#" Debug="true" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %&g...

Web resources about - Export To Excel Excel - asp.net.getting-started

Export - Wikipedia, the free encyclopedia
An export of a good occurs when there is a change of ownership from a resident to a non-resident; this does not necessarily imply that the good ...

Export - Wikipedia, the free encyclopedia
An export of a good occurs when there is a change of ownership from a resident to a non-resident; this does not necessarily imply that the good ...

Facebook To Developers: No Using Platform To Export User Data To Other Social Networks
Facebook issued a stern reminder to developers that its platform cannot be used to export user data to other social networks without permission. ...

Facebook Users Can Now Opt Into Letting Friends Export Their Email Addresses via Download Your Information ...
Previously, Facebook's Download Your Information tool let users export an archive of their status updates, photos, as well as a list of friends' ...

Thread: Why is Twitter letting us export?
... Instagram and how they own your pictures (not mine, I never used the service) it raises the question as to why Twitter is letting us export ...

Export Import Business Group
Here we are, people in the business of export and import from all over the world. I invite you people also to join for more info of the business ...

Contacts Backup & Transfer - Sync, copy and export your whole address book in vcard/vcf format on the ...
Get Contacts Backup & Transfer - Sync, copy and export your whole address book in vcard/vcf format on the App Store. See screenshots and ratings, ...

About 13,000 sheep stranded on export ship off Fremantle
A ship carrying about 13,000 live sheep and cattle remains stranded in Western Australia after an engine problem.

Croc plan to boost NT product export
... crocodile eggs can now be harvested from the wild under a new Northern Territory trade management plan aimed at bolstering the product export ...

Recharge Wrap-up: Tesla Model X towing video, ethanol exports to China rise
... Automakers , Electric What does towing do to the Tesla Model X's driving range? A new blog describes buying a Tesla in Portugal. Ethanol exports ...

Resources last updated: 1/9/2016 8:42:54 PM