Question

How to import a Dataset into a new Excel File (ASP.NET)

Asked by: saruman101

Hey whats up Experts.This is my first time trying to import data from my asp.net page to an excel file.
First and foremost the Excel file does not exist. So this file will be created upon the user clicking on a button.
2nd I want to have on my dataset to be imported into my new excel sheet.

Oh yeah and 3rd how do you guys get to import the class for excel..I have browse through EE pages seeing examples and which some of them i try, but can not complete it because I can't get the excel objects to work. Right Now i'm using Asp.net and when I go to the import system I don't see anything that has the name of excel...unless if it is under a different class name.

4th keep in mind this is my very first time exporting data to an excel file...I have never done this so please explain thoroughly and clean :)

Thanks in advance guys :)

P.S: I'm using Asp.net Under VS 2003 (main language VB.NET)

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2007-02-20 at 12:55:00ID22401998
Tags

excel

,

dataset

,

import

,

file

Topic

Programming for ASP.NET

Participating Experts
2
Points
500
Comments
23

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Asp.Net: dataset adding a row
    Hello experts, in my Asp.Net (Vb.NET) web application I want to add a row to a dataset. Private _ds As DataSet in Page_Load _ds = New DataSet _ds.ReadXml(Server.MapPath("mypackage.xml")) Dim dsPk As DataColumn() = {_ds.Tables("mypackage").Column...
  2. ListBox Dataset ASP.net
    Hi Experts, I am using ASP.net, VB.net, Sql Server 2000 for some web development. I am populating the listbox from a dataset_1, now how can I return the selected items from the listbox to the dataset(It can be some other dataset) For the listbox , I have mult...
  3. How do I update the dataset using ASP.net/VB.net
    Hello, I'm using ASP.net/VB.net I have a dataset that contains records that I want to update (all the records should be updated) Adapter.SelectCommand = new SQLCommand(mySQL, Connect) Adapter.Fill(ds_FruitList,"FruitList") How do I update the dataset ? Example......

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: samtran0331Posted on 2007-02-20 at 13:28:22ID: 18574209

this is about as easy as it gets to export a datagrid to excel:
http://www.dotnetjohn.com/articles.aspx?articleid=78

 

by: samtran0331Posted on 2007-02-20 at 13:32:41ID: 18574248

this is an example from MS:
http://support.microsoft.com/kb/319180

this is c#
http://www.eggheadcafe.com/articles/20050404.asp

but his class is so small, it converts quickly using
http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx


Imports System.Data
Imports System.IO
Imports System.Xml
Imports System.Xml.Xsl
Namespace ExcelUtil

 Public Class WorkbookEngine

   Public Shared Function CreateWorkbook(ByVal ds As DataSet) As String
     Dim xmlDataDoc As XmlDataDocument = New XmlDataDocument(ds)
     Dim xt As XslTransform = New XslTransform
     Dim reader As StreamReader = New StreamReader(GetType(WorkbookEngine).Assembly.GetManifestResourceStream(GetType(WorkbookEngine), "Excel.xsl"))
     Dim xRdr As XmlTextReader = New XmlTextReader(reader)
     xt.Load(xRdr, Nothing, Nothing)
     Dim sw As StringWriter = New StringWriter
     xt.Transform(xmlDataDoc, Nothing, sw, Nothing)
     Return sw.ToString
   End Function
 End Class
End Namespace

 

by: davidlars99Posted on 2007-02-20 at 13:40:17ID: 18574320

It is very easy. You just have to create XSLT file for transforming dataset. Once that's done you need to do the following:

1. Load you XML from the DataSet into a XmlControl like so:

Private Function GenerateXmlControl(ByVal ds As DataSet) As System.Web.UI.WebControls.Xml
      Dim xmlControl As New System.Web.UI.WebControls.Xml
      Dim xDoc As New XmlDocument
      Dim xTrans As New XslTransform
      Dim xslDoc As New XmlDocument

      xDoc.LoadXml(ds.GetXml())
      xmlControl.Document = xDoc

      xmlControl.TransformSource = "path to your xslt document"

      Return xmlControl
End Function


2. Then do the actual transformation like so:

Private Sub ConvertToExcel(ByVal xmlControl As System.Web.UI.WebControls.Xml)
      Response.Charset = ""
      Response.ContentType = "application/vnd.ms-excel"

      Dim stringWrite As New System.IO.StringWriter
      Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)


      xmlControl.RenderControl(htmlWrite)

      Response.Write("<html xmlns:x=""urn:schemas-microsoft-com:office:excel"">")
      Response.Write("<head>")
      Response.Write("<!--[if gte mso 9]><xml>")
      Response.Write("<x:ExcelWorkbook>")
      Response.Write("<x:ExcelWorksheets>")
      Response.Write("<x:ExcelWorksheet>")
      Response.Write("<x:Name>" & "" & "</x:Name>")
      Response.Write("<x:WorksheetOptions>")
      Response.Write("<x:Print>")
      Response.Write("<x:ValidPrinterInfo/>")
      Response.Write("</x:Print>")
      Response.Write("</x:WorksheetOptions>")
      Response.Write("</x:ExcelWorksheet>")
      Response.Write("</x:ExcelWorksheets>")
      Response.Write("</x:ExcelWorkbook>")
      Response.Write("</xml>")
      Response.Write("<![endif]--> ")
      Response.Write("</head>")
      Response.Write(stringWrite.ToString)
