Darrell Norton's Blog [MVP]

Sponsors

The Lounge

News

  • Darrell Norton pic

    MVP logo

    View Darrell Norton's profile on LinkedIn

    Currently Reading:

    weewar.com

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Export a DataGrid to Excel

I just found a neat way to output the results of an ASP.NET DataGrid to Excel.  Just put the following code in a button_click event handler, and when a user clicks the button, it will output the DataGrid, including formatting, to a new Excel window, leaving the browser window undisturbed.  Of course this assumes your users have Excel.

' Set the content type to Excel
Response.ContentType = "application/vnd.ms-excel"

'Turn off the view state
Me.EnableViewState = False

'Remove the charset from the Content-Type header
Response.Charset = String.Empty

Dim myTextWriter As New System.IO.StringWriter()
Dim myHtmlTextWriter As New System.Web.UI.HtmlTextWriter(myTextWriter)

'Get the HTML for the control
myDataGrid.RenderControl(myHtmlTextWriter)

'Write the HTML to the browser
Response.Write(myTextWriter.ToString())

'End the response
Response.End()

Update: Added the Response.ContentType line which I had inadvertently forgotten.

Update 2:  Brendan Tompkins has graciously offered 3 XSLT files to transform a DataGrid's XML into Excel, comma-delimited, and tab-delimited files.  Thanks Brendan!


Posted 02-12-2004 7:50 PM by Darrell Norton

[Advertisement]

Comments

Robert Hurlbut wrote re: Export a DataGrid to Excel
on 02-12-2004 3:36 PM
Darrell -- just looking at the code, I don't understand how it knows to go to Excel? Where is the "hook", or am I missing something? Thanks, Robert
JosephCooney wrote re: Export a DataGrid to Excel
on 02-12-2004 4:58 PM
I don't know if it is what Darrell had in mind, but I think if you set the mime type to " application/excel" in the header then this would display in the client in Excel, but maybe there is some "tick" or "hook" that I missed too. I found a fairly extensive list of MIME types here: http://www.webmaster-toolkit.com/mime-types.shtml
Robert Hurlbut wrote re: Export a DataGrid to Excel
on 02-12-2004 10:52 PM
Thanks for the great link, Joseph.
Darrell wrote re: Export a DataGrid to Excel
on 02-13-2004 12:36 AM
Robert and Joseph - yes I forgot the MIME type line! Thanks for pointing it out! I have fixed the code example by adding two lines of code above.
Brendan Tompkins wrote re: Export a DataGrid to Excel
on 02-13-2004 1:51 AM
D-Ral.. Yep this is cool. You can also try transforming your ds.GetXml() using a variety of different methods to return comma delim, or even ExcelXML... Try these XSLT files, http://www.intrinsigo.com/bsblog/ExcelXML.zip against your DataSet's GetXml() string, before you do your Response.Write

-B

