Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET File I/O and Registry > How to: Export DGview to an Excel file other than cell by cell?


Reply
 
Thread Tools Display Modes
  #1  
Old 10-16-2007, 01:51 PM
Richard_U Richard_U is offline
Freshman
 
Join Date: Apr 2007
Posts: 46
Default How to: Export DGview to an Excel file other than cell by cell?


I have a function which populates an excell spreadsheet, but I am wondering if there is an easier way to do this....

Is there a way to do this line by line instead of cell by cell?

Better yet, is there a way I can drop the entire datagridview into Excel?

Sub kickitout(ByVal dg As DataGridView)
Dim dtr As DataRow

Form2.Show()
Form2.Update()
Dim strExcelFile As String = "C:\temp\Error_Report.xls"

Dim OuputObject As OutputVariables
OuputObject = New OutputVariables
Dim i As Integer

Dim SheetNumber As Integer = 1
With OuputObject.excel

For Each OuputObject.o In dg.Rows
For OuputObject.iItms = 0 To dg.ColumnCount - 1 '-3
.Sheets(SheetNumber).Cells(OuputObject.ictr, OuputObject.iItms + 1) = OuputObject.o.cells.item(OuputObject.iItms).value
Next
OuputObject.ictr += 1
If OuputObject.ictr > 65000 Then

.Worksheets.Add(After:=.Worksheets(.Worksheets.Count))

Dim a As Integer = .Worksheets.Count
OuputObject.ictr = 6
SheetNumber += 1
End If
Next
For Each dtr In dg.Rows
.Sheets(SheetNumber).rows(OuputObject.ictr).select()


Next
.Sheets(1).activate()
.Sheets(1).Range("A5:E5").Select()
With .Selection.Font
.Name = "Arial"
.Size = 14
End With
With .Selection.Interior
.ColorIndex = 34
End With
.Columns("A:I").Select()
.Range("I1").Activate()
OuputObject.ictr = .Worksheets(1).UsedRange.Rows.Count + 3
For i = 0 To dg.Columns.Count - 1
.Cells(5, i + 1) = dg.Columns(i).HeaderText
Next
.Cells(OuputObject.ictr, 1).Select()
.Selection.font.size = 14
.Cells(OuputObject.ictr, 1) = "REPORT SUMMARY"
.Cells(OuputObject.ictr + 1, 1) = "Total Instruments with Error "
.Cells(OuputObject.ictr + 1, 2) = OuputObject.sTotal
.Cells(OuputObject.ictr + 2, 1) = "Total Instruments "
.Cells(OuputObject.ictr + 2, 2) = OuputObject.Number_of_Instruments
.Cells(OuputObject.ictr + 3, 1) = "Percentage of Instruments with Error "
.Cells(OuputObject.ictr + 3, 2) = OuputObject.PercentWithError
.Range(.Cells(OuputObject.ictr + 1, 1), .Cells(OuputObject.ictr + 3, 2)).Select()
.Selection.font.size = 12
.Columns("A:I").EntireColumn.AutoFit()
.Cells(1, 1) = OuputObject.sRTS
.Range(.Cells(2, 1), .Cells(3, 1)).Select()
.Selection.font.size = 12
.Cells(2, 1) = OuputObject.sPar
.Cells(3, 1) = OuputObject.sNumOfErr & " For " & OuputObject.Inst & " With the Error Code of: " & OuputObject.sPrimary
If OuputObject.Location <> "" Then OuputObject.sHold = OuputObject.sHold & "Located in" & OuputObject.Location
OuputObject.sHold = OuputObject.sHold & " That occurred "
OuputObject.sHold = OuputObject.sHold & "between " & OuputObject.sStart & " and " & OuputObject.sEnd
If OuputObject.Member_ID <> "" Then OuputObject.sHold = OuputObject.sHold & " With the Member Name " & OuputObject.Member_ID
.Cells(4, 1) = Trim(OuputObject.sHold)
Try
.ActiveWorkbook().SaveAs(strExcelFile)
.ActiveWorkbook.Close(SaveChanges:=True)
Catch
Form2.Close()
Exit Sub
End Try

End With
Try
System.Diagnostics.Process.Start(strExcelFile)
Catch

End Try
OuputObject = Nothing
Form2.Close()

End Sub
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->