End Sub

 

by: davidlars99Posted on 2007-02-20 at 13:46:19ID: 18574381

Of course you can skip the XSLT part if you don't want to custom formatting applied to you excel document.

 

by: saruman101Posted on 2007-02-20 at 14:12:03ID: 18574578

Hey thanks for the reply guys.
Right now I'm trying your example davidlars99 and sorry to bother you with this stuff..

but in your example when you put this
//
xmlControl.TransformSource = "path to your xslt document"
//

Do you mean where will the xml file will place at?...or if there is an xml file already created?
again this is my first time doing this...so I don't know what you meant by that part of the code?

 

by: saruman101Posted on 2007-02-20 at 14:16:49ID: 18574622

also this portion of the code as well
      Response.Write("<html xmlns:x=""urn:schemas-microsoft-com:office:excel"">")
I need a bit of help understanding this portion.

 

by: saruman101Posted on 2007-02-20 at 14:24:18ID: 18574686

also I tried your code which works by the way, the only major problem is that it takes the whole entire web page and puts it into an excel sheet...so pretty much it takes my calendar,textbox and etc with it lol

Is there a way to just getting only the Grid Data into an excel sheet.

 

by: saruman101Posted on 2007-02-20 at 14:33:13ID: 18574758

Also I thought it would be better to feed the excel sheet with the dataset instead of the datagrid ?
Which way is much better to do?

 

by: davidlars99Posted on 2007-02-20 at 15:10:17ID: 18575013

You can do both DataSet and DataGrig, but I think XSLT way is much better becasue you can do so much by giving it a custom look and feel.

I'll show you tomorrow how you can do it with DataGrid...

 

by: davidlars99Posted on 2007-02-21 at 07:28:00ID: 18579057

Actually it is easier than I though...  :)

DataGrid has the same method as XmlControl because they inherit some method from the same interface - DataGrid.RenderControl(htmlWrite)

So you just bind the DataGrid with some DataSet and call - Public Sub RenderControl(ByVal htmlWrite As HtmlTextWriter)

 

by: saruman101Posted on 2007-02-21 at 07:42:57ID: 18579198

Ok I'm getting two problems.
First one when I click on my export button I get everything from my webpage imported to an excel sheet.
Including text box, Labels, and my Data from the Dataset.
I just want to import only my data not the whole entire page.

2nd If I decide to close the page right after Excel keeps running HUGE amount of process on the CPU.
I need to know how to just to import only the data and not the whole page, and how close the page appropriately in order to release things out of memory.

So far I have use this code

//////////////

Private Sub ConvertToExcel(ByVal xmlControl As System.Web.UI.WebControls.Xml)
      Response.Charset = ""
      Response.ContentType = "application/vnd.ms-excel"

      Dim stringWrite As New System.IO.StringWriter
      Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)


      xmlControl.RenderControl(htmlWrite)

      Response.Write("<html xmlns:x=""urn:schemas-microsoft-com:office:excel"">")
      Response.Write("<head>")
      Response.Write("<!--[if gte mso 9]><xml>")
      Response.Write("<x:ExcelWorkbook>")
      Response.Write("<x:ExcelWorksheets>")
      Response.Write("<x:ExcelWorksheet>")
      Response.Write("<x:Name>" & "" & "</x:Name>")
      Response.Write("<x:WorksheetOptions>")
      Response.Write("<x:Print>")
      Response.Write("<x:ValidPrinterInfo/>")
      Response.Write("</x:Print>")
      Response.Write("</x:WorksheetOptions>")
      Response.Write("</x:ExcelWorksheet>")
      Response.Write("</x:ExcelWorksheets>")
      Response.Write("</x:ExcelWorkbook>")
      Response.Write("</xml>")
      Response.Write("<![endif]--> ")
      Response.Write("</head>")
      Response.Write(stringWrite.ToString)
End Sub
////////////

It works but it takes everything on my web page with it.

 

by: davidlars99Posted on 2007-02-21 at 09:55:33ID: 18580430

Didn't I just tell you how to do this with DataGrid?

Private Sub ConvertToExcel(ByVal dg As DataGrid)
      Response.Charset = ""
      Response.ContentType = "application/vnd.ms-excel"

      Dim stringWrite As New System.IO.StringWriter
      Dim htmlWrite As New System.Web.UI.HtmlTextWriter(stringWrite)


      dg.RenderControl(htmlWrite)

      Response.Write("<html xmlns:x=""urn:schemas-microsoft-com:office:excel"">")
      Response.Write("<head>")
      Response.Write("<!--[if gte mso 9]><xml>")
      Response.Write("<x:ExcelWorkbook>")
      Response.Write("<x:ExcelWorksheets>")
      Response.Write("<x:ExcelWorksheet>")
      Response.Write("<x:Name>" & "" & "</x:Name>")
      Response.Write("<x:WorksheetOptions>")
      Response.Write("<x:Print>")
      Response.Write("<x:ValidPrinterInfo/>")
      Response.Write("</x:Print>")
      Response.Write("</x:WorksheetOptions>")
      Response.Write("</x:ExcelWorksheet>")
      Response.Write("</x:ExcelWorksheets>")
      Response.Write("</x:ExcelWorkbook>")
      Response.Write("</xml>")
      Response.Write("<![endif]--> ")
      Response.Write("</head>")
      Response.Write(stringWrite.ToString)