Darrell wrote re: Export a DataGrid to Excel
on 02-13-2004 2:16 AM
Nice, thanks Brendan!
jeremy t wrote re: Export a DataGrid to Excel
on 03-24-2004 3:08 PM
Has anyone yet encountered the msgbox "Unable to Read file" if the datagrid contains over say 100 records ?
Darrell wrote re: Export a DataGrid to Excel
on 03-24-2004 3:20 PM
Jeremy - no I haven't. The tests I ran were on pages with around a hundred rows, but I didn't count to make sure. Hopefully I can test again soon.
Bill wrote re: Export a DataGrid to Excel
on 04-03-2004 2:40 AM
Jeremy, I get the 'Unable to read file' error like you, after approx. 100 rows. Have you found a resolution?
Darrell wrote re: Export a DataGrid to Excel
on 04-03-2004 6:26 AM
Bill - if you could send me (email me via the blog and I'll reply) a copy of what you are using I could try to fix it. I can't seem to replicate this for the life of me. Also, what version of Excel are you testing on?
Yvonne wrote re: Export a DataGrid to Excel
on 04-06-2004 3:46 AM
I have been running into the exact same problem the code below fixed it

IMPORTANT: Excel XP will not show the report if this line is not added:
EnableViewState = false; If you don't do it , it will tell you that it is "unable to read the file"
Darrell wrote re: Export a DataGrid to Excel
on 04-06-2004 4:45 AM
Yvonne - I already had that line of code in there.
chris wrote re: Export a DataGrid to Excel
on 04-13-2004 3:33 AM
I got an error
alishcka wrote re: Export a DataGrid to Excel
on 04-21-2004 4:33 AM
I have other kind of solution for the same problem it's called dataset to excel. I didn't send datagrid to excel but I send to dataset which fill the grid. you can find code in the internet for this solution. but main problem is for me I don't live in the English spoken countries so how can I fit the charset problem
because in turkey we use latin alfabet but some kind of vovels not have equvalent.
so if you know any kind of way please share.
Darrell wrote re: Export a DataGrid to Excel
on 04-21-2004 4:48 AM
alishcka - I thought setting the Response.Charset to an empty string would solve that problem. I'm not sure on how to internationalize this, since I don't have that much experience in this area.
Kevin wrote re: Export a DataGrid to Excel
on 04-23-2004 8:59 AM
I get this error when the export button is pushed System.Web.HttpException: Control 'dgCore__ctl2__ctl0' of type 'Button' must be placed inside a form tag with runat=server.

and it refers to the line:
dgCore.RenderControl(myHtmlTextWriter)

My datagrid is inside the form tags so i do not know what the problem is? Does anyone know?
Darrell wrote re: Export a DataGrid to Excel
on 04-23-2004 9:28 AM
Kevin - I haven't seen that error message before. Can you send some sample code via the contact page?
Kevin wrote re: Export a DataGrid to Excel
on 04-23-2004 9:38 AM
Here is the code for the button:

Sub exportbutton_onclick(s as Object, e as EventArgs)

' Set the content type to Excel
Response.ContentType = "application/vnd.ms-excel"

'Turn off the view state
Me.EnableViewState = False

'Remove the charset from the Content-Type header
Response.Charset = String.Empty

Dim myTextWriter As New System.IO.StringWriter()
Dim myHtmlTextWriter As New System.Web.UI.HtmlTextWriter(myTextWriter)

'Get the HTML for the control
dgCore.RenderControl(myHtmlTextWriter)

'Write the HTML to the browser
Response.Write(myTextWriter.ToString())

'End the response
Response.End()

End Sub
Darrell wrote re: Export a DataGrid to Excel
on 04-24-2004 4:14 AM
Kevin - I'm not ignoring, just have a lot going on this weekend. I'll work on it some and try to get back to this blog entry. Also you can send me an email via the Contact page and I'll get in touch with you in person.
Danny wrote re: Export a DataGrid to Excel
on 04-25-2004 1:55 AM
Hi Kevin,

You will get the error 'must be placed inside a form tag with runat=server' if the datagrid contains a control.

In your case you have the datagrids AllowSorting="true" which makes the headers of the datagrid a link which is then a control.
Darrell wrote re: Export a DataGrid to Excel
on 04-25-2004 4:41 AM
Danny - thanks! Good answer.
Kevin wrote re: Export a DataGrid to Excel
on 04-26-2004 10:28 AM
Danny,
thanks for the help what it was is that i had a column for editing the records so that is why it was not working. Thanks for the help!!!
Darrell wrote re: Export a DataGrid to Excel
on 04-26-2004 10:57 AM
Look what happens when people try to get along and help each other. Can't we all just... get along? :)
Kevin wrote re: Export a DataGrid to Excel
on 04-26-2004 11:43 AM
Is there a way to open the excel in a new window?
Darrell wrote re: Export a DataGrid to Excel
on 04-26-2004 2:42 PM
Kevin - add this line of code right before the Response.ContentType line:

//This will make the browser interpret the output as an Excel file
Response.AddHeader( "Content-Disposition", "filename="+someName);
kevin wrote re: Export a DataGrid to Excel
on 05-03-2004 8:10 AM
what is suppose to be placed in the someName spot because when i put that in there it tells me that i have not declared someName!!
thanks!
Darrell wrote re: Export a DataGrid to Excel
on 05-03-2004 9:07 AM
Kevin - someName is just a string variable that holds a file name. You can use "myExcelFile.xls" if you want.
Thomas wrote Response.End thread was being aborted
on 05-10-2004 2:50 AM
Response.End thread was being aborted

How can i fix it ?
Sal wrote re: Export a DataGrid to Excel
on 05-10-2004 9:55 AM
Code works great for basic grid. Thanks!
It does fail, however, if you use the DataGrid's AllowPaging property (which I find useful for presenting large data tables. Does anyone have a solution to this one (besides turning off paging)? I've tried and failed.
Darrell wrote re: Export a DataGrid to Excel
on 05-10-2004 10:05 AM
Sal - try checking out Brendan's utility class here: http://dotnetjunkies.com/WebLog/bsblog/archive/2004/04/27/12229.aspx
larry wrote re: Export a DataGrid to Excel
on 06-09-2004 11:59 AM
how about exporting just one row of the datagrid to excel.
Darrell wrote re: Export a DataGrid to Excel
on 06-09-2004 3:43 PM
Set the pagesize of the DataGrid to 1, then rebind the datagrid, and then export to Excel.
Larry wrote re: Export a DataGrid to Excel
on 06-10-2004 4:59 AM
I'll try that. But, how does that give it the row that I want?
Darrell wrote re: Export a DataGrid to Excel
on 06-10-2004 5:14 AM
Larry - make sure to sort the datagrid so that the row you want is at the top. Is your datagrid bound to a dataset? If so, use the Find method to select the individual row.
gaye wrote re: Export a DataGrid to Excel
on 06-29-2004 7:43 AM
I was trying to open the excel in a new window using the information above:

Response.AddHeader( "Content-Disposition", "filename="+someName);


But this doesn't open in a new window -- it opens in a new page -- is there a way to open in a new window?? or am I doing something wrong?

Thanks, Gaye
Darrell wrote re: Export a DataGrid to Excel
on 06-29-2004 9:00 AM
Gaye - what do you mean by opens in a new page? Are you using Mozilla or IE? Is it a new page in Excel or the browser?
Gaye wrote re: Export a DataGrid to Excel
on 06-29-2004 9:53 AM
Hi Darrell,

The way it works is it opens a new page in excel within my existing browser. If the user closes - what they think is excel - it closes the whole browser. I would like to open the new excel page in a new browser -- so if the user closes the excel page - it doesn't close the original browser. Hope that makes sense.

I'm using IE.

Thanks, Gaye
Darrell wrote re: Export a DataGrid to Excel
on 06-29-2004 10:03 AM
In the button's properties, have the target set to _new.
Gaye wrote re: Export a DataGrid to Excel
on 06-29-2004 10:24 AM
I don't find target as a property for the button control. It's not a button column on the datagrid --it's just an asp button at the bottom of my page.
Darrell wrote re: Export a DataGrid to Excel
on 06-29-2004 10:35 AM
Tim wrote re: Export a DataGrid to Excel
on 07-06-2004 5:59 AM
Works great, was wondering how to change the value going in to Excel as 'TEXT' not 'GENERAL' most of the account numbers are coming as 2.1999457E+1 so really from 'GENERAL' to 'NUMBER' etc...? Or if you have any other sugests..
Darrell wrote re: Export a DataGrid to Excel
on 07-06-2004 6:25 AM
Take a look at Jason Haley's blog post here:
http://dotnetjunkies.com/WebLog/jhaley/archive/2004/03/20/9583.aspx

He includes formulas, so I'm sure a similar method could be used to include cell formatting for numbers and so forth.
Ben White wrote re: Export a DataGrid to Excel
on 07-27-2004 10:53 PM
Has anyone come up with a workable solution to opening the report up in a new window?
Ben wrote Export datagrid to excel : Open in new window
on 07-28-2004 3:27 AM
OK -

OPEN REPORT IN NEW WINDOW : you need to add the following code.

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

The code above gives a file download dialogue box and the user can select save or open.

TO REMOVE UNWANTED CONTROLS (eg. buttons that cause a form error)

You will need to remove the column from the datagrid. Code has been posted suggesting that the following will work :

clientval.Columns.Remove(clientval.Columns.Item(11))

The above will not work as the datagrid has to be rebound in order for the column changes to take effect.

I ended up using a function to return a dataset, removing the column then binding the datagrid again before exporting to XLS.

Hope this helps (I have just spent best part of a day on this!!!)

Darrell wrote re: Export a DataGrid to Excel
on 07-28-2004 3:29 AM
Thanks Ben!
Adam wrote re: Export a DataGrid to Excel
on 07-30-2004 3:51 AM
I am also trying to output additional lines to the Excel spreadsheet. I am adding lines like the following in the button click:

myHtmlTextWriter.WriteLine(lblStartDate.Text)
myHtmlTextWriter.WriteLine(lblEndDate.Text)

Unfortunately, even though this is supposed to write line terminators, it doesn't and all the additional text is on the same line.

does anyone know how to get each WriteLine to output to a new excel line?
Darrell wrote re: Export a DataGrid to Excel
on 07-30-2004 4:01 AM
Have you tried something like this?

myHtmlTextWriter.WriteLine(lblStartDate.Text)
myHtmlTextWriter.WriteLine(Environment.Newline)
myHtmlTextWriter.WriteLine(lblEndDate.Text)
Adam wrote re: Export a DataGrid to Excel
on 07-30-2004 4:31 AM
Darrell. Adding the Environment.Newline doesn't work. Thanks for the idea though.
Adam wrote re: Export a DataGrid to Excel
on 07-30-2004 4:41 AM
Interestingly I found out that the data grid output is in HTML format however the WriteLine output is not. The debugger showed me this.

I guess this means I need to format my WriteLine output in HTML and add a table and it should work.




?myTextWriter.ToString()
"Start Date: 6/6/2004


End Date: 6/6/2004


<table cellspacing="0" cellpadding="4" rules="all" class="standard_font" bordercolor="#CCCCCC" border="1" id="datagridCommissionReport" style="border-color:#CCCCCC;border-width:1px;border-style:Solid;border-collapse:collapse;">
<tr class="tabletitle">
<td>Title</td><td>Title1</td><td>Retail</td><td>Title2</td><td>Title3</td><td>Title4</td><td>Title5</td><td>Title6</td><td>Title7</td><td>Title8</td><td>Title9</td>
</tr><tr>
<td nowrap="nowrap" style="font-weight:bold;">X,X</td><td align="Right">$0.00</td><td align="Right">$0.00</td><td align="Right">$0.00</td><td align="Right">$0.00</td><td align="Right">$0.00</td><td align="Right">$0.00</td><td align="Right">$0.00</td><td align="Right">$0.00</td><td align="Right">$0.00</td><td align="Right">$0.00</td>
</tr><tr class="tablelightcell">
<td nowrap="nowrap" style="font-weight:bold;">X,X</td><td align="Right">$0.00</td><td align="Right">$0.00</td><td align="Right">$0.00</td><td align="Right">$0.00</td><td align="Right">$0.00</td><td align="Right">$0.00</td><td align="Right">$0.00</td><td align="Right">$0.00</td><td align="Right">$0.00</td><td align="Right">$0.00</td>
</tr>
</table>"
Darrell wrote re: Export a DataGrid to Excel
on 07-30-2004 5:13 AM
Interesting.
Chris wrote re: Export a DataGrid to Excel
on 08-18-2004 3:04 AM
Does anyone know how to change the sheet name produced so that it doesn't just default to the actual name of the page?

Thanx, Chris
Geert wrote re: Export a DataGrid to Excel
on 09-07-2004 7:59 PM
Great stuff! Just what I needed :-)
Brian wrote re: Export a DataGrid to Excel
on 09-14-2004 8:02 AM
When user hit open in file download dialogue box? It doesn't open a new window. Please help.
THanks
Raj wrote re: Export a DataGrid to Excel
on 09-16-2004 11:13 AM
Brian,
Did you get your excel sheet to open in a new browser window? If not let me know and I can help. I just got mine to work. Thanks
Andrew wrote re: Export a DataGrid to Excel
on 09-16-2004 9:27 PM
I am trying to export to excel and that works fine! but i then wish to import that data back to the server at a later date! my problem being i hav a validation check and i need the saved .XLS file to be in application/vnd.ms-excel format. it is however not in this format so it does not work!? any ideas

