2

giving '', hexadecimal value 0x02, is an invalid character error

foreach (DataRow dataRow in data.Rows)
    {
         row = new DocumentFormat.OpenXml.Spreadsheet.Row { RowIndex =+rowcount};
         for (int i = 0; i < fieldsToExpose.Length; i++)
            {
               row.Append(CreateTextCell(ColumnLetter(i), rowcount,dataRow[fieldsToExpose[i]].ToString()));
            }
             sheetData.AppendChild(row);
     }
 worksheetPart.Worksheet.Save();

Getting error when saving the worksheetPart.Worksheet.Save() because 'row' contains the hexadecimal value

3
  • Can you please provide more details? Commented Dec 16, 2017 at 0:01
  • Can you reduce your data that you are looping through so that you can track down what portion of it is actually causing the problem? Is it a particular cell, or is it endemic throughout the spreadsheet? Commented Dec 16, 2017 at 0:08
  • Is the data unicode or binary? Commented Dec 16, 2017 at 0:44

1 Answer 1

0

There must be some invalid XML text. you can check it by using below statement:

System.Xml.XmlConvert.VerifyXmlChars(dataRow[fieldsToExpose[i]].ToString())

Use the below code:

row.Append(
  CreateTextCell(
    ColumnLetter(i), 
    rowcount, 
    Regex.Replace(
      dataRow[fieldsToExpose[i]].ToString(), 
      @"[\u0000-\u0008\u000A-\u001F\u0100-\uFFFF]", 
      ""
    )
  )
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.