Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using closedXML C# library, How can I figure out what data is causing this error when trying to save

I have a C# app that exports to Excel using ClosedXML. It works fine but just ran into an issue where when i hit the :

  var ms = new MemoryStream();
  workbook.SaveAs(ms);

I get an exception:

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

Its definitely data related because it I look at certain data it works fine but other data it causes this issue.

how can i figure out which character is causing the issue? Also, once I figure that out, what is the best way of finding where this character is within my data?

like image 292
leora Avatar asked Nov 20 '13 17:11

leora


People also ask

Does ClosedXML need Excel installed?

ClosedXML allows you to create Excel files without the Excel application. The typical example is creating Excel reports on a web server.

How do I read an Excel file in ClosedXML?

C# read Excel file using ClosedXML. Excel; using var wbook = new XLWorkbook("simple. xlsx"); var ws1 = wbook. Worksheet(1); var data = ws1.

What is the difference between OpenXml and ClosedXML?

Macros – ClosedXml doesn't support macros as its base library OpenXml also doesn't support it. Embedding – We cannot embed any file into Excel using ClosedXml, no APIs built for that, so some features of OpenXml still need to be implemented. Charts – No functionality related to charting is present.


1 Answers

Since you have invalid characters in the data / strings you put into the ClosedXML sheet, you have to find them and get them out.

The simplest solution is to add

.Replace((0x0B).ToString(), " ")

to all your strings to get rid of the vertical tabs and replace them with spaces.

like image 139
Raidri Avatar answered Sep 17 '22 10:09

Raidri