- andrew.j.h.phillips@btinternet.com
Darrell wrote re: Export a DataGrid to Excel
on 09-17-2004 2:54 AM
Andrew - see if any of the resources on this page help: http://weblogs.asp.net/sbehera/archive/2003/11/13/37251.aspx
stefan demetz wrote re: Export a DataGrid to Excel
on 10-01-2004 11:23 AM
Brian wrote re: Export a DataGrid to Excel
on 10-12-2004 4:40 AM
To open it in a new window simply direct the form at the top of the report to
target = "_blank"
Nand wrote re: Export a DataGrid to Excel
on 10-12-2004 9:20 PM
i ve used the same code as above but it does not work
it opens the excel sheet with controls in web page.The data grid that i ve is a simple one with no controls.pls let me know what is wrong
Nand wrote re: Export a DataGrid to Excel
on 10-12-2004 9:21 PM
this is the code i use

response.Clear()
' Response.buffer=true

Response.ContentType = "application/vnd.ms-excel"
Me.EnableViewState = false
Response.Charset = String.Empty



Dim tw As New System.IO.StringWriter()
Dim hw As New System.Web.UI.HtmlTextWriter(tw)

dgExport.RenderControl(hw)

response.write(tw.Tostring())
response.end
nand wrote Export a DataGrid to Excel
on 10-12-2004 10:29 PM
Let me know what is problem with above code
Darrell wrote re: Export a DataGrid to Excel
on 10-13-2004 2:09 AM
To open it in a new window simply direct the form at the top of the report to
target = "_blank"
nand wrote re: Export a DataGrid to Excel
on 10-14-2004 11:05 PM
Darrell ,can u help me with the problem.
The above is the code i used.it opens the excel file with controls in web page but not data in data grid.
Darrell wrote re: Export a DataGrid to Excel
on 10-15-2004 1:33 AM
Nand - try checking out a complete tutorial here:
http://www.dotnetjohn.com/articles/articleid78.aspx
Nand wrote re: Export a DataGrid to Excel
on 10-16-2004 10:17 PM
Darrell i ve gone thru the code..if possible can u send me the screen shot and code.if u tell ur email address i could give u how my excel file looks...i use notepad to type my code and not the VS editor..is there any meta tag to add
Darrell wrote re: Export a DataGrid to Excel
on 10-18-2004 1:46 AM
No meta tags or anything of that nature. You can always post your problem on the ASP.NET Forums. If you post your code there people generally can answer your question right away. Post it to this forum:

