473,805 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exporting to Excel

Hello,

I have a routine in my Windows application that exports the contents of a
datagrid to Excel. It is modeled closely after the HowTo example on MSDN:
http://tinyurl.com/5g2jm.

Depending on the number of rows/columns in the datagrid, it can be extremely
slow (several minutes to populate a 1000x50 spreadsheet). That is
understandable, looking at the number of steps to copy over just one row.

I did notice that when you physically highlight the entire datagrid and
press Ctrl+C to copy it to the Clipboard and then manually paste that into
Excel, it is nearly instantaneous. How can I duplicate that Copy/Paste
operation? In what data format should I copy to the Clipboard for Excel to
recognize it?

Thank you

Nov 20 '05 #1
3 3764
<an*******@disc ussions.microso ft.com> wrote in
news:e0******** *****@TK2MSFTNG P10.phx.gbl:
I did notice that when you physically highlight the entire datagrid
and press Ctrl+C to copy it to the Clipboard and then manually paste
that into Excel, it is nearly instantaneous. How can I duplicate that
Copy/Paste operation? In what data format should I copy to the
Clipboard for Excel to recognize it?


If you tab delimit the data, Copy and Paste will recognize it in Excel.

--
Lucas Tam (RE********@rog ers.com)
Please delete "REMOVE" from the e-mail address when replying.
http://members.ebay.com/aboutme/coolspot18/
Nov 20 '05 #2
You could also try using ADO and the jet driver to write to an excel file. The driver is quite buggy, but should be easy just writing into a file.

Or just write into a .csv file and excel should be able to open it.

--
Rgds,
Anand
VB.NET MVP
http://www.dotnetindia.com
"an*******@disc ussions.microso ft.com" wrote:
Hello,

I have a routine in my Windows application that exports the contents of a
datagrid to Excel. It is modeled closely after the HowTo example on MSDN:
http://tinyurl.com/5g2jm.

Depending on the number of rows/columns in the datagrid, it can be extremely
slow (several minutes to populate a 1000x50 spreadsheet). That is
understandable, looking at the number of steps to copy over just one row.

I did notice that when you physically highlight the entire datagrid and
press Ctrl+C to copy it to the Clipboard and then manually paste that into
Excel, it is nearly instantaneous. How can I duplicate that Copy/Paste
operation? In what data format should I copy to the Clipboard for Excel to
recognize it?

Thank you

Nov 20 '05 #3
Thank you for your replies, Lucus and Anand.

