CopyFromRecordset vb.net等效于openxml电子表格

时间:2015-12-17 09:39:44

标签: vb.net excel openxml

我有一个应用程序,我正在从我在这里找到的帖子进行调整,以使用OpenXML构建格式良好的电子表格。该工具连接到数据库以获取数据并构建20(ish)工作表。这部分工作正常。我遇到的问题是获取数据行。我最初尝试的方法是逐个单元格写单元格,但每张单页可能多达5万行,因此逐个单元格写入速度非常慢。

我从(http://www.codeproject.com/Articles/371203/Creating-basic-Excel-workbook-with-Open-XML)开始的应用程序是对该主题的精彩介绍,对我帮助很大。代码中甚至还有一种用于AddTable的方法。遗憾的是,它并不完整,无论我尝试什么,我都无法使代码正常运行。

我有一个传递给方法的数据表,我正在努力做的是将数据表转储到工作表中。

到目前为止,这是我的代码:

Public Shared Function AddTable(spreadsheet As DocumentFormat.OpenXml.Packaging.SpreadsheetDocument, worksheet As DocumentFormat.OpenXml.Spreadsheet.Worksheet, columnIndex As UInt32, rowIndex As UInt32, tbl As System.Data.DataTable)

    'Dim t As DocumentFormat.OpenXml.Spreadsheet.Table
    Dim row As DocumentFormat.OpenXml.Spreadsheet.Row
    Dim sheetPart As DocumentFormat.OpenXml.Packaging.WorksheetPart
    'Dim sheetPart = spreadsheet.WorkbookPart.AddNewPart(Of DocumentFormat.OpenXml.Spreadsheet.Worksheet)
    Dim sheetData = New DocumentFormat.OpenXml.Spreadsheet.SheetData()
    'Dim sheetPart = allWorksheetParts.Add(thisWorkbookpart.AddNewPart(Of WorksheetPart)(currentSheet.Id))
    'sheetPart.Worksheet = New DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData)

    sheetPart = spreadsheet.WorkbookPart.AddNewPart(Of DocumentFormat.OpenXml.Packaging.WorksheetPart)()
    sheetPart.Worksheet = New DocumentFormat.OpenXml.Spreadsheet.Worksheet(New DocumentFormat.OpenXml.Spreadsheet.SheetData())
    sheetPart.Worksheet.Save()

    ' ' Add the sheet and make relation to workbook
    ' sheet = New DocumentFormat.OpenXml.Spreadsheet.Sheet With {
    '.Id = spreadsheet.WorkbookPart.GetIdOfPart(sheetPart),
    '.SheetId = (spreadsheet.WorkbookPart.Workbook.Sheets.Count() + 1),
    '.Name = name}

    ' sheets.Append(sheet)
    ' spreadsheet.WorkbookPart.Workbook.Save()

    Dim strCols As String = ""
    'Dim arrColumns As List(Of String)

    Try
        For Each column In tbl.Columns
            row = New DocumentFormat.OpenXml.Spreadsheet.Row
            strCols = strCols & column.ColumnName & ","
            'arrColumns = strCols.Split(",")

            Dim cell As New DocumentFormat.OpenXml.Spreadsheet.Cell()
            cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String
            cell.CellValue = New DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName)
            row.AppendChild(cell)
        Next

        Dim arrColumns As New List(Of String)(strCols.Split(","c))
        worksheet.AppendChild(row).DefaultIfEmpty
        'sheetData.AppendChild(row).DefaultIfEmpty
        'worksheet.AppendChild(sheetData).DefaultIfEmpty
        'worksheet.AppendChild(row)

        For Each r In tbl.Rows
            Dim newRow As New DocumentFormat.OpenXml.Spreadsheet.Row()
            For Each col In arrColumns
                Dim cell As New DocumentFormat.OpenXml.Spreadsheet.Cell()
                cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String
                cell.CellValue = New DocumentFormat.OpenXml.Spreadsheet.CellValue(r(col).ToString())
                newRow.AppendChild(cell)
            Next

            worksheet.AppendChild(newRow)
        Next

        Return True
    Catch ex As Exception
        Return False
    End Try

End Function

对不起,它太乱了,我已经尝试过各种各样的东西让它运转起来。

代码不会产生错误,但它也不会产生填充的工作表。

任何人都可以帮助我掌握基础知识吗?

0 个答案:

没有答案