http://asp.net/Forums/ShowForum.aspx?tabindex=1&ForumID=24
POD wrote re: Export a DataGrid to Excel
on 11-02-2004 11:45 AM
I have a question guys, i dunno much about ASP, so i do most programs on VB and Access. I am creating a program now on VB and I'm kindly asking you guys if you can help me export data on my VB Datagrid to an Excel file (template Excel file - meaning the file is already existing and program should fill up which columns i want to fill up). Example Col1 & Row1 of VB Datagrid should fill up Col5 and Row5 of my Excel Template. Or any workaround will do. Please do help me if you have any idea on this. thanks....
Temp wrote re: Export a DataGrid to Excel
on 11-04-2004 2:29 AM
I am getting error response.End() part. It says 'Thread was being aborted'. Could any one help me fix this.
Lori wrote re: Export a DataGrid to Excel
on 12-01-2004 8:12 AM
I have the export to excel code (shown at the top) and it was working great, until our company pushed out office 2003. Now you get the file download box, but when you press open, the page flickers, the download box goes away, but you never get the document. I tried inserting the "add header" line, but then I get 2 file download boxes, one right after the other, then the excel does open in a new browser. Anyone have any ideas on what changed? I don't think my customer's what to press open twice. Thanks
Darrell wrote re: Export a DataGrid to Excel
on 12-01-2004 9:10 AM
Lori - Check out Carlos' Excel Xml Writer utility. Office 2003 works much better through Excel. The hack above was to make sure it worked on down-level Excel versions.

