簡體   English   中英

如何使用VB.Net自定義Excel

[英]How to Customize Excel using VB.Net

大家好,早上好。

我在VB.Net中有一個程序,它將Mysql中的數據填充到Datagridview中。

我還有一個名為Export的按鈕,它將以Excel格式導出Datagridview數據,如下所示。

在此處輸入圖片說明

但是我的教授喜歡這種格式。 在此處輸入圖片說明

我該如何實現?

  1. 放置中心標題

  2. 在數字列的數字末尾放置.00

  3. 在列中找到最后一個單元並求和。

我希望有人能幫助我。

這是我在導出中的代碼

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

試試這個代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM