Chat with a LIVE Microsoft Access Expert!
 
       
 

         

   

Go Back   Access World Forums > Apps and Windows > VB.NET

 
 
Chat with a LIVE Microsoft Access Expert!
Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 11-23-2009, 12:32 PM
El Corazon El Corazon is offline
Registered User
 
Join Date: Nov 2009
Posts: 1
El Corazon is on a distinguished road
Arrow formatting problems while exporting to Excel

Hello there,
Using vb.net, I am pulling data from SQL Server through the use of a datagridview and exporting to excel. Problem is I cannot format the columns. columns D-H are all date/time fields and I cant seem to modify anything. Here is the code I am using to export to excel. Any help would be greatly appreciated!

If DataGridView1.Rows.Count = 0 Then
MessageBox.Show("There are no records to display")
Else

Dim wapp As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application
Dim wsheet As Microsoft.Office.Interop.Excel.Worksheet
Dim wbook As Microsoft.Office.Interop.Excel.Workbook

wapp.Visible = True
wbook = wapp.Workbooks.Add()
wsheet = wbook.ActiveSheet

Dim iX AsInteger
Dim iY AsInteger
Dim iC AsInteger
For iC = 0 To DataGridView1.Columns.Count - 1
wsheet.Cells(1, iC + 1).Value = DataGridView1.Columns(iC).HeaderText
wsheet.Cells(1, iC + 1).font.bold = True
Next
wsheet.Rows(2).select()

For iX = 0 To DataGridView1.Rows.Count - 1
For iY = 0 To DataGridView1.Columns.Count - 1
wsheet.Cells(iX + 2, iY + 1).value = DataGridView1(iY, iX).Value.ToString
Next


Next



wapp.Visible = True
wapp.Columns.AutoFit()
wapp.Rows.AutoFit()
wapp.UserControl = True
EndIf
EndSub
End
Class
Reply With Quote
Sponsored Links
  #2  
Old 01-12-2010, 12:12 PM
fixo fixo is offline
Registered User
 
Join Date: Jan 2010
Posts: 2
fixo is on a distinguished road
Re: formatting problems while exporting to Excel

Quote:
Originally Posted by El Corazon View Post
Hello there,
Using vb.net, I am pulling data from SQL Server through the use of a datagridview and exporting to excel. Problem is I cannot format the columns. columns D-H are all date/time fields and I cant seem to modify anything. Here is the code I am using to export to excel. Any help would be greatly appreciated!

If DataGridView1.Rows.Count = 0 Then
MessageBox.Show("There are no records to display")
Else

Dim wapp As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application
Dim wsheet As Microsoft.Office.Interop.Excel.Worksheet
Dim wbook As Microsoft.Office.Interop.Excel.Workbook

wapp.Visible = True
wbook = wapp.Workbooks.Add()
wsheet = wbook.ActiveSheet

Dim iX AsInteger
Dim iY AsInteger
Dim iC AsInteger
For iC = 0 To DataGridView1.Columns.Count - 1
wsheet.Cells(1, iC + 1).Value = DataGridView1.Columns(iC).HeaderText
wsheet.Cells(1, iC + 1).font.bold = True
Next
wsheet.Rows(2).select()

For iX = 0 To DataGridView1.Rows.Count - 1
For iY = 0 To DataGridView1.Columns.Count - 1
wsheet.Cells(iX + 2, iY + 1).value = DataGridView1(iY, iX).Value.ToString
Next


Next



wapp.Visible = True
wapp.Columns.AutoFit()
wapp.Rows.AutoFit()
wapp.UserControl = True
EndIf
EndSub
End
Class
Try change NumberFormat property for particular cells:

Code:
 
If iY = 3 Or iY= 4  Then '<--change on columns you need
wsheet.Cells(iX + 2, iY + 1).NumberFormat ="DD.MM.YYYY" '<--change format to suit
....
End If
~'J'~
Reply With Quote
Sponsored Links
Reply

Tags
excel, format, formatting, vb.net

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Exporting Formatted Report to Excel LadyDi Reports 31 11-21-2008 02:21 AM
Exporting to Excel (Formatting Issues) xoail Reports 0 09-08-2008 08:36 AM
Excel import problems - please help! JediDG General 11 11-11-2003 03:40 AM
Exporting data to excel Mansoor Ahmad Macros 5 06-18-2003 02:48 AM
Exporting to excel lipin General 1 03-12-2003 05:43 AM


All times are GMT -8. The time now is 10:34 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
(c) copyright 2009 Access World