http://www.carlosag.net/Tools/ExcelXmlWriter/Default.aspx
Chris Cox wrote re: Export a DataGrid to Excel
on 12-01-2004 1:34 PM
Response.AddHeader("content-disposition","attachment; filename=divreinvestment.xls")
Opens in new excel window.
Thanks BEN I've looking everywhere for that.
Louise wrote re: Export a DataGrid to Excel
on 12-06-2004 10:08 PM
I have exported a datagrid to excel, but some of the fields themselves contain new lines (\r\n). When this is exported the data which should appear in one cell breaks over a number of lines. Is there some character I can replace the \r\n with, which will cause excel to take a new line within the same cell (Equivalent of Alt + Enter).
BlackCatBone wrote re: Export a DataGrid to Excel
on 12-21-2004 6:45 AM
Darrell,

I've succeeded in exporting my DataGrid contents to Microsoft Excel by adding this line to the export logic:
Response.AddHeader("content-disposition","attachment; filename=divreinvestment.xls")

However, I would like to display the exported data in a second IE window that I open when the user clicks the Export button. If I omit the line I referenced above, the exported data overlay the page the user was on when they clicked the "Export" button. In other words, there is no second window. The exported data just wipe out the page containing the Export button.

I think you mentioned a way to open a second IE (not Excel) window in one of your earlier posts when you stated:

