如何使用 VB.Net 自定义 Excel

Posted

技术标签:

【中文标题】如何使用 VB.Net 自定义 Excel【英文标题】:How to Customize Excel using VB.Net 【发布时间】:2016-09-06 06:56:08 【问题描述】:

大家早上好。

我在 VB.Net 中有一个程序,可以将 mysql 中的数据填充到 Datagridview 中。

我还有一个名为“导出”的按钮,它将像这样以 Excel 格式导出 Datagridview 数据。

但是我的教授喜欢这种格式。

我怎样才能做到这一点?

    放置一个中心标题

    在数字列的数字末尾放置一个 .00

    查找列中的最后一个单元格并求和。

我希望有人能帮助我。

这是我在导出中的代码

If DataGridView1.Rows.Count = 0 Then
            MsgBox("Nothing to Export")
        Else
            Dim ExcelApp As Object, ExcelBook As Object
            Dim ExcelSheet As Object
            Dim i As Integer
            Dim j As Integer

            ExcelApp = CreateObject("Excel.Application")
            ExcelBook = ExcelApp.WorkBooks.Add
            ExcelSheet = ExcelBook.WorkSheets(1)
            With ExcelSheet
                For Each column As DataGridViewColumn In DataGridView1.Columns
                    .cells(1, column.Index + 1) = column.HeaderText
                Next
                For i = 1 To Me.DataGridView1.RowCount
                    .cells(i + 1, 1) = Me.DataGridView1.Rows(i - 1).Cells("ItemCode").Value
                    For j = 1 To DataGridView1.Columns.Count - 1
                        .cells(i + 1, j + 1) = DataGridView1.Rows(i - 1).Cells(j).Value

                    Next


                Next
            End With
            ExcelApp.Visible = True
            ExcelSheet = Nothing
            ExcelBook = Nothing
            ExcelApp = Nothing

        End If

【问题讨论】:

【参考方案1】:

试试这个代码:

If DataGridView1.Rows.Count = 0 Then
    MsgBox("Nothing to Export")
Else
    Dim ExcelApp As Object, ExcelBook As Object
    Dim ExcelSheet As Object
    Dim i As Integer
    Dim j As Integer
    Dim rowIndex As Integer = 1
    Dim total As Double = 0
    Dim indexTotal As Integer

    ExcelApp = CreateObject("Excel.Application")
    ExcelBook = ExcelApp.WorkBooks.Add
    ExcelSheet = ExcelBook.WorkSheets(1)
    With ExcelSheet

        With .Range(.Cells(rowIndex, 1), .Cells(rowIndex, DataGridView1.Columns.Count))
            .HorizontalAlignment = Excel.Constants.xlCenter
            .VerticalAlignment = Excel.Constants.xlCenter
            .MergeCells = True
            .Value = "PURCHASE REQUISITION"
            .Font.Bold = True
        End With

        rowIndex += 2

        For Each column As DataGridViewColumn In DataGridView1.Columns
            .cells(rowIndex, column.Index + 1) = column.HeaderText
        Next

        .Range(.Cells(rowIndex, 1), .Cells(rowIndex, DataGridView1.Columns.Count)).Font.Bold = True

        rowIndex += 1

        For i = 0 To Me.DataGridView1.RowCount - 1
            .cells(rowIndex, 1) = Me.DataGridView1.Rows(i).Cells("ItemCode").Value
            For j = 1 To DataGridView1.Columns.Count - 1
                If IsNumeric(DataGridView1.Rows(i).Cells(j).Value) Then
                    .cells(rowIndex, j + 1).NumberFormat = "#,##0.00"
                    .cells(rowIndex, j + 1) = DataGridView1.Rows(i).Cells(j).Value
                Else
                    .cells(rowIndex, j + 1) = DataGridView1.Rows(i).Cells(j).Value
                End If
                'You can test also by index for example : if j = indexofTotalColumn then
                If DataGridView1.Columns(j).Name = "Total" Then
                    total += DataGridView1.Rows(i).Cells(j).Value
                    indexTotal = j
                End If
            Next
            rowIndex += 1
        Next

        .cells(rowIndex, indexTotal) = "Grand Total"
        .cells(rowIndex, indexTotal + 1).NumberFormat = "#,##0.00"
        .cells(rowIndex, indexTotal + 1) = total 
        .Range(.Cells(rowIndex, indexTotal), .Cells(rowIndex, indexTotal + 1)).Font.Bold = True

    End With
    ExcelApp.Visible = True
    ExcelSheet = Nothing
    ExcelBook = Nothing
    ExcelApp = Nothing
End If

【讨论】:

嗨,先生,我只能说哇!非常感谢,我的代码有一个错误,它不计算列 J 中的单元格。总是显示 9.00 而不是计算总计,这只是我的问题。我会接受的。 好的,我明白了,您必须在这一行将indexTotal 更改为total.cells(rowIndex, indexTotal + 1) = total 喜欢答案(我有更改答案)! 先生,我只能说非常感谢您,T.Y.S.M 已经完成,您对我帮助很大

以上是关于如何使用 VB.Net 自定义 Excel的主要内容,如果未能解决你的问题,请参考以下文章

用vb.net如何读取excel单元格内容?

如何选择使用 VB.NET 打开的 Excel 版本?

如何使用 vb.net 保存 excel 文件?

如何使用 vb.net 将 excel 文件导入 sql server

VB.Net 如何将 app.config 文件移动到自定义位置

如何使用 vb.net 以编程方式对 Excel 中的列进行排序?