一个将DataGridView数据导出到EXCEL工作表的方法

一个将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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值