"To open it in a new window simply direct the form at the top of the report to
target = "_blank" "

My problem is I don't know how to do what you are suggesting. Could you supply a code fragment that shows how and where you assign a value to "target"?

Thanks for your help.

BlackCatBone


Darrell wrote re: Export a DataGrid to Excel
on 12-21-2004 7:44 AM
Did you put the export to excel code in a button? If so that should open up Excel in its own window, leaving the browser window unchanged.
BlackCatBone wrote re: Export a DataGrid to Excel
on 12-21-2004 9:23 AM
Darrell,

Yes, the code is in a button. And yes, Microsoft Excel opens in a separate window and displays the exported data.

But I don't think I made my question clear. I'm not trying to open Microsoft Excel. I want to display the exported data inside a second instance of the IE web browser. Meaning that when the user clicks the Export button, IE would pop open another window that hosted the Excel data.

Why am I trying to do this? My customer doesn't like the "open or save" prompt that is always displayed before the data are made visible. I believe that if I can open the exported data in a separate IE window, my customer will not get this prompt.

Thanks for any help you can provide.

BCB
Darrell wrote re: Export a DataGrid to Excel
on 12-21-2004 2:54 PM
I'm not really sure. Try the code sample on this page to see if it works the way you want it to:
http://www.eggheadcafe.com/articles/20021220.asp
Sravanth wrote re: Export a DataGrid to Excel
on 12-23-2004 9:53 PM
Does anyone know how to change the sheet name produced so that it doesn't just default to the actual name of the page?
The problem im facing is that the name of the excel file is "assignmentList.xls" but the Sheet gets named to ".xls]AssignmentList". Any clues.

