重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
以下是我以前百度找的资料 希望对你有用 你读取DataGridView到DataGrid然后直接调用函数即可
创新互联专注于企业网络营销推广、网站重做改版、建安网站定制设计、自适应品牌网站建设、H5开发、商城系统网站开发、集团公司官网建设、成都外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为建安等各大城市提供网站开发制作服务。
Public Function ExportXLsD(ByVal datagrid As DataGrid) '春坦, ByVal Title As String)
'Dim Mytable As New DataTable
'Mytable = CType(datagrid.DataSource, DataTable)
If mytable Is Nothing Then
MessageBox.Show("没有记录不能导扒蠢桐出数据", "PurpleStar", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Function
End If
If mytable.Rows.Count 0 Then
Dim MyFileName As String
Dim FileName As String
With SaveFileDialog1
.AddExtension = True '如果用户忘记添加扩展名,将自动家上
.DefaultExt = "xls" '默认扩展名
.Filter = "Excel文件(*.xls)|*.xls"
.Title = "文件保存到"
If .ShowDialog = DialogResult.OK Then
FileName = .FileName
End If
End With
MyFileName = Microsoft.VisualBasic.Right(FileName, 4)
If MyFileName = "档判" Then
Exit Function
End If
If MyFileName = ".xls" Or MyFileName = ".XLS" Then
Dim FS As FileStream = New FileStream(FileName, FileMode.Create)
Dim sw As StreamWriter = New StreamWriter(FS, System.Text.Encoding.Default)
sw.WriteLine(vbTab FileName vbTab Date.Now)
Dim i, j As Integer
Dim str As String = ""
For i = 0 To mytable.Columns.Count - 1
str = mytable.Columns(i).Caption
sw.Write(str vbTab)
Next
sw.Write(vbCrLf)
For j = 0 To mytable.Rows.Count - 1
For i = 0 To mytable.Columns.Count - 1
Dim strColName, strRow As String
strRow = IIf(mytable.Rows(j).Item(i) Is DBNull.Value, "", mytable.Rows(j).Item(i))
sw.Write(strRow vbTab)
Next
sw.Write(vbLf)
Next
sw.Close()
FS.Close()
MessageBox.Show("数据导出成功!", "PurpleStar", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
Exit Function
End If
Else
MessageBox.Show("没有记录不能导出数据", "PurpleStar", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Function
Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
Dim saveExcel As SaveFileDialog
saveExcel = New SaveFileDialog
saveExcel.Filter = "Excel文件(.xls)|*.xls"
Dim filename As String
If saveExcel.ShowDialog = Windows.Forms.DialogResult.Cancel Then Exit Sub
filename = saveExcel.FileName
Dim excel As Excel.Application
excel = New Excel.Application
excel.DisplayAlerts = False
excel.Workbooks.Add(True)
excel.Visible = False
Dim i As Integer
For i = 0 To DataGridView1.Columns.Count - 1
excel.Cells(1, i + 1) = DataGridView1.Columns(i).HeaderText
Next
'设置标题
Dim j As Integer
For i = 0 To DataGridView1.Rows.Count - 1 '填充数据
For j = 0 To DataGridView1.Columns.Count - 1
excel.Cells(i + 2, j + 1) = DataGridView1(j, i).Value
Next
Next
excel.Workbooks(1).SaveCopyAs(filename) '保存
Me.Close()
End Sub
不好意思 现在才看到;Private Sub 导锋野雀出EXCEL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim xlApp, xlBook, xlSheet As Object
xlapp = CreateObject("Excel.Application")
xlbook = xlapp.Workbooks.Add
xlsheet = xlbook.Worksheets(1)
'Dim xlapp As New Excel.Application
'Dim xlbook As Excel.Workbook
'Dim xlsheet As Excel.Worksheet
Dim rowindex, colindex As Integer rowindex = 1 '行
colindex = 0 '列
'xlbook = xlapp.Workbooks.Open("c:\EXCEL.xls") '打开EXCEL文件
xlsheet = xlapp.Worksheets("sheet1") '打开sheet1那页 Dim a As New DataSet
Dim table As New System.Data.DataTable table = DataSet11.Tables("INVMATLISTA"脊拆) '填充银早表 Dim row As DataRow '定义row为表格的行
Dim col As DataColumn '定义col为表格的列 '把表格的每一列写到EXCEL去
For Each col In table.Columns
colindex = colindex + 1
xlapp.Cells(1, colindex) = col.ColumnName Next
'把表格的每一行写到EXCEL去
For Each row In table.Rows
rowindex = rowindex + 1
colindex = 0
For Each col In table.Columns
colindex = colindex + 1
xlapp.Cells(rowindex, colindex) = row(col.ColumnName)
Next
Next
xlapp.Visible = True
End Sub 前提要先引用一个Microsoft.Office.Interop.Excel.dll然后在最上面先输入Imports Microsoft.Office.Interop; 就可以了
这里有段VB6.0的,你可以参考。
注意添加引用。
Private Sub Command6_Click()
Dim i, j As Integer
Dim xlApplication As Excel.Application, xlWorkbook As Excel.Workbook, xlSheet
Dim xlApp As Excel.Application
On Error Resume Next
Set xlApplication = GetObject(, "渗运Excel.Application")
Set xlApp = CreateObject("Excel.Application")
If MsgBox("确认将文件信息导出到EXCEL中?�", vbExclamation + vbYesNo, "警告") = vbYes Then
If Err.Number 0 Then Set xlApplication = CreateObject("Excel.Application")
Set xlWorkbook = xlApplication.Workbooks.Add
Set xlSheet = xlWorkbook.ActiveSheet
xlSheet.Cells(1, 2) = lblcl.Caption
xlSheet.Range("A1:E1").MergeCells = True
xlSheet.Range("A1:E1").HorizontalAlignment = xlCenter
xlSheet.Cells(2, 2).ColumnWidth = 18
For i = 1 To DataGrid1.Columns.Count
xlSheet.Cells(2, 1) = "槐喊拍编号"
xlSheet.Cells(2, i + 1) = DataGrid1.Columns(i).Caption
For j = 0 To DataGrid1.VisibleRows - 1
xlSheet.Cells(j + 3, 1) = j + 1
xlSheet.Cells(j + 3, i + 1) = DataGrid1.Columns(i).CellText(DataGrid1.RowBookmark(j))
Next j
Next i
xlApplication.Visible = True
Set xlSheet = Nothing
Set xlWorkbook = Nothing
Set xlApplication = Nothing
'xlApp.Range("A2:L2").Columns.Interior.ColorIndex = 40
'xlApp.Range("A2:L2").Borders.LineStyle = xlContinuous
'xlApp.Visible = True
'xlApp.Range(xlSheet.Cells(2 + PartsRs.RecordCount + 1, 1), xlSheet.Cells(2 + PartsRs.RecordCount + 1, 8)).Columns.Interior.ColorIndex = 40
'xlApp.Range(xlSheet.Cells(2 + PartsRs.RecordCount + 1, 1), xlSheet.Cells(2 + PartsRs.RecordCount + 1, 8)).Borders.LineStyle = xlContinuous
Else
MsgBox "无信息可供您导出,请确铅羡认!", vbExclamation + vbOKOnly, "警告"
End If
End Sub