End Sub


> I just want to import only my data not the whole entire page.

Do it in another window. Just popup the new browser window.

 

by: saruman101Posted on 2007-02-21 at 11:06:19ID: 18581069

ok well now that I tried it the way you said, I get an error message telling me the control is not in a form.
Even though I know for sure it is within a form.
This is the error message I got.
//
Control 'grdResults__ctl2__ctl0' of type 'DataGridLinkButton' must be placed inside a form tag with runat=server
//
Again I know for sure that my grid plus the button I'm clicking on are within the Form and each one has the
runat = server

 

by: saruman101Posted on 2007-02-21 at 14:27:14ID: 18582868

//
Do it in another window. Just popup the new browser window.
//

If you were to do this what would be the best way?
So far i made a new page , but every time I pass the data over I get nothing.
I'm pretty much using the same code you gave me which passes it to excel.

But this time in the new page I have a public variable which I access from the old page, put all the data
within and then try to use the method you gave me.

So far I get no data in there....Would you think a session variable would be best? or some other way?

 

by: saruman101Posted on 2007-02-21 at 14:36:02ID: 18582929

This is what I have in page 1
 Dim SendResults As New WebFormExcelReader
            SendResults.WebFormExcelWriter = SqlDataset

Which SqlDataset has my data already

then on page 2
at the top i have this
 Public WebFormExcelWriter As DataSet

Then within page 2 I have your code running upon page load ....but when i put a few break points
I notice that the webformexcelwriter gets nothing at all..therefore it gives me a blank excel sheet.

 

by: davidlars99Posted on 2007-02-21 at 14:56:32ID: 18583054

> So far i made a new page , but every time I pass the data over I get nothing.

Try to put in session and see what you get.

 

by: saruman101Posted on 2007-02-22 at 07:18:43ID: 18588244

Good morning davidlars99. I just got this thing to work. Just like you said with a session on the 2nd page I was able to pull off the data and push it to the new page.
But I 'm having a little bit of a problem...well then again I don't know if it is a bug or not. Let say the user has 2 rows of data, it passes it with no problem..actually something small like 10 rows or 8 etc..but if it gets very large like 200 rows I get a message telling me that it can't read the file and then I see a prompt to save the file to the local drive.

Now i don't know if it does this because the buffer gets too fill up and therefore it ask me to save before it loses the data or its just a bug overall.

Is it possible to display large amount of rows to 2nd page. If it is then how many rows can you actually display on the 2nd page.

 

by: davidlars99Posted on 2007-02-22 at 07:30:31ID: 18588374

> I get a message telling me that it can't read the file and then I see a prompt to save the file...

Getting prompt to save or open the file is normal, but rest of it is not. Here something to try out - On that pop up page (.aspx), make sure there is nothing but @Page directive. Make sure there's no HTML there.

> If it is then how many rows can you actually display on the 2nd page.

When I was testing mine, I did it with over 5000 rows...

You should keep trying some tweaks until you succeed. This is how it always is. :)

 

by: davidlars99Posted on 2007-02-22 at 07:31:09ID: 18588388

Ohh... and good morning to you too.

 

by: saruman101Posted on 2007-02-22 at 09:20:11ID: 18589505

um..ok well as a noob I will ask what do you mean by @page directive.
Also if i wipe out all the html from the 2nd page what will happen the grid that I have in the 2nd page to get fill up.

 

by: davidlars99Posted on 2007-02-22 at 10:02:50ID: 18589850

> what do you mean by @page directive.

This is page directive - <%@ Page Language="vb" AutoEventWireup="false" Codebehind="MyPage.aspx.vb" Inherits="MyPage"%>

> what will happen the grid that I have in the 2nd page to get fill up...

You should not have anything else on the pop up page. It will actually be turned into an excel document once it is loaded.

 

by: saruman101Posted on 2007-02-22 at 10:51:13ID: 18590269

Awesome it works. LOL
you have no idea how this is for me . I mean for one I'm very new to asp.net so I'm still trying to get the hang
of it, plus all this binding,rebinding and bind again lol stuff .

Overall it works I fill up a page which had about roughly 50k rows with no problem what so ever.
Thanks for your time and patience davidlars99.

One question..why does it not work with all the html stuff defined?...
I would think that it would need the html in order to render the page itself.

 

by: davidlars99Posted on 2007-02-22 at 18:18:54ID: 18593288

> why does it not work with all the html stuff defined?...

You'll have to do some tricks with XSLT and you can anything u want.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...