Thanks in Advance,
Sravanth
BlackCatBone wrote re: Export a DataGrid to Excel
on 12-29-2004 2:57 AM
Hello,

Does anyone know how to set the charset of the Response object so that the unicode character set is used when exporting a datagrid to Excel? All the examples I have seen include the equivalnet of this C# line:

Response.Charset = "";

I am using the Northwind database and international characters (such as two dots over the letter O) are scrambled in Excel. I tried this line but it did not work:

Response.Charset = "utf-8";

Can anyone help with this?

Thanks.

BCB
Akram Javed Kayani wrote re: Export a DataGrid to Excel
on 01-15-2005 7:25 PM
Hi,
Im haveing a little issue with the formatting of coulms after the export is complete. For example I have a coulm that contains text, but after it is exported into excel if a certain cell in the column only contained digits like 1234 excel leaves it as it is but if I have a longer string of digits like 12345678809 excel converst it into something like 1234Exx or something. Same is with some dates values like if i write 1: 21-12-2003 it would convert it into 1-1-2003 1:21AM where "1:" was only intended as a line number kind of thing. How can i prevent excel from formating the data on its own.
Tony Ngo wrote re: Export a DataGrid to Excel
on 01-17-2005 1:34 AM
You can check this out.
Tony Ngo wrote re: Export a DataGrid to Excel
on 01-17-2005 1:45 AM
Sorry for not showing the link but here it is again.
http://weblogs.asp.net/datagridgirl/archive/2003/04/14/5631.aspx
Buddy2005 wrote re: Export a DataGrid to Excel
on 02-25-2005 1:18 AM
Hi All

Can anyone help me in solving my problem. I am battling to make it right. Problem is --

When I click on "Export to Excel" button on my web page - it opens a new window b/c I have addded the javascript code as window.open(strURL). On this new browser it opens the excel workseet with the same contents I need. But the problem is that the name of the worksheet is picked up from "URL".

Please find a fraction of code :-
Response.Clear()
Response.Buffer = True
Response.Charset = String.Empty
Me.EnableViewState = False
Response.AddHeader("Content-Disposition", "inline;filename=MyReport.xls")
Response.ContentType = "application/vnd.ms-excel"

If I open this web application on web server then the name of worksheet and window name is "MyReport.xls".

But if open the web application from any other client machine then the window and worksheet name is picked up from the URL instead of "MyReport.xls".

I have no clue, how to resolve this. I have tried a lot of things. But doesn't work.

Can any one help me out please.....

Buddy2005 wrote Export To Excel - Response.AddHeader doesn't work from client machine
on 02-25-2005 1:18 AM
Hi All

Can anyone help me in solving my problem. I am battling to make it right. Problem is --

When I click on "Export to Excel" button on my web page - it opens a new window b/c I have addded the javascript code as window.open(strURL). On this new browser it opens the excel workseet with the same contents I need. But the problem is that the name of the worksheet is picked up from "URL".

Please find a fraction of code :-
Response.Clear()
Response.Buffer = True
Response.Charset = String.Empty
Me.EnableViewState = False
Response.AddHeader("Content-Disposition", "inline;filename=MyReport.xls")
Response.ContentType = "application/vnd.ms-excel"

If I open this web application on web server then the name of worksheet and window name is "MyReport.xls".

But if open the web application from any other client machine then the window and worksheet name is picked up from the URL instead of "MyReport.xls".