I finally got a chance this morning to try your suggestions. I had problems
with Excel recognizing the comma-delimitted data...most likely something I'm
messing up. However, I tried tab-delimitted and it worked perfectly the
first time! This is very exciting (geesh, I'm a nerd), as it reduces the
worst-case export from 30 minutes to about 3 seconds!

Below is the code to do this. (It assumes a reference to the Microsoft
Excel 10.0 Object Library, but you can use other versions with very little
modification.)

Thanks again; your tips helped tremendously!

Eric

'\\\\
Private Sub ExportToExcel(B yRef tbl As DataTable)
' This routine copies the contents of a data table, named "dt"
(declared
' Private within this Public class), to the Windows Clipboard in a
tab-
' delimitted format. It then creates an Excel spreadsheet and
pastes the
' contents of the Clipboard to the spreadsheet.

Dim sb As New StringBuilder
Dim row, col As Integer

' Add the title.
sb.Append(tbl.T ableName & vbNewLine & vbNewLine)

' Add column headers.
For col = 0 To tbl.Columns.Cou nt - 1
If Not IsDBNull(tbl.Co lumns.Item(col) .ColumnName) Then
sb.Append(tbl.C olumns.Item(col ).ColumnName)
End If
sb.Append(vbTab )
Next
sb.Append(vbNew Line)

' Add rows.
For row = 0 To tbl.Rows.Count - 1
For col = 0 To tbl.Columns.Cou nt - 1
If Not IsDBNull(tbl.Ro ws(row)(col)) Then
If TypeOf tbl.Rows(row)(c ol) Is DateTime Then
Dim d As DateTime = tbl.Rows(row)(c ol)
sb.Append(d.ToS hortDateString)
Else
sb.Append(tbl.R ows(row)(col))
End If
End If
sb.Append(vbTab )
Next
sb.Append(vbNew Line)
Next

' Copy tab-delmitted data to Clipboard.
Clipboard.SetDa taObject(sb.ToS tring)

'MessageBox.Sho w("Table copied to Clipboard.")

' Create Excel Objects
Dim ExcelApp As Excel.Applicati on
Dim Book As Excel.Workbook
Dim Sheet As Excel.Worksheet
Dim Range As Excel.Range

' Start Excel and get Application object:
ExcelApp = CreateObject("E xcel.Applicatio n")
ExcelApp.Visibl e = True

' Add a new workbook
Book = ExcelApp.Workbo oks.Add
Sheet = Book.ActiveShee t
Sheet.Name = "Orders"

' Paste the Clipboard contents.
Sheet.Paste()

' Format column headers.
Range = Sheet.Rows(3)
Range.Font.Bold = True

' AutoFit Columns
Range = Sheet.Range("A1 ", "IA1")
Range.EntireCol umn.AutoFit()

' Format title.
Range = Sheet.Cells(1, 1)
Range.Font.Bold = True
Range.Font.Size = 14

' Add date/time stamp.
Range = Sheet.Cells(2, tbl.Columns.Cou nt)
Range.Value = "Report Created: " & Now.ToString
Range.Font.Size = 8
Range.Horizonta lAlignment = Excel.XlHAlign. xlHAlignRight

' Center title across selection.
Dim cellStart As Excel.Range = ExcelApp.Range( "A1")
Dim cellEnd As Excel.Range = _
DirectCast(Shee t.Cells(1, _
tbl.Columns.Cou nt), Excel.Range)
Dim rng As Excel.Range = _
ExcelApp.Range( cellStart, cellEnd)
rng.Merge()
rng.HorizontalA lignment = _
Excel.XlHAlign. xlHAlignCenterA crossSelection
End Sub
'////


<an*******@disc ussions.microso ft.com> wrote in message
news:e0******** *****@TK2MSFTNG P10.phx.gbl...
Hello,

I have a routine in my Windows application that exports the contents of a
datagrid to Excel. It is modeled closely after the HowTo example on MSDN:
http://tinyurl.com/5g2jm.

Depending on the number of rows/columns in the datagrid, it can be extremely slow (several minutes to populate a 1000x50 spreadsheet). That is
understandable, looking at the number of steps to copy over just one row.

I did notice that when you physically highlight the entire datagrid and
press Ctrl+C to copy it to the Clipboard and then manually paste that into
Excel, it is nearly instantaneous. How can I duplicate that Copy/Paste
operation? In what data format should I copy to the Clipboard for Excel to recognize it?

Thank you

Nov 20 '05 #4

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

Similar topics

3
9251
by: sridevi | last post by:
Hello How to export data from ms-access database to excel worksheet using ASP. mainly i need to export data to multiple worksheets. it is very urgent to us. i have a sample code which works only exporting to single worksheet. but i need to export data to multiple worksheets. it is very urgent to us. so please help me in code.
4
3951
by: D | last post by:
I've created a report with many subreports of aggregate data. I want my client to be able to export this data to Excel to make her charts, etc. Only one problem: one of the fields is a "SchoolYear" TEXT field that contains data such as 2000/01, 2001/02, etc. If I export a Query with this kind of data to Excel, it gives me the text value of this field; however, when I export a Report bound to this TEXT field, Excel gives me the values 36526,...
2
7715
by: G | last post by:
When I export data from access to excel by with "export" or "Analyze with" I seem to loose parts of some fields (long text strings). Is there a way to export it all to excel? Thanks G
2
8014
by: Kenneth | last post by:
How do I remove the limitation in Access that deny me from exporting 24000 rows and 17 columns (in a query) into Excel? Kenneth
5
3104
by: Neil | last post by:
Hi I'm currently exporting my datagrid to excel, taking advantage of the fact that excel can render html, the problem is that when i click the button to export it opens in browser, I want the button to launch Excel and leave the browser as is Any help appreciate p.s. I know that this can be achieved by changing the settings on the client machine and de-selecting the 'browse in same window' for .xls extensions but this is not acceptable...
2
6933
by: Mustufa Baig | last post by:
Hi everybody, I have an ASP.NET website where clients can view their monthly billings by selecting different options. One of the option is the way they want to see the report i.e. whether they want to see it in PDF or EXCEL etc etc..... What I am trying to acheive is depending on their choice of format, I want to send the stream of that particulae selected format to the browser. I have tried couple od solutions but I couldn't able to get...
1
3173
by: Mustufa Baig | last post by:
I have an ASP.NET website where I am showing off crystal reports to users by exporting them to pdf format. Following is the code: ---------------- 1 Private Sub ExportReport() 2 Dim oStream As System.IO.MemoryStream = 3 myReport.ExportToStream( ExportFormatType.PortableDocFormat) 4 Response.Clear() 5 Response.Buffer() = True
2
2417
by: bienwell | last post by:
Hi, I have a question about exporting data from datagrid control into Excel file in ASP.NET. On my Web page, I have a linkbutton "Export data". This link will call a Sub Function to perform exporting ALL data from the datagrid control. Exporting data works fine when I show all data on the datagrid control. I'd like to shows only 30 records on the datagrid control instead of ALL data using page navigation, and perform exporting...
2
3188
by: Snozz | last post by:
The short of it: If you needed to import a CSV file of a certain structure on a regular basis(say 32 csv files, each to one a table in 32 databases), what would be your first instinct on how to set this up so as to do it reliably and minimize overhead? There are currently no constraints on the destination table. Assume the user or some configuration specifies the database name, server name, and filename+fullpath. The server is SQL...
4
2523
by: Tom | last post by:
I have a gridview on all of my web pages in my web app and they all export to excel. I have one page where the gridview is binding to a datatable that i created and only the first column is exporting to excel. How can I get the entire grid to export to excel?
0
9716
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
10356
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...
1
10361
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10103
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...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
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...
1
4316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.