简体   繁体   English

导出到excel数据时出错:'',十六进制值0x07,使用C#是无效字符

[英]error while exporting to excel data : ' ', hexadecimal value 0x07, is an invalid character using c#

I have got strange problem when I try to export data in kendo ui grid to excel ..... 当我尝试将Kendo ui网格中的数据导出到Excel时,我遇到了一个奇怪的问题。

Error : 错误:

  An exception of type 'System.ArgumentException' occurred in System.Xml.dll but was not handled in user code

Additional information: ' ', hexadecimal value 0x07, is an invalid character.

and the below method I was used for export to excel 和下面的方法,我被用来导出到Excel

  private static void WriteDataTableToExcelWorksheet(DataTable dt, WorksheetPart worksheetPart)
  {
        OpenXmlWriter writer = OpenXmlWriter.Create(worksheetPart);
        writer.WriteStartElement(new Worksheet());
        writer.WriteStartElement(new SheetData());

        string cellValue = "";

        int numberOfColumns = dt.Columns.Count;
        bool[] IsNumericColumn = new bool[numberOfColumns];

        string[] excelColumnNames = new string[numberOfColumns];
        for (int n = 0; n < numberOfColumns; n++)
            excelColumnNames[n] = GetExcelColumnName(n);

        uint rowIndex = 1;

        writer.WriteStartElement(new Row { RowIndex = rowIndex });
        for (int colInx = 0; colInx < numberOfColumns; colInx++)
        {
            DataColumn col = dt.Columns[colInx];
            AppendTextCell(excelColumnNames[colInx] + "1", col.ColumnName, ref writer);
            IsNumericColumn[colInx] = (col.DataType.FullName == "System.Decimal") || (col.DataType.FullName == "System.Int32") || (col.DataType.FullName == "System.Double") || (col.DataType.FullName == "System.Single");
        }
        writer.WriteEndElement();   

        double cellNumericValue = 0;
        foreach (DataRow dr in dt.Rows)
        {

            ++rowIndex;

            writer.WriteStartElement(new Row { RowIndex = rowIndex });

            for (int colInx = 0; colInx < numberOfColumns; colInx++)
            {
                cellValue = dr.ItemArray[colInx].ToString();

                if (IsNumericColumn[colInx])
                {

                    cellNumericValue = 0;
                    if (double.TryParse(cellValue, out cellNumericValue))
                    {
                        cellValue = cellNumericValue.ToString();
                        AppendNumericCell(excelColumnNames[colInx] + rowIndex.ToString(), cellValue, ref writer);
                    }
                }
                else
                {

                    AppendTextCell(excelColumnNames[colInx] + rowIndex.ToString(), cellValue, ref writer);
                }
            }
            writer.WriteEndElement(); 
        }
        writer.WriteEndElement(); 
        writer.WriteEndElement(); 

        writer.Close();
   }

   private static void AppendTextCell(string cellReference, string cellStringValue, ref OpenXmlWriter writer)
   {

        writer.WriteElement(new Cell { CellValue = new CellValue(cellStringValue), CellReference = cellReference, DataType = CellValues.String });
   }

in the below mentioned method I am getting above exception error.. 在下面提到的方法中,我遇到了以上异常错误。

  private static void AppendTextCell(string cellReference, string cellStringValue, ref OpenXmlWriter writer)
  {           
       writer.WriteElement(new Cell { CellValue = new CellValue(cellStringValue), CellReference = cellReference, DataType = CellValues.String });
  }

would any one please help on how to overcome this error while exporting to excel data .... 任何人都可以帮助导出导出到excel数据时如何克服此错误....

Many thanks in advance.. 提前谢谢了..

XML can handle just about any character, but there are ranges, control codes and such, that it won't. XML几乎可以处理任何字符,但是有范围,控制代码等,而它不能。

Your best bet, if you can't get them to fix their output, is to sanitize the raw data you're receiving. 最好的选择是,如果无法让他们修复其输出,则要清理接收到的原始数据。 You need replace illegal characters with the character reference format you noted. 您需要使用记下的字符参考格式替换非法字符。

There are a lot of symbols which can't be in xml code. xml代码中没有很多符号。 For replace them we can use Reqex.Replace 要替换它们,我们可以使用Reqex.Replace

static string ReplaceHexadecimalSymbols(string txt)
{
   string r = "[\x00-\x08\x0B\x0C\x0E-\x1F\x26]";
   return Regex.Replace(txt, r,"",RegexOptions.Compiled);
}

x26是“&”字符-可以用“&”编码,以便出现在Excel工作表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM