473,770 Members | 6,158 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exporting to Excel in a VB Windows App

2 New Member
Hello All,

I have a windows application that exports data from a datagridview into excel. Now this data can change based on what a user does in the datagridview itself. An example would be out of 1,000 files the user can sort through using DataGridViewAut oFilter to search for a handful of rows, say 50 or 143 or whatever. The user then clicks on the button which exports to the excel worksheet. I'm looking for some help on getting a subtotal count at the bottom of the export. I would like to be able to have some additional code in place that would subtotal the records that the user exports into the worksheet. Let me show you what I have so far:



Expand|Select|Wrap|Line Numbers
  1. Private Sub ExportToExcelToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportToExcelToolStripMenuItem.Click
  2.         Dim rowsTotal, colsTotal As Short
  3.         Dim I, j, iC As Short
  4.  
  5.         System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
  6.         Dim xlApp As New Excel.Application
  7.  
  8.         Try
  9.             Dim excelBook As Excel.Workbook = xlApp.Workbooks.Add
  10.             Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)
  11.             'here
  12.             Dim sY As String
  13.             Dim iX As Integer
  14.             Dim iY As Integer
  15.             'here
  16.             'more here
  17.             Dim TheRg As Excel.Range
  18.             Dim oRow As Excel.Range
  19.  
  20.             'more here
  21.  
  22.             xlApp.Visible = True
  23.             rowsTotal = DataGridView1.RowCount - 0
  24.             colsTotal = DataGridView1.Columns.Count - 1
  25.             For iX = 0 To DataGridView1.Rows.Count - 2
  26.  
  27.             Next
  28.  
  29.             For iY = 0 To DataGridView1.Columns.Count - 2
  30.  
  31.             Next
  32.             With excelWorksheet
  33.                 .Cells.Select()
  34.                 .Cells.Delete()
  35.                 For iC = 0 To colsTotal
  36.                     .Cells(1, iC + 1).Value = DataGridView1.Columns(iC).HeaderText
  37.                 Next
  38.                 For I = 0 To rowsTotal - 1
  39.                     For j = 0 To colsTotal - 0
  40.                         .Cells(I + 2, j + 1).value = DataGridView1.Rows(I).Cells(j).Value
  41.                     Next j
  42.                 Next I
  43.                 .Rows("1:1").Font.FontStyle = "Bold"
  44.                 .Rows("1:1").Interior.colorindex = 37
  45.                 .Rows("1:1").Font.Size = 11
  46.  
  47.                 'test
  48.                 '.Rows("2:100").Interior.colorindex = 40
  49.  
  50.                 TheRg = .Rows("2:500")
  51.                 For Each oRow In TheRg
  52.                     If (oRow.Row / 2) = Int(oRow.Row / 2) Then
  53.                         With .Rows(oRow.Row).Interior
  54.                             .ColorIndex = 40
  55.  
  56.                         End With
  57.                     End If
  58.                 Next
  59.  
  60.  
  61.  
  62.  
  63.  
  64.                 'test
  65.  
  66.                 .Cells.Columns.AutoFit()
  67.                 .Cells.Select()
  68.                 .Cells.EntireColumn.AutoFit()
  69.                 .Cells(1, 1).Select()
  70.                 sY = DataGridView1(iY, iX).Value.ToString & ","
  71.             End With
  72.  
  73.         Catch ex As Exception
  74.             MsgBox("Export Excel Error " & ex.Message)
  75.         Finally
  76.             'RELEASE ALLOACTED RESOURCES
  77.             System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
  78.             xlApp = Nothing
  79.  
  80.             'codes starts here
  81.         End Try
  82.     End Sub
Oct 20 '09 #1
1 2117
tlhintoq
3,525 Recognized Expert Specialist
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Oct 20 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
9248
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.
2
8013
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
2
1778
by: Michael | last post by:
I need to be able to export data from a web form into MS Excel. I know you can do it with a VB.net windows application, but how can you do it with an asp.net application using vb.net? Any help would be appreciated! Michael
2
6930
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
3169
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
3
3761
by: | last post by:
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.
2
2415
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
3184
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...
0
1153
by: Dan | last post by:
Hi, i have some problem with exporting data from sql server to some formats. Excel and Word are not a problem, but .txt and .csv do not work properly: - with .txt, the data are shown (correctly) in the browser but the windows asking to save or to open the file never appears. - with .csv, the code below produces an identical excel file as with.xls (no comma). Here an extract of the code:
0
9592
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
9425
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
10231
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
10059
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
9871
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
7416
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
6679
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
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
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

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.