vb.net - Hexadecimal value 0x00, is an invalid character OpenXML SDK -
i have following function takes datatable , builds excel spreadsheet data using openxml sdk, , returns byte array. 9 out of ten times, works great, occasionally, error out following message:
'.', hexadecimal value 0x00, invalid character.
here function:
private function buildexcelsheet(byref tbl datatable) byte() dim memstream new memorystream memstream.position = 0 dim spreadsheetdocument spreadsheetdocument = spreadsheetdocument.create(memstream, spreadsheetdocumenttype.workbook) ' add workbookpart document. dim workbookpart workbookpart = spreadsheetdocument.addworkbookpart try workbookpart.workbook = new workbook catch ex exception debug.writeline(ex.message) end try ' add worksheetpart workbookpart. dim worksheetpart worksheetpart = workbookpart.addnewpart(of worksheetpart)() try worksheetpart.worksheet = new worksheet(new sheetdata()) catch ex exception debug.writeline(ex.message) end try ' add sheets workbook. dim sheets sheets = spreadsheetdocument.workbookpart.workbook.appendchild(of sheets)(new sheets()) ' append new worksheet , associate workbook. dim sheet sheet = new sheet try sheet.id = spreadsheetdocument.workbookpart.getidofpart(worksheetpart) sheet.sheetid = 1 sheet.name = "data" sheets.append(sheet) catch ex exception debug.writeline(ex.message) end try dim sheetdata = worksheetpart.worksheet.getfirstchild(of sheetdata)() try integer = 0 tbl.rows.count - 1 dim row datarow = tbl.rows(i) dim erow new spreadsheet.row ' loop through cols j integer = 0 tbl.columns.count - 1 dim ecell new spreadsheet.cell ' ecell.datatype = spreadsheet.cellvalues.string if string.isnullorempty(row(j).tostring) = false 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 exception debug.writeline(ex.message) end try try workbookpart.workbook.save() catch ex exception debug.writeline(ex.message) end try try ' close document. spreadsheetdocument.close() catch ex exception debug.writeline(ex.message) end try memstream.position = 0 dim bytes byte() = memstream.toarray() memstream.close() return bytes end function
when check debug output, error occurring @ following line:
spreadsheetdocument.close()
i assume has data being passed function, when @ data, i'm not seeing null characters.
it simple i'm overlooking, appreciated.
thanks.
edit:
here debug output around error:
a first chance exception of type 'system.threading.threadabortexception' occurred in mscorlib.dll exception of type 'system.threading.threadabortexception' occurred in mscorlib.dll not handled in user code first chance exception of type 'system.argumentexception' occurred in system.xml.dll '.', hexadecimal value 0x00, invalid character. first chance exception of type 'system.threading.threadabortexception' occurred in mscorlib.dll
and here stacktrace:
> 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
just aren't missing anything, here caller:
protected sub page_load(byval sender object, byval e system.eventargs) handles me.load dim bytes byte() try call getrecords() catch ex exception debug.writeline("problem loading records export excel: " & ex.message) end try try bytes = buildexcelsheet(tbl) catch ex 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 exception debug.writeline(ex.message) end try response.end() end sub
Comments
Post a Comment