十六进制值0x00,是无效字符OpenXML SDK

时间:2015-04-22 13:57:46

标签: vb.net visual-studio-2012 bytearray openxml-sdk memorystream

我有以下函数,该函数使用OpenXML SDK获取数据表并使用数据构建Excel电子表格,并将其作为字节数组返回。十分之九,它的效果很好,但偶尔会出现以下消息:

'.', hexadecimal value 0x00, is an invalid character.

这是我的功能:

Private Function buildExcelSheet(ByRef tbl As DataTable) As Byte()
        Dim memstream As New MemoryStream
        memstream.Position = 0
        Dim spreadsheetDocument As SpreadsheetDocument = spreadsheetDocument.Create(memStream, SpreadsheetDocumentType.Workbook)

        ' Add a WorkbookPart to the document.
        Dim workbookpart As WorkbookPart = spreadsheetDocument.AddWorkbookPart
        Try
            workbookpart.Workbook = New Workbook
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        End Try


        ' Add a WorksheetPart to the WorkbookPart.
        Dim worksheetPart As WorksheetPart = workbookpart.AddNewPart(Of WorksheetPart)()
        Try
            worksheetPart.Worksheet = New Worksheet(New SheetData())
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        End Try

        ' Add Sheets to the Workbook.
        Dim sheets As Sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(Of Sheets)(New Sheets())

        ' Append a new worksheet and associate it with the workbook.
        Dim sheet As Sheet = New Sheet
        Try
            Sheet.Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart)
            Sheet.SheetId = 1
            Sheet.Name = "Data"

            sheets.Append(Sheet)
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        End Try

        Dim sheetData = worksheetPart.Worksheet.GetFirstChild(Of SheetData)()
        Try
            For i As Integer = 0 To tbl.Rows.Count - 1
                Dim row As DataRow = tbl.Rows(i)
                Dim erow As New Spreadsheet.Row
                ' loop through cols
                For j As Integer = 0 To tbl.Columns.Count - 1
                    Dim ecell As New Spreadsheet.Cell
                    ' ecell.DataType = Spreadsheet.CellValues.String
                    If String.IsNullOrEmpty(row(j).ToString) = False Then
                        ecell.CellValue = New Spreadsheet.CellValue(row(j).ToString)
                        ecell.DataType = New EnumValue(Of CellValues)(CellValues.String)
                    Else
                        ecell.CellValue = New Spreadsheet.CellValue(" ")
                    End If
                    erow.AppendChild(ecell)
                Next
                sheetData.AppendChild(erow)

            Next
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        End Try

        Try
            workbookpart.Workbook.Save()
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        End Try

        Try
            ' Close the document.
            spreadsheetDocument.Close()
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        End Try
        memStream.Position = 0
        Dim bytes As Byte() = memStream.ToArray()

        memStream.Close()

        Return bytes
    End Function

当我检查调试输出时,错误总是发生在以下行:

spreadsheetDocument.Close()

我认为它与传递给函数的数据有关,但是当我查看数据时,我没有看到任何NULL字符。

当然,我可以忽略一些简单的事情,所以任何帮助都会非常感激。

感谢。

编辑:

这是围绕错误的调试输出:

A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll
An exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll but was not handled in user code
A first chance exception of type 'System.ArgumentException' occurred in System.Xml.dll
'.', hexadecimal value 0x00, is an invalid character.
A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll

这是堆栈跟踪:

>   Compliance.dll!Compliance.Excel.buildExcelSheet(System.Data.DataTable tbl) Line 118 Basic
    Compliance.dll!Compliance.Excel.Page_Load(Object sender, System.EventArgs e) Line 30 + 0x13 bytes   Basic

这样你们都不会错过任何东西,这里是来电者:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim bytes As Byte()
        Try
            Call getRecords()
        Catch ex As Exception
            Debug.WriteLine("Problem Loading records for export to Excel: " & ex.Message)
        End Try

        Try
            bytes = buildExcelSheet(tbl)
        Catch ex As Exception
            Debug.WriteLine("Problem building Excel Spreadsheet: " & ex.Message)
        End Try


        Response.Clear()
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}_{1}.xlsx", focustype, rectype))
        Try
            Response.BinaryWrite(bytes)
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        End Try

        Response.End()

    End Sub

0 个答案:

没有答案
相关问题