2016-05-11 11 views
0

Hallo alle Guten Morgen.Wie Sie Excel mit VB.Net anpassen

Ich habe ein Programm in VB.Net, die Daten von Mysql in die Datagridview füllen wird.

Ich habe auch eine Schaltfläche namens Export und es wird Datagridview Daten im Excel-Format wie folgt exportieren.

enter image description here

Aber meine unsere Prof. mag dieses Format. enter image description here

Wie kann ich das erreichen?

  1. Setzen Sie ein Zentrum Kopf

  2. einen .00 am Ende der Nummer eines Spaltennummer Put

  3. die letzte Zelle in einer Spalte finden und es auf den Punkt.

Ich hoffe, jemand würde mir helfen.

Hier mein Code in Export ist

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 

Antwort

0

Versuchen Sie diesen Code:

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 
+0

Hallo Sir, alles was ich sagen kann, ist Wow! Vielen Dank, ich habe einen Fehler im Code, es berechnet nicht die Zelle in Spalte J. Immer zeigt 9.00 statt der Berechnung der Grand Total, das ist nur mein Problem. Ich werde das akzeptieren. –

+0

ok Ich verstehe, Sie müssen 'indexTotal' durch' total' in dieser Zeile ändern: '.cells (rowIndex, indexTotal + 1) = total 'wie Antwort (ich habe Antwort ändern)! –

+0

Sir Alles, was ich sagen kann, ist DANKE SO VIEL, Wirklich bist du eine große Hilfe für mich T.Y.S.M bereits getan –

Verwandte Themen