Excel erstellen und als Tabelle formatieren

  • VB.NET

Es gibt 12 Antworten in diesem Thema. Der letzte Beitrag () ist von petaod.

    Excel erstellen und als Tabelle formatieren

    Hi,
    ich exportiere Daten aus einer DGV nach Excel. Das funktioniert auch wunderbar. Jetzt möchte ich die Excel aber noch als Tabelle formatieren. Hier scheitert leider jeglicher Versuch.

    Hier mein Code.

    VB.NET-Quellcode

    1. Dim oExcel As Object
    2. Dim oBook As Object
    3. Dim oSheet As Object
    4. Try
    5. oExcel = CreateObject("Excel.Application")
    6. oBook = oExcel.Workbooks.Add
    7. oSheet = oBook.Worksheets(1)
    8. oSheet.Cells(1, 1) = "NAME"
    9. oSheet.Cells(1, 2) = NUMMER
    10. For i = 0 To DataGridView1.RowCount - 1
    11. oSheet.Cells(i + 2, 1).NumberFormat = "@"
    12. Try
    13. oSheet.Cells(i + 2, 1) = DataGridView1(0, i).Value.ToString()
    14. oSheet.Cells(i + 2, 2) = DataGridView1(1, i).Value.ToString()
    15. Catch ex As Exception
    16. End Try
    17. Next
    18. oSheet.Columns("A:Z").AutoFit
    19. Dim password As String = "Test"
    20. oBook.Password = password
    21. oBook.SaveAs(ExcelExportPfad, password:=password)
    22. oSheet = Nothing
    23. oBook.Close()
    24. oBook = Nothing
    25. oExcel.Quit()
    26. oExcel = Nothing


    Wenn ich bei Excel den Macrorecorder laufen lasse, erhalte ich folgendes:

    VB.NET-Quellcode

    1. Range("A1:C1").Select
    2. Application.CutCopyMode = False
    3. ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$C$1"), , xlYes).Name = _
    4. "Tabelle1"
    5. Range("Tabelle1[#All]").Select
    6. ActiveSheet.ListObjects("Tabelle1").TableStyle = "TableStyleMedium15"


    Ich weiß nun aber nicht wie ich das umsetzen kann :(

    Vielleicht könnt ihr mir ja weiterhelfen :)

    *Topic verschoben*

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Marcus Gräfe“ ()

    Hi,
    Arbeite doch mit der Microsoft.Office.Interop.Excel DLL, damit hast Du mehr Möglichkeiten, z-Bsp. direkten Zugriff auf Range...
    Ich ab eine Extension für den Import/Export einer DataTable, sieht in C# dann so aus:

    C#-Quellcode

    1. using Excel = Microsoft.Office.Interop.Excel;
    2. public static void ExcelExport(this DataTable dt, string path = null)
    3. {
    4. Excel.Application xl = new Excel.Application();
    5. if (xl == null)
    6. {throw new ApplicationException("Excel ist nicht installiert!!");}
    7. Excel.Workbook wkb = xl.Workbooks.Add();
    8. Excel.Worksheet wks = wkb.Sheets[1];
    9. int colCount = dt.Columns.Count;
    10. if (dt == null || colCount == 0)
    11. { throw new ApplicationException("Excel Export gescheitert, da die Tabelle leer ist"); }
    12. wks.Name = "Übersicht";
    13. wks.Cells[1, 2] = "Zusammenfassung der Umsätze";
    14. wks.Cells[3, 1] = "ID";
    15. wks.Cells[3, 2] = "Verkäufernummer";
    16. wks.Cells[3, 3] = "Name";
    17. wks.Cells[3, 4] = "Umsatz";
    18. int rowCount = dt.Rows.Count;
    19. object[,] Cells = new object[rowCount, colCount];
    20. for (int j = 0; j < rowCount; j++)
    21. for (int i = 0; i < colCount; i++)
    22. Cells[j, i] = dt.Rows[j][i];
    23. wks.get_Range((Excel.Range)(wks.Cells[4, 1]), (Excel.Range)(wks.Cells[rowCount + 1, colCount])).Value = Cells;
    24. wks.get_Range((Excel.Range)(wks.Cells[4, 4]), (Excel.Range)(wks.Cells[rowCount + 1, colCount])).NumberFormat = @"#.0,0 €";
    25. xl.Visible = true;
    26. }
    "Hier könnte Ihre Werbung stehen..."

    Sub7evenHH schrieb:

    Range("A1:C1").Select
    Application.CutCopyMode = False
    ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$C$1"), , xlYes).Name = _
    "Tabelle1"
    Range("Tabelle1[#All]").Select
    ActiveSheet.ListObjects("Tabelle1").TableStyle = "TableStyleMedium15"

    VB.NET-Quellcode

    1. With oSheet​.ListObjects.Add(4, oSheet.Range("$A$1:$C$1"), , 1)
    2. .Name = "Tabelle1"
    3. .TableStyle = "TableStyleMedium15
    4. End With
    --
    If Not Program.isWorking Then Code.Debug Else Code.DoNotTouch
    --
    @petaod ich habe deine Lösung versucht umzusetzen. Leider erscheint mir hier ein Fehler:


    XML-Quellcode

    1. ​System.ArgumentException: Falscher Parameter. (Ausnahme von HRESULT: 0x80070057 (E_INVALIDARG))
    2. bei Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack)
    3. bei Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
    4. bei test.ExcelExport() in C:\Testprojekt\test\test.vb:Zeile 567.


    Hast du da noch einen Rat?

    @MichaHo Danke für den Tipp. Das werde ich mir natürlich auch mal anschauen. Ich würde aber doch schon gerne bei dem ersten Entwurf bleiben. Trotzdem herzlichen Dank!
    Wenn ich den Teil von dir mit bei meinem Code einfüge.
    Der Code:

    VB.NET-Quellcode

    1. ​Dim oExcel As Object
    2. Dim oBook As Object
    3. Dim oSheet As Object
    4. Try
    5. oExcel = CreateObject("Excel.Application")
    6. oBook = oExcel.Workbooks.Add
    7. oSheet = oBook.Worksheets(1)
    8. With oSheet.ListObjects.Add(4, oSheet.Range("$A$1:$C$1"), , 1)
    9. .Name = "Tabelle1"
    10. .TableStyle = "TableStyleMedium15"
    11. End With
    12. oSheet.Cells(1, 1) = "NAME"
    13. oSheet.Cells(1, 2) = NUMMER
    14. For i = 0 To DataGridView1.RowCount - 1
    15. oSheet.Cells(i + 2, 1).NumberFormat = "@"
    16. Try
    17. oSheet.Cells(i + 2, 1) = DataGridView1(0, i).Value.ToString()
    18. oSheet.Cells(i + 2, 2) = DataGridView1(1, i).Value.ToString()
    19. Catch ex As Exception
    20. End Try
    21. Next
    22. oSheet.Columns("A:Z").AutoFit
    23. Dim password As String = "Test"
    24. oBook.Password = password
    25. oBook.SaveAs(ExcelExportPfad, password:=password)
    26. oSheet = Nothing
    27. oBook.Close()
    28. oBook = Nothing
    29. oExcel.Quit()
    30. oExcel = Nothing


    Beim Debuggen kann ich leider nichts finden was mir weiterhilft bzw. was ich verstehe. Die einzige Meldung die nach Aufruf kommt ist:

    Ausnahme ausgelöst: "System.ArgumentException" in Microsoft.VisualBasic.dll
    Über NuGet Microsoft.Office.Interop.Excel einbinden.
    Im Form dann

    VB.NET-Quellcode

    1. Imports Excel = Microsoft.Office.Interop.Excel


    Dann sollte folgender Code funktionieren.

    VB.NET-Quellcode

    1. Dim oExcel As Excel.Application = New Excel.Application()
    2. If oExcel Is Nothing Then
    3. Throw New ApplicationException("Excel ist nicht installiert!!")
    4. End If
    5. Dim oBook As Excel.Workbook
    6. oBook = oExcel.Workbooks.Add
    7. Dim oSheet As Excel.Worksheet = CType(oBook.Sheets(1), Excel.Worksheet)
    8. oSheet.Cells(1, 1) = "NAME"
    9. oSheet.Cells(1, 2) = "NUMMER"
    10. For i = 0 To DataGridView1.RowCount - 1
    11. If DataGridView1(0, i).Value IsNot Nothing Then
    12. Dim rangeToFormat = oSheet.Range(oSheet.Cells(i + 2, 1), oSheet.Cells(i + 2, 1))
    13. rangeToFormat.NumberFormat = "@"
    14. oSheet.Cells(i + 2, 1) = DataGridView1(0, i).Value.ToString()
    15. oSheet.Cells(i + 2, 2) = DataGridView1(1, i).Value.ToString()
    16. End If
    17. Next
    18. Dim lastRow As Integer = oSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing).Row
    19. Dim lastColumn As Integer = oSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell, Type.Missing).Column
    20. Dim xlRangeLast As Excel.Range = oSheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell)
    21. Dim xlRangeToFormat = oSheet.Range(oSheet.Cells(1, 1), oSheet.Cells(lastRow, lastColumn))
    22. oSheet.ListObjects.Add(, xlRangeToFormat, , Excel.XlYesNoGuess.xlYes).Name = "Tabelle1"
    23. oSheet.ListObjects("Tabelle1").TableStyle = "TableStyleMedium15"
    24. oSheet.Range("A1:B1").EntireColumn.AutoFit()
    25. Dim password As String = "Test"
    26. oBook.Password = password
    27. oBook.SaveAs("D:\Test.xlsx", Password:=password)
    28. oSheet = Nothing
    29. oBook.Close()
    30. oBook = Nothing
    31. oExcel.Quit()
    32. oExcel = Nothing

    Sub7evenHH schrieb:

    Die einzige Meldung die nach Aufruf kommt ist:

    Ausnahme ausgelöst: "System.ArgumentException" in Microsoft.VisualBasic.dll
    Mach die Try..Catch-Aufrufe raus und teile uns mit, in welcher Zeile die Fehlermeldung ausgelöst wird.
    --
    If Not Program.isWorking Then Code.Debug Else Code.DoNotTouch
    --
    @Dksksm
    Da erhalte ich folgenden Fehler:

    XML-Quellcode

    1. ************** Ausnahmetext **************
    2. System.InvalidCastException: Das COM-Objekt des Typs "Microsoft.Office.Interop.Excel.ApplicationClass" kann nicht in den Schnittstellentyp "Microsoft.Office.Interop.Excel._Application" umgewandelt werden. Dieser Vorgang konnte nicht durchgeführt werden, da der QueryInterface-Aufruf an die COM-Komponente für die Schnittstelle mit der IID "{000208D5-0000-0000-C000-000000000046}" aufgrund des folgenden Fehlers nicht durchgeführt werden konnte: Fehler beim Laden der Typbibliothek/DLL. (Ausnahme von HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY)).
    3. bei System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease)
    4. bei Microsoft.Office.Interop.Excel.ApplicationClass.get_Workbooks()


    @petaod Mit deinem Code in der Zeile:

    VB.NET-Quellcode

    1. With oSheet.ListObjects.Add(4, oSheet.Range("$A$1:$C$1"), , 1)

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Sub7evenHH“ ()

    Ersetz mal die 4 durch eine 1.

    Ansonsten: Versuche einfach selbst zu verstehen, was der Aufruf macht:
    docs.microsoft.com/en-us/offic…el.xllistobjectsourcetype
    docs.microsoft.com/en-us/offic…api/excel.listobjects.add
    und probier die Parameter aus, bis er macht, was du willst.
    --
    If Not Program.isWorking Then Code.Debug Else Code.DoNotTouch
    --
    Das sieht mir nicht nach "Option Strict On" aus....

    Wenn man bei mir die Zeile

    VB.NET-Quellcode

    1. oSheet.ListObjects.Add(, xlRangeToFormat, , Excel.XlYesNoGuess.xlYes).Name = "Tabelle1"

    mit

    VB.NET-Quellcode

    1. oSheet.ListObjects.Add(Excel.XlListObjectSourceType.xlSrcRange, xlRangeToFormat, , Excel.XlYesNoGuess.xlYes).Name = "Tabelle1"

    ersetzt, müsste es laufen, warum sollte es sonst bei mir funktionieren?

    Wenn der Thread erledigt ist, dann setzt ihn doch bitte auch auf ereldigt.

    Dksksm schrieb:

    Das sieht mir nicht nach "Option Strict On" aus....
    Nein, ist weit weg von Strict On
    Es beginnt damit, dass die Excel-Instanz per Late Binding mit CreateObject erzeugt wird, weshalb auch die ganzen Interop-Konstanten nicht verfügbar sind und numerisch angegeben werden müssen.
    --
    If Not Program.isWorking Then Code.Debug Else Code.DoNotTouch
    --