I have no clue, how to resolve this. I have tried a lot of things. But doesn't work.

Can any one help me out please.....

Carlos M wrote re: Export a DataGrid to Excel
on 03-08-2005 4:23 PM
I am trying to Export DataGrids to Excel. Here is my problem:

The HTMLTextWriter passes the HTML tables into Excel. This causes problems with large data sets. For example, I have about 16K rows (7 columns). After downloading it to the client, the .xls file takes about 5 min to open on a relatively fast computer.

Any ideas? DataGrid would be the preferred method when exporting. Thanks in advance.
Darrell wrote re: Export a DataGrid to Excel
on 03-09-2005 5:45 AM
Use CarlosAg's Excel XML Writer library:
http://www.carlosag.net/Tools/ExcelXmlWriter/Default.aspx
kimptonr wrote re: Export a DataGrid to Excel
on 03-15-2005 3:42 AM
I have a similar problem I'm trying to download 11k rows and I get a blank screen and nothing returns. Any ideas? Thanks
Sam wrote re: Export a DataGrid to Excel
on 03-15-2005 2:09 PM
I have used :
Response.AddHeader("content-disposition", "attachment; filename=myfile.xls")
This opens the file download dialog box with open and save options I have expected. The save button works great, the problem is when I click, Open, excel application opens and then an eror message is displayed something in these lines "the file you are looking for is not found in documents and settings/<user>/Local Settings/Temporary Internet Files/Contents.IE5...."
And also I oberved that, if I keep the excel application open when I click on the button, the Open button on file download works fine. It opens the excel file.
Can anyone help me solve the mistery??
Sam wrote re: Export a DataGrid to Excel
on 03-15-2005 2:45 PM
I figured the problem of my question. I have a user control which has the button that triggers the whole datagrid to excel export.
I used Response object and neglected that I have to point the response object of the parent page in which the user control resides.
I then went and added:

MyBase.Response, and boom, it works now!
kimptonr wrote re: Export a DataGrid to Excel
on 03-16-2005 1:42 AM
Hi Sam, My problem is that the dialog box doesn't open! Any ideas? Here is my code
Response.Clear()
Response.ClearContent()
Response.ClearHeaders()
Response.AppendHeader("Content-Disposition", "attachment;filename=stockmovementbreakdown.xls")

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

Response.Charset = String.Empty

Dim tw As New System.IO.StringWriter
Dim hw As New System.Web.UI.HtmlTextWriter(tw)

' Get the HTML for the control.
ordermovementgrid.RenderControl(hw)
' Write the HTML back to the browser.
Response.Write(tw.ToString())
' End the response.
Response.End()
Sam wrote re: Export a DataGrid to Excel
on 03-16-2005 9:14 AM
Kimptonr,

I am not sure if this could be a problem but try, Response.AddHeader instaed of Response.AppendHeader.
Sam wrote re: Export a DataGrid to Excel
on 03-16-2005 9:15 AM
Hello,
I still have the same problem. It looked for a while that it got solved, but nope. I still have the same problem as mentioned below. But one thing I have observed is, it works great in a mozilla browser, or firefox. Any ideas??

"I have used :
Response.AddHeader("content-disposition", "attachment; filename=myfile.xls")
This opens the file download dialog box with open and save options I have expected. The save button works great, the problem is when I click, Open, excel application opens and then an eror message is displayed something in these lines "the file you are looking for is not found in documents and settings/<user>/Local Settings/Temporary Internet Files/Contents.IE5...."
And also I oberved that, if I keep the excel application open when I click on the button, the Open button on file download works fine. It opens the excel file.
Can anyone help me solve the mistery??"
kimptonr wrote re: Export a DataGrid to Excel
on 03-16-2005 9:26 AM
Hi Sam tryed changing to addheader but still no good. I know the code works as I can download the file with a smaller date range but when I try for the full 6k lines I get no dialogue box. Just a blank aspx page