一个将DataGridView数据导出到EXCEL工作表的方法
(第一次写博文,不知道效果)
Private Sub dcxls(byval DGV as DataGridView)
If DGV.Rows.Count > 0 Then
Dim xh, xh2 As Integer
Dim xl As New Microsoft.Office.Interop.Excel.Application
Dim bk As Microsoft.Office.Interop.Excel.Workbook = xl.Workbooks.Add
Dim sht As Microsoft.Office.Interop.Excel.Worksheet = bk.Worksheets.Add
xl.ScreenUpdating = False
xl.Calculation = Microsoft.Office.Interop.Excel.XlCalculation.xlCalculationManual
For xh2 = 0 To DGV.ColumnCount - 1
sht.Cells(1, xh2 + 1) = DGV.Columns(xh2).HeaderText
Next
For xh = 0 To DGV.RowCount - 1
For xh2 = 0 To DGV.ColumnCount - 1
sht.Cells(xh + 2, xh2 + 1).NumberFormatLocal = "@"
sht.Cells(xh + 2, xh2 + 1) = DGV.Rows(xh).Cells(xh2).Value.ToString.Trim
Next
Me.BackgroundWorker1.ReportProgress((xh + 1) / DGV.RowCount * 100)
Next
sht.Range(sht.Cells(1, 1), sht.Cells(xh, xh2)).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlDiagonalDown).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone
sht.Range(sht.Cells(1, 1), sht.Cells(xh, xh2)).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlDiagonalUp).LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlLineStyleNone
With sht.Range(sht.Cells(1, 1), sht.Cells(xh, xh2)).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft)
.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin
End With
With sht.Range(sht.Cells(1, 1), sht.Cells(xh, xh2)).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop)
.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin
End With
With sht.Range(sht.Cells(1, 1), sht.Cells(xh, xh2)).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom)
.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin
End With
With sht.Range(sht.Cells(1, 1), sht.Cells(xh, xh2)).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight)
.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin
End With
With sht.Range(sht.Cells(1, 1), sht.Cells(xh, xh2)).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical)
.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin
End With
With sht.Range(sht.Cells(1, 1), sht.Cells(xh, xh2)).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal)
.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin
End With
sht.Cells.EntireColumn.AutoFit()
sht.Cells.EntireRow.AutoFit()
xl.Visible = True
xl.ScreenUpdating = True
xl.Calculation = Microsoft.Office.Interop.Excel.XlCalculation.xlCalculationAutomatic
bk.SaveAs(My.Application.Info.DirectoryPath & "\" & Format(Now, "yyyyMMddHHmmss") & ".XLSX", FileFormat:=Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook, CreateBackup:=False)
bk.Close()
xl.Quit()
xl = Nothing
End If
End Sub