473,386 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Exporting to Excel in a VB Windows App

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 DataGridViewAutoFilter 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 2094
tlhintoq
3,525 Expert 2GB
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
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...
2
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
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...
2
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...
1
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...
3
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...
2
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...
2
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...
0
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)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.