SALVAR TABELA DATAGRIDVIEW EM EXCEL
Eu uso a rotina em anexo. Feita em C#, mas roda tranquilamente em VB.net. é possÃvel ainda compilar em VC# Express e criar um DLL.
Tranquilo e funcional para exportar sem o Excel instalado.
Citação:Leoteorico Achei este código: Dim ExcelApp As Object, ExcelBook As Object:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs)
Using excelSheetToOpen As New OpenFileDialog()
Const definicao1 As String = [Ô]Data1[Ô]
excelSheetToOpen.Filter = [Ô]Excel 97- 2003 WorkBook (*.xls)| *.xls | Excel 2007 WorkBook (*.xlsx) | *.xlsx | All files (*.*)|*.*[Ô]
excelSheetToOpen.FilterIndex = 3
excelSheetToOpen.Multiselect = True
If excelSheetToOpen.ShowDialog() = DialogResult.OK Then
Dim title As String
Dim style As MsgBoxStyle
Dim response As MsgBoxResult
style = MsgBoxStyle.Information Or MsgBoxStyle.Information Or MsgBoxStyle.YesNo
title = [Ô]Importando dados[Ô]
Dim msg As String = String.Format([Ô]{0} você deseja importar os dados do arquivo selecionado?[Ô], Label1.Text)
response = MsgBox(msg, style, title)
Dim excelApplication As Excel.Application = New Excel.Application()
If response = MsgBoxResult.Yes Then
excelApplication.DisplayAlerts = False
Dim excelWrkBook As Excel.Workbook
excelApplication.Visible = False
excelWrkBook = excelApplication.Workbooks.Open(excelSheetToOpen.FileName)
DirectCast(excelWrkBook.Sheets(1), Excel.Worksheet).SaveAs(Filename:=[Ô]C:Arquivos de programas.... [Ô] & definicao1 & [Ô].csv[Ô], FileFormat:=Microsoft.Office.Interop.Excel.XlFileFormat.xlCSVWindows)
excelWrkBook.Close()
Dim connString = ConfigurationManager.ConnectionStrings([Ô]ConnectionString1[Ô]).ConnectionString
myConnection.ConnectionString = connString
myConnection.Open()
Using insertt As NpgsqlCommand = New NpgsqlCommand([Ô]set client_encoding to [ô]iso-8859-1[ô];COPY dados1 FROM [ô]C:Program Files....CCUpgradedata1.csv[ô] WITH DELIMITER [ô],[ô] CSV HEADER; [Ô], myConnection)
insertt.ExecuteNonQuery()
End Using
MsgBox([Ô]Operação realizada com sucesso ...[Ô], [Ô]Informação[Ô])
myConnection.Close()
carregar()
Else
Exit Sub
End If
End If
End Using
End Sub
Sub carregar()
Dim c As New [Ô]Nome da dll que contém o select [Ô]() [ô]instancÃa a dll
Dim ds As New DataSet [ô]define dataset
Try
ds = c.buscaDados()
If ds.Tables(0).Rows.Count > 0 Then
GridControl1.DataSource = ds.Tables(0)[ô] .................. preeenche o datagrid!
End If
Catch ex As Exception
MsgBox(ConfigurationManager.AppSettings([Ô]NothingConn[Ô]))
End Try
End Sub
Dim ExcelSheet As Object
Dim i As Integer
Dim j As Integer
[ô]create object of excel
ExcelApp = CreateObject([Ô]Excel.Application[Ô])
ExcelBook = ExcelApp.WorkBooks.Add
ExcelSheet = ExcelBook.WorkSheets(1)
With ExcelSheet
For i = 1 To Me.dgv.RowCount
.cells(i, 1) = Me.dgv.Rows(i - 1).Cells([Ô]cod[Ô]).Value
For j = 1 To dgv.Columns.Count - 1
.cells(i, j + 1) = dgv.Rows(i - 1).Cells(j).Value
Next
Next
End With
ExcelApp.Visible = True
[ô]
ExcelSheet = Nothing
ExcelBook = Nothing
ExcelApp = Nothing
Ele é bem mais simplificado e faz o mesmo efeito, e o lado bom é que não preciso chamar a Caixa de Dialogo para salvar... WCOSTA, vou dar uma estudada no código q me passou...