简体   繁体   中英

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 .....

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

  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 ....

Many thanks in advance..

XML can handle just about any character, but there are ranges, control codes and such, that it won't.

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. For replace them we can use 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工作表中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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