Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / September 2007

Tip: Looking for answers? Try searching our database.

Export to Excel.  Browser setting?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bill E. - 07 Sep 2007 10:35 GMT
I've used the following code countless times to successfully create an
excel export from a datagrid.

Protected Sub ExportToExcel(ByVal datagrid As Control)
       'Create an MS Excel Response
       Response.Clear()
       Response.Buffer = True
       Response.ContentType = "application/vnd.ms-excel"
       Response.Charset = ""

       Dim oStringWriter As System.IO.StringWriter = New
System.IO.StringWriter
       Dim oHtmlTextWriter As System.Web.UI.HtmlTextWriter = New
System.Web.UI.HtmlTextWriter(oStringWriter)

       datagrid.RenderControl(oHtmlTextWriter)
       Response.Write(oStringWriter.ToString)
       Response.End()
   End Sub

However, for some reason on one particular project I'm only getting
the first row of results
when the browser returns with the response.

If I comment out the line

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

I receive all rows.  Is there a browser directive that I'm missing?

Bill E.
Hollywood, FL
Braulio Diez - 07 Sep 2007 13:02 GMT
Hello,

 I have seen a sample on the web and the code used it's a bit different,
try the sample, it should work fine:

http://www.dotnetjohn.com/articles.aspx?articleid=231

   protected void btnExport_Click ( object sender, EventArgs e )

   {

       Response.Clear ( );

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

       Response.Charset = "";



       Response.ContentType = "application/vnd.xls";

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

       HtmlTextWriter HtmlTextWriter = new HtmlTextWriter ( StringWriter );

       gvToExport.RenderControl ( HtmlTextWriter );

       Response.Write ( StringWriter.ToString ( ) );

       Response.End ( );

   }

 To check for more resources about generating Excel from ASP .net:

http://www.tipsdotnet.com/ArticleBlog.aspx?KWID=51&Area=Excel&PageIndex=0

 

/// ------------------------------
/// Braulio Diez
///
/// http://www.tipsdotnet.com
/// ------------------------------

> I've used the following code countless times to successfully create an
> excel export from a datagrid.
[quoted text clipped - 28 lines]
> Bill E.
> Hollywood, FL
Bill E. - 07 Sep 2007 17:40 GMT
Braulio,

Thanks for the reply.  There really is no difference between the code
you posted and what I posted except the content type, which I don't
believe is correct in your case.

I discovered that the Response.End statement was the culprit.  For
whatever reason, the response was getting truncated by this, and only
when the content type was Excel.  I'll have to look at it in more
depth because this has not happened before to me.

Bill
Bill E. - 20 Sep 2007 17:50 GMT
I found the cause of the problem.  The datagrid was populated by a
DataSet based on a stored procedure.  One of the columns returned by
the stored procedure was a varchar type that contained a string
representing an XML document.  Therefore, each row contained an XML
document in that column.  I can only guess that the tags in these XML
documents were confusing the browser.  When I removed this column from
the stored procedure, everything worked normally and I received a
perfectly good Excel workbook with all data.

Bill E.
Hollywood, FL

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.