How to Create an Excel File from VB.Net

VWD 2008 Express.  Visual Basic.

Is it possible to build an Excel workbook from VB code behind an aspx page.  What do I need to do?  Can anyone provide a code sample?  Thanks.


Dr. Douglas Pruiett
Good News Jail & Prison Ministry
www.goodnewsjail.org
0
Chaplain
6/18/2008 4:14:35 PM
asp.net.getting-started 91979 articles. 3 followers. Follow

12 Replies
835 Views

Similar Articles

[PageSpeed] 7

Not knowing your exact needs, this code sample from ASPNet101.com shows how to export from a Gridview to an Excel file

http://www.aspnet101.com/aspnet101/aspnet/codesample.aspx?code=gvExportExcel


David Wier
MCP/ASPInsider
ASPNet101.com - where to look first!
Please Vote for ASPNet101 - 'Best Community Resource'!
Control Grouper - easily control properties for multiple controls with one control!
Calendar Express - The Best HTML Calendar Generator on the web!
(Please 'Mark as Answer' when it applies)
0
augustwind
6/18/2008 4:21:08 PM

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

0
koolprogrammer
6/18/2008 4:26:01 PM

Thanks for the reply.  What I need and want to do is to build an Excel file NOT from a GridView control, but from data I am generating programmatically.  I need to be able to create the Excel file, and populate some of its cells with data I have programmatically.  I know how to create a regular text file (>CSV) from my VB code behind, but I really need to create a true Excel file.  Any other ideas?  Thnaks.


Dr. Douglas Pruiett
Good News Jail & Prison Ministry
www.goodnewsjail.org
0
Chaplain
6/18/2008 6:50:25 PM

I am trying to work with the code example to which you refer.  It produces an Excel worksheet, but the first row is blank and then row 2 has my headings (field names top be used in a mail merge).  Why would a blank row be placed before the actual header row?  How can I get rid of it?  Thanks for any additional help.


Dr. Douglas Pruiett
Good News Jail & Prison Ministry
www.goodnewsjail.org
0
Chaplain
6/19/2008 1:06:03 PM

Hi,

There are three approaches can export to excel file, approach 2 and 3 are helpful to you.

1.One is using GridView that is bound on DataSet from database. And export GridView to Excel File. http://forums.asp.net/t/1197704/2076903.aspx.
2.Another approach is more flexible: Write an Excel file directly via the data in DataSet. http://forums.asp.net/p/1222903/2189537.aspx.

3.As to export the data to existing excel file or write data into specific cell of excel file, you can use Excel Library: http://forums.asp.net/p/1235485/2244394.aspx.

In this approach, you can define more details about the cells. For example, define the style and format of cells in excel file that is unavailable with the other two approaches.

C# and VB.NET converter: http://www.codechanger.com/


Hope it helps.

 



Vince Xu
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Vince
6/20/2008 6:06:34 AM
0
boltonss
7/15/2008 8:22:56 AM

Thanks for the link.  This deals with Visual Studio.NET.  I am using Visual Web Developer 2008 Express.  However, I went through the steps of adding the reference to the Microsoft Excel 11.0 Object Library (Website>Add Reference), then adding the following at the top of my class module:

Imports Microsoft.Office.Interop.Excel

But I get the error mesage:

Warning 1 Namespace or type specified in the Imports 'Microsoft.Office.Interop.Excel' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.

What other steps might I need to use the example at the link you gave in my VWD 2008 application?


Dr. Douglas Pruiett
Good News Jail & Prison Ministry
www.goodnewsjail.org
0
Chaplain
7/18/2008 9:27:50 PM

If you want to be able to read / write native Excel files, complete with formula's, pivot tables, etc... then you should have a look at the controls from Soft Artisans.  I've used them myself, and can highly recommend them - albeit they are a bit pricey.

Worth checking them out either way.


Cheers,
BSolveIT

http://www.bsolveit.com

If you get an answer to a question, please mark it as the answer. Many thanks!
0
BSolveIT
7/18/2008 9:44:18 PM

I needed to use the following to get the Excel 2003 library:

Imports Microsoft.Office.Interop.Owc11

and instead of:

