导出excel数据时出错:'',十六进制值0x07,在c#中是无效字符

本文关键字:字符 无效 0x07 数据 excel 十六进制 导出 出错 | 更新日期: 2023-09-27 18:08:40

我有奇怪的问题,当我试图导出数据在剑道ui网格excel .....

错误:

  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.

和下面的方法我被用来导出到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 });
   }

在下面提到的方法中,我得到了上面的异常错误…

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

请任何人帮助如何克服这个错误,而导出到excel数据....

导出excel数据时出错:'',十六进制值0x07,在c#中是无效字符

XML可以处理几乎任何字符,但是有范围、控制代码等,它不能处理。

你最好的办法,如果你不能让他们修复他们的输出,是净化你收到的原始数据。您需要将非法字符替换为您注意到的字符参考格式。

有很多符号不能在xml代码中出现。为了取代它们,我们可以使用Reqex。替换

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

x26是与符号'&'字符-可以用'&'编码,以便它出现在您的excel工作表中。