473,804 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Export data to excel

Hi,
How could I export data from table or query to excel file in VB.NET?
Thanks in advance,
Tomek
Nov 20 '05 #1
4 1693
On Mon, 14 Jun 2004 11:45:42 +0200, "Tomek" <tn******@bcdpl us.pl>
wrote:
Hi,
How could I export data from table or query to excel file in VB.NET?
Thanks in advance,
Tomek


What version of Excel?
What version of VB.NET?
Nov 20 '05 #2
Hi,
VB.NET 1.1
Excel - 97, 2000 or XP.
Tomek

"Michael Gray" <fl****@newsguy .spam.com> wrote in message
news:di******** *************** *********@4ax.c om...
On Mon, 14 Jun 2004 11:45:42 +0200, "Tomek" <tn******@bcdpl us.pl>
wrote:
Hi,
How could I export data from table or query to excel file in VB.NET?
Thanks in advance,
Tomek


What version of Excel?
What version of VB.NET?

Nov 20 '05 #3
Hi Tomek,

I had need to do this so often that I wrote a function for it. It is below.
Bear in mind that this takes some time, but it's currently better that using
..xml, which is much faster but it has a couple of flaws - the biggest of
which is that a label with number characters only will appear inside excel
as, say, '833' when it should be '00833'. If you can figure that out, let
me know.

The function below allows for either .csv or .xls (.xls works best but is
slow), and you can also use column names as headers, if you wish.

HTH,

Bernie Yaeger
Public Function sqltabletocsvor xls(ByVal dt As DataTable, ByRef strpath As
String, ByVal dtype As String, ByVal includeheader As Boolean) As Integer

' signature:

' dim funcs as new imcfunctionlib. functions

' dim xint as integer

' xint = funcs.sqltablet ocsvorxls(dsman ifest.tables(0) ,mstrpath,
"csv",false )

' where mstrpath = , say, "f:\imcapps\xls files\test.xls"

sqltabletocsvor xls = 0

Dim objxl As Excel.Applicati on

Dim objwbs As Excel.Workbooks

Dim objwb As Excel.Workbook

Dim objws As Excel.Worksheet

Dim mrow As DataRow

Dim colindex As Integer

Dim rowindex As Integer

Dim col As DataColumn

Dim fi As FileInfo = New FileInfo(strpat h)

If fi.Exists = True Then

Kill(strpath)

End If

objxl = New Excel.Applicati on

'objxl.Visible = False ' i may not need to do this

objwbs = objxl.Workbooks

objwb = objwbs.Add

objws = CType(objwb.Wor ksheets(1), Excel.Worksheet )

' i many want to change this to pass in a variable to determine

' if i want to have a column name row or not

If includeheader Then

For Each col In dt.Columns

colindex += 1

objws.Cells(1, colindex) = col.ColumnName

Next

rowindex = 1

Else

rowindex = 0

End If

Dim fixedstring As String

For Each mrow In dt.Rows

rowindex += 1

colindex = 0

For Each col In dt.Columns

colindex += 1

fixedstring = mrow(col.Column Name).ToString( ).Replace(vbCrL f, "")

'objws.Cells(ro windex, colindex) = mrow(col.Column Name).ToString( )

If col.DataType.To String = "System.Str ing" Then

objws.Cells(row index, colindex) = "'" & fixedstring

Else

objws.Cells(row index, colindex) = fixedstring

End If

Next

Next

If dtype = "csv" Then

objwb.SaveAs(st rpath, xlCSV)

Else

objwb.SaveAs(st rpath)

End If

objxl.DisplayAl erts = False

objwb.Close()

objxl.DisplayAl erts = True

Marshal.Release ComObject(objws )

objxl.Quit()

Marshal.Release ComObject(objxl )

objws = Nothing

objwb = Nothing

objwbs = Nothing

objxl = Nothing

End Function

"Tomek" <tn******@bcdpl us.pl> wrote in message
news:O5******** *******@tk2msft ngp13.phx.gbl.. .
Hi,
How could I export data from table or query to excel file in VB.NET?
Thanks in advance,
Tomek

Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5041
by: Matt | last post by:
I have an ASP page that calls ASP routines that I created that execute a database query and return the results to a recordset. I then iterate through the recordset and display the data in a table. Before I iterate through the recordset I instruct the browser that the content type is Excel using the following line: (Response.ContentType = "application/vnd.ms-excel") This works fine with Excel 2003 but with older versions (I tested Excel...
6
4071
by: Elena | last post by:
I'm trying to export data to an Excel worksheet. I can export the data in the cell values perfectly. I need the code to change a header and footer for the worksheet, not for the columns. Is this possible? If so, I really need the code immediately. Thank you, Elena
4
15016
by: Jiro Hidaka | last post by:
Hello, I would like to know of a fast way to export data source data into an Excel sheet. I found a way from C# Corner(Query Tool to Excel using C# and .NET) which is a neat little way of exporting dataset data to an excel using the Excel COM object. This works fine but the problem is its pretty darn slow when exporting large
13
13259
by: Hemant Sipahimalani | last post by:
The following piece of code is being used to export HTML to excel. HttpContext.Current.Response.ContentType = "application/vnd.ms-excel" HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=ABC.xls") HttpContext.Current.Response.Write(strHTML) HttpContext.Current.Response.End() However when the user tries to save it the Default File Type is Web Page(*.htm; *.html)
1
7211
by: JawzX01 | last post by:
Hello All, First, thank you for any help you can provide. I'm trying to do a simple export to excel. I've used the classic code that is all over the internet, and of course it worked without a problem. The problem is that I want to add text above the data grid in the excel sheet. The text already exists on the web page in various panels (there is different text depending on the parameters of the datagrid
1
9780
by: smaczylo | last post by:
Hello, I've recently been asked to work with Microsoft Access, and while I feel quite comfortable with Excel, I'm at a complete loss with databases. If someone could help me with this issue I'm having I'd be most appreciative. The database is already constructed, I'm just wanting to export the data to an excel file. In short, I'm hoping to export two Tables (or queries...not sure which to use - they both seem to have the same data) in...
1
10513
by: CoolFactor | last post by:
MY CODE IS NEAR THE BOTTOM I want to export this Access query into Excel using a command button on an Access form in the following way I describe below. Below you will find the simple query I am trying to export to Excel using a command in an Access Form. RowID strFY AccountID CostElementWBS 1 2008 1 7 2 2008 1 7 I want to...
7
28910
Merlin1857
by: Merlin1857 | last post by:
Its great producing data for users to look at in your web pages and generally that is sufficient for their needs but sometimes you may want to supply your user with the data in a form they can actually do something more with. This code shows you how to display data from your database and then how to give that data to the user in the form of a useable Excel spreadsheet which they can then take away and play with themselves. The way I have shown...
3
7168
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I have a question for you. I have a .csv file which has many lines of data. Each line has many data fields which are delimited by ",". Now I need to extract part of data from this file but save it as an excel file. The data in this excel file will be imported into an Access database. The
2
6425
hemantbasva
by: hemantbasva | last post by:
Note We need to have a template on server for generating report in multiple sheet as we do not had msoffice on server moreover this require a batch job to delete excel file created by the method.... it creates 6 sheets # region Namespaces using System;
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10600
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10352
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10097
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7642
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5535
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.