xlApp = New Excel.ApplicationClass  (Which seems to create an Excel 2007 file)

I used:

xlApp = New Excel.Application  (Which creates an Excel 2003 file)

So the whole thing is:

Imports Excel = Microsoft.Office.Interop.Owc11
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object,_
	 ByVal e As System.EventArgs) Handles Button1.Click

        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value

        xlApp = New Excel.Application
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")
        xlWorkSheet.Cells(1, 1) = "http://vb.net-informations.com"
        xlWorkSheet.SaveAs("C:\vbexcel.xlsx")

        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

        MsgBox("Excel file created , you can find the file c:\")
    End Sub

    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub

End Class

 

Dr. Douglas Pruiett
Good News Jail & Prison Ministry
www.goodnewsjail.org
0
Chaplain
7/18/2008 9:56:44 PM

Dear BSolveIT:

The code is working well when in the Visual Web Developer 2008 Express environment (debug).  However, when I move the code to the server, it croaks on the Excel stuff I added.  I have (among other things) the followinf lines of code in the module that fails on the server (the error I get follows).  Any idea why I get the error?

Imports Excel = Microsoft.Office.Interop.Excel

 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button1.Click

        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet
        Dim misValue As Object = System.Reflection.Missing.Value

        xlApp = New Excel.Application
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")
        xlWorkSheet.Name = "test"
        xlWorkSheet.Cells(1, 1) = "Test"
        xlWorkSheet.SaveAs(MapPath("~/Extranet/Temp Files/" + Session("FullName") + " Test.xls"))

        xlWorkBook.Close()
        xlApp.Quit()

        releaseObject(xlApp)
        releaseObject(xlWorkBook)
        releaseObject(xlWorkSheet)

        MsgBox("Excel file created , you can find the file c:\")
    End Sub

    Private Sub releaseObject(ByVal obj As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
            obj = Nothing
        Catch ex As Exception
            obj = Nothing
        Finally
            GC.Collect()
        End Try
    End Sub
 
I get the following error when I run the code on the server:
 
Server Error in '/' Application.
--------------------------------------------------------------------------------

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC30002: Type 'Excel.Application' is not defined.

Source Error:

 

Line 496:    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button1.Click
Line 497:
Line 498:        Dim xlApp As Excel.Application
Line 499:        Dim xlWorkBook As Excel.Workbook
Line 500:        Dim xlWorkSheet As Excel.Worksheet
 
 
Dr. Douglas Pruiett
Good News Jail & Prison Ministry
www.goodnewsjail.org
0
Chaplain
7/19/2008 9:49:47 PM

I would have to assume that Microsoft Office isn't installed on the server?

 


Cheers,
BSolveIT

http://www.bsolveit.com

If you get an answer to a question, please mark it as the answer. Many thanks!
0
BSolveIT
7/19/2008 10:47:25 PM

For quite sometime now, i am trying to solve several issues with OWC 11. Unfortunately i still haven't found any solutions to my problem.

BSolveIT:

I would have to assume that Microsoft Office isn't installed on the server?

I have the same issue actually, and i've already installed OWC on my server but still we will be getting this error.

I was also interested on Chaplain Doug post on top, where he mention that Excel.Application is more on Office 2003 and Excel.ApplicationClass is more on Office 2007. My main reason actually trying to export all my data into a "NATIVE" excel format because everytime i export it, it is an XML based, which i cannot open on OpenOfficeCalc.

So any possible solutions, out there? TIA


Please don't forget to click "Answer" if it helps you as it may help others also.
0
jobejufranz
7/21/2008 2:08:25 AM
Reply:

Similar Artilces:

Creating an Excel file using VB.Net
Just after some help to create an Excel file using VB.Net please. I've had a bit of a hunt around but can't find an exact example of code that I'm after. I'm new to .Net and VB.Net and wanted to create and save records to an EXCEL file. I can translate from C# if need be. Thanks for any help. Hi Amelia99, welcome to the forums. Which version of Excell do you want to use? The latest version (2003) has xml possibilities and you can download a free SDK at the microsoft site that makes it quite easy to use with vs.net 2003. It makes new project templates and one of them is for ex...

how can i Get OCX File in .net IDE vb.net/C3
hi all,      How can I generate OCX file in VB.net/c# 2005 that will interact in web pages...?Thanks & Regards,Murthy. Hi murthysrn, As far as I know, VB 2005 and C# 2005 cannot create the project for ocx file. However, I hope the following link will be helpful to you. Creating an ActiveX Control in Visual Studio 2005http://msmvps.com/blogs/pauldomag/archive/2006/08/16/107758.aspxSincerely,Benson YuMicrosoft Online Community Support Please remember to mark the replies as answers if they help and unmark them if they provide no help. This can be beneficial to other co...

Need tutorial on creating Excel File using VB.NET
I am trying to create a pipe "|" delimited excel file from my VB.NET application. I did a search and didn't find anything very useful. I will create the pipe delimited text string in code. I just need to write each text string into a different row in a spreadsheet. Then create the .xls file. I have done this previously with a .txt file, but from what I have read this is different when creating a .xls file. Does anyone have any good tutorials for this? Thanks.Don't forget to click "Mark as Answer" on the post that helped you.This credits that member, earns you a po...

converting vb.net 2003 files to vb.net 2005
Hi, i am beginner..i watch videos about vb.net..they made on vs.net 2003 when i wrote these codes in 2005 ,it gives errors..is there any program which convert the code automatically?   thanks,Thanks,Speranza What videos are you talking about?  There are lots of great, up-to-date ASP.NET Videos on this site for free.Darrell Norton, MVPDarrell Norton's BlogPlease mark this post as answered if it helped you! i am talking about learnvisualstudio.net videos..Thanks,Speranza Speranza:they made on vs.net 2003 when i wrote these codes in 2005 ,it gives errors..is there a...

Sending SMS using vb.net or C#.net using vb.net or c#.net
Hi  My requirement is I hav one csv file with these fields id,mobilenum,messgae,status.intiallu staus is 0. once i read the all fileds and take that mobile number.using tat mobile number  i need to send sms .after sending sms i shuld change status as 1 How to send sms thru coding (please dont provide any links.if it is provide also please give working links becox i checked codeproject .i didnt get any nice link. and also provide the how to update the status field im csv file   Thank ssandhya   To send SMS, you need some third party SMS providers. if you consul...

How to Create .mdb file from Excel file using C#.net
Hi all I want to create .mdb file that i have created excel feel from MS jet Database... through code behind Help me in this..  Regards RaviPlease click "mark as answer" if this post helped you. How to create an mdb file programmtically:http://support.microsoft.com/kb/317881How to import Excel data into it: http://www.mikesdotnetting.com/Article.aspx?ArticleID=79 Regards Mike [MVP - ASP/ASP.NET]My site...

How to get RS (From VB6) to .NET (VB.NET)
Hi Friends,         I have requirement, there is one function in VB6 it returns a RS, I need to call that function in  VB.NET, how to get those values into .NET, if can any body can help (pass me some code snippets), that will be greate, Thank you.Madhu... -Madhu hey guys i got this one just by using fill method.-Madhu...

.Net 2.0 Web application using Vb.net is unable to create object of another dll writen in C# .net 2.0
Hi, I habe created one ASP.net web application using Vb.net which is adding reference of dlls written in C# and .net 2.0. But whenever trying to create object of referencing dll, it is throwing error :Object reference does not set to an object. But, locally it is working fine,. In the server i have deployed the .aspx files and dll files in the bin. There were already an web.config in the server which is of .net 1.1. But checked it is retrieving value for that web.config correctly. Should i have to deply any other files and if not what can be the solution for this? Please help. Thanks, So...

converting to vb.net from c#.net authorize.net
authorize.net offered me some sample code when I signed up with them the only problem is the sample code is in c#.net but my page that they type all of their credit card into is vb.net <code><%@ Import Namespace="System.Net" %> <%@ Import Namespace="System.IO" %> <script language="C#" runat="server"> void Page_Load(Object Src, EventArgs E) { myPage.Text = readHtmlPage("https://certification.authorize.net/gateway/transact.dll"); } private String readHtmlPage(string url) { ...

creating setup file in vb.net
Hi, I have created a setup file for the application which i had developed. The file is being created and when i install the same in the client, a shortcut is created in the desktop of the client. When i double click that shortcut a window is being opened with all the desktop contents. I will be thankful if anyone can give me a detailed procedure of how to create a setup file in vb.net. Thanks in advance... Cheers ChakriCheers,Chakri If you're using Visual Studio, it comes packaged with a dinky installer template. It's apart of the Project Templates when you create a new ...

vb.net how to create .DLL file
How to create .dll file some on please help me on this maxmax IN Visual Studio... File - New - Project - Class Library That's it... that compiles to a DLL Thank you for answering this question, but can you give me little demo or give me some code or may be I can create dll and use that dll in some other program thank you maxmax that's really way more then this forum is designed for. If you download any of the multi-tier starterkits you will usually see an example of this, but in general I'd suggest you start with a good book....

How to create a set up file in vb.net
How to create setup files in Visual Studio for windows applications with sqldatabase? so that I can run it on any other computer which does not having visual studio .NET Try this walkthrough:http://msdn.microsoft.com/en-us/library/k3bb4tfd.aspx Darrell Norton, MVPDarrell Norton's BlogPlease mark this post as answered if it helped you!  Hi,  Thank you for your information.....  ...

How to Print Excel File In VB.Net???
hi, I have a function for creating an excel report: PseudoCode: Loop1        Read through the dabase; Can Have several Records; Each Record is 1 file            Loop2               MyBase.BookOpen("C:\Documents and Settings\sheila\My Documents\Visual Studio 2005\Projects\excel1\Format.xls")               1 file---->can have several pages    ...

opening excel file with vb.net?
Hi, I'm trying to open an excel file using vb.net. I've found a way on the web but I'm getting this error: <script runat="server"> Sub LinkButton_Click(sender As Object, e As EventArgs) Dim xlApp As Excel.Application Dim xlWB1 As Excel.Workbook Set xlApp = New Excel.Application xlApp.Visible = True Set xlWB1 = xlApp.Workbooks.Open("test.xls") xlWB1.Close Set xlWB1 = Nothing xlApp.Quit Set xlApp = Nothing End Sub </script> Error: Excel.Application not defined. Which .net class handles the excel object?...

Web resources about - How to Create an Excel File from VB.Net - asp.net.getting-started

Wikipedia:Why create an account? - Wikipedia, the free encyclopedia
This is Wikipedia . You do not have to log in to learn. Almost anyone can edit almost any article at any given time too, but be aware that the ...

Facebook Teams Up With Google, Microsoft, Others To Create WebPlatform.org, A Developer Support Hub
Facebook may have a tricky relationship with HTML5 , but it still wants developers all over the Web to learn how to use it efficiently. The social ...

Perth man ditches day job, creates Postcode Honey hive network
He quit the mining industry to chase his dream of making ultra-local honey accessible to everyone in Perth.

Omnicom to Use P&G as Impetus to Create a Third Media Agency
Omnicom Media Group, the big winner in the P&G review, today said it's planning to launch a third agency network for which P&G will be the charter ...

You can now create and manage reminders in Google Calendar
Youve been able to create and manage your events on Google Calendar since its launch on iOS last March. But now, Google has made it easier for ...

Spotify lets you create your year in music
... and genres, top artists by season, total minutes of music streamed and total number of songs and artists played. Once your list is created ...

Verizon has hired thousands of people to create the video service it says will replace cable TV
It’s no secret that the traditional cable TV business is in trouble, as younger millennial cord-cutters don’t see a need to pay for TV service. ...

Trump’s call to ban Muslims would create H-1B, other turmoil
Early in his campaign, Republican presidential candidate Donald Trump established himself as a champion of restrictions on the H-1B visa program. ...

Can A Coupon Taste Good? Krispy Kreme Creates Barcode Of Glaze For Free Dozen Donut Coupon
... 12 free glazed dough tori from Krispy Kreme. To ensure everyone has their fair shot at what it’s calling #dayofthedozen, the company has created ...

Create royalty-free soundtracks with the click of your mouse
I just made three custom, royalty-free soundtracks using nothing more than a website and my Mac. I’ll be able to use these in any video project ...

Resources last updated: 12/10/2015 3:54:07 PM