vb.net '',十六进制值 0x1A,是一个无效字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36746851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:50:04  来源:igfitidea点击:

'', hexadecimal value 0x1A, is an invalid character

vb.netclosedxml

提问by Simon Price

I am using closedXML to take a datatable and place this into an Excel file.

我正在使用 closedXML 来获取数据表并将其放入 Excel 文件中。

The code I am working with works, with 99% of the files I put through the application, but I get an error with a file every now and then. (no this is not a debugging issue)

我正在使用的代码可以处理我通过应用程序提交的 99% 的文件,但我时不时地收到一个文件错误。(不,这不是调试问题)

The problem must originate from the data, however I am at a loss on how to resolve this.

问题必须源于数据,但是我不知道如何解决这个问题。

The code I'm using is

我使用的代码是

Public Sub WriteToExcel(dt As DataTable, filePath As String)

    Dim workbook = New XLWorkbook()
    Dim worksheet = workbook.Worksheets.Add(dt, "Call Validate Export")
    Try
        workbook.SaveAs(filePath)
        Process.Start(filePath)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

When saving the file I get the error

保存文件时出现错误

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

'',十六进制值 0x1A,是一个无效字符。

between the '' there is a little arrow pointing to the right.

在''之间有一个指向右边的小箭头。

When reading the file in to the datatable it reads in fine, looking at the file I see no hex characters.

将文件读入数据表时,它读取得很好,查看文件时我看不到十六进制字符。

The file being read in is a ^ delimited csv file.

正在读入的文件是一个 ^ 分隔的 csv 文件。

So, my question is how to I check and repair\replace the bad characters in the output that will allow me to save 100% of the time.

所以,我的问题是如何检查和修复\替换输出中的错误字符,以便我节省 100% 的时间。

回答by Francois Botha

From the XML specification ( https://www.w3.org/TR/xml/#charsets)

来自 XML 规范 ( https://www.w3.org/TR/xml/#charsets)

Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] /* any Unicode character, excluding the surrogate blocks, FFFE, and FFFF. */

字符 ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] /* 任何 Unicode 字符,不包括代理块、FFFE 和 FFFF。*/

Which implies that character #x1A is an invalid character. You should manually remove it.

这意味着字符 #x1A 是无效字符。您应该手动删除它。

回答by Francois Botha

https://github.com/ClosedXML/ClosedXML/pull/66is a pull request which could solve your issue. Please try it.

https://github.com/ClosedXML/ClosedXML/pull/66是一个拉取请求,可以解决您的问题。请尝试一下。

回答by Bharat Mori

@Simon,

@西蒙,

It looks like whatever you are trying to export to Excel, it contains some invalid characters (Arrow pointing to right side).

看起来无论您要导出到 Excel 中的任何内容,它都包含一些无效字符(箭头指向右侧)。

I was receiving the similar error and upon detailed look, I come to know that I am exporting DataTable into Excel and one of the cell value of DataTable was looking like:

我收到了类似的错误,经过详细查看,我知道我正在将 DataTable 导出到 Excel 中,并且 DataTable 的单元格值之一看起来像:

Please Note -> Some comment (Here, -> is actually a single character : Arrow pointing to right side)

请注意 -> 一些评论(这里, -> 实际上是一个字符:箭头指向右侧)

I have removed those characters and it is working fine now.

我已经删除了这些字符,现在工作正常。

Hope this helps.

希望这可以帮助。