DataGridView数据导出到Excel或者Word
谁能给个Vb.net下DataGridView数据导出到Excel或者Word的例子?
[解决办法]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim dsEmployeeContract As DataSet = New DataSet
dsEmployeeContract.Clear()
dsEmployeeContract = ds
If dsEmployeeContract Is Nothing Then
MsgBox("没有数据,无法导出!", MsgBoxStyle.OKOnly)
Exit Sub
Else
Dim xlsapp As New Excel.Application
xlsapp.Workbooks.Add()
xlsapp.Visible = True
xlsapp.Range(xlsapp.Cells(1, 1), xlsapp.Cells(1, dsEmployeeContract.Tables(0).Columns.Count)).Select()
xlsapp.Selection.Merge()
xlsapp.Range(xlsapp.Cells(1, 1), xlsapp.Cells(1, 1)).Font.Size = 15
xlsapp.Range(xlsapp.Cells(1, 1), xlsapp.Cells(1, dsEmployeeContract.Tables(0).Columns.Count)).Font.Bold = True
Dim i As Int16
For i = 1 To dsEmployeeContract.Tables(0).Columns.Count
xlsapp.Cells(2, i) = dsEmployeeContract.Tables(0).Columns(i - 1).ColumnName
Next
Dim rowindex As Integer = 3
Dim colindex As Integer
Dim col As DataColumn
Dim row As DataRow
Dim nxh As Integer = 1
For Each row In dsEmployeeContract.Tables(0).Rows
colindex = 1
For Each col In dsEmployeeContract.Tables(0).Columns
If colindex = 1 Then
xlsapp.Cells(rowindex, colindex) = RTrim(Convert.ToString(row(col.ColumnName)))
Else
xlsapp.Cells(rowindex, colindex) = RTrim(Convert.ToString(row(col.ColumnName)))
End If
colindex += 1
Next
rowindex += 1
nxh += 1
Next
xlsapp.Columns.AutoFit()
End If
Catch ex As Exception
MsgBox(Err.Description, MsgBoxStyle.Critical)
End Try
End Sub
ds为Public ds As DataSet = New DataSet,是你DataGridView数据
[解决办法]
Public QueryTable As DataTable
Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
Dim xlApp As Excel.Application '定义EXCEL类
Dim xlBook As Excel.Workbook '定义工件簿类
Dim xlsheet As Excel.Worksheet '定义工作表类
Dim Fieldlen
Dim Irow As Integer = 0 : Dim Icol As Integer = 0
Dim IrowCount As Integer = 0 : Dim IcolCount As Integer = 0
Dim ExcelName As String
xlApp = CreateObject("Excel.Application") '创建EXCEL应用类
xlBook = xlApp.Workbooks.Add 'Open("H:\shock\sales\temp\bb.xls") '打开EXCEL工作簿
xlsheet = xlBook.Worksheets(1) '打开EXCEL工作表
IrowCount = QueryTable.Rows.Count '总行数
IcolCount = QueryTable.Columns.Count '总列数
ReDim Fieldlen(IcolCount)
ExcelName = Trim(labBT.Text)
xlApp.Caption = ExcelName & "表"
xlApp.Cells(1, 2) = ExcelName & "表"
xlApp.Range("B1:G2").MergeCells = True '合并单元格
Dim Col As DataColumn
Dim Row As DataRow
For Each Col In QueryTable.Columns
Icol = Icol + 1
xlApp.Cells(4, Icol + 1) = Col.ColumnName
Next
'得到的表所有行,赋值给单元格
For Each Row In QueryTable.Rows
Irow = Irow + 1
Icol = 0
For Each Col In QueryTable.Columns
Icol = Icol + 1
xlApp.Cells(Irow + 4, Icol + 1) = "'" & Row(Col.ColumnName)
Next
Next
With xlsheet
.Columns.AutoFit() '自动调整EXCEL行列宽度
.Range(.Cells(1, 2), .Cells(2, IcolCount + 1)).Font.Name = "黑体" '字型
.Range(.Cells(1, 2), .Cells(2, IcolCount + 1)).Font.Size = 15 '字体大小
'字段标题设置
.Range(.Cells(4, 2), .Cells(4, IcolCount + 1)).Font.Name = "黑体" '字型
.Range(.Cells(4, 2), .Cells(4, IcolCount + 1)).Font.Size = 13 '字体大小
'记录内容设置
.Range(.Cells(5, 2), .Cells(IrowCount + 4, IcolCount + 1)).Font.Name = "宋体" '字型
.Range(.Cells(5, 2), .Cells(IrowCount + 4, IcolCount + 1)).Font.Size = 12 '字体大小
.Range(.Cells(5, 2), .Cells(IrowCount + 4, IcolCount + 1)).Font.Bold = False '粗体
.Range(.Cells(5, 2), .Cells(IrowCount + 4, IcolCount + 1)).Font.Italic = False '斜体
.Range(.Cells(5, 2), .Cells(IrowCount + 4, IcolCount + 1)).Font.ColorIndex = 1 '字体颜色
'边框设置
.Range(.Cells(4, 2), .Cells(IrowCount + 4, IcolCount + 1)).Borders.LineStyle = Excel.XlLineStyle.xlContinuous '边框线型
.Range(.Cells(4, 2), .Cells(IrowCount + 4, IcolCount + 1)).Borders.Weight = Excel.XlBorderWeight.xlThin '边框粗细
.Range(.Cells(4, 2), .Cells(IrowCount + 4, IcolCount + 1)).Borders.ColorIndex = 1 '边框颜色
'居中
.Rows.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter '水平居中
.Rows.VerticalAlignment = Excel.XlHAlign.xlHAlignCenter '垂直居中
End With
xlApp.Visible = True '设置EXCEL可见
xlsheet.Activate() '激活工作表
xlBook.RunAutoMacros(Excel.XlRunAutoMacro.xlAutoOpen) '运行EXCEL中的启动宏
xlsheet = Nothing
xlBook = Nothing
xlApp = Nothing '交还控制给Excel
End Sub