This project has moved. For the latest updates, please go here.

Text File Imports

Nov 13, 2014 at 5:36 PM
So far, the package works very, very well with datatables created from database data sources.

Is it possible to import a delimited text file into the workbook? I know of one package which allows for text import options and can consume text files directly. This is hugely important to our needs for my company; is it possible with ClosedXML? If so, can you please update the documentation to demonstrate?

Thank you for a great product.
Nov 13, 2014 at 6:11 PM
You have to parse the file and then feed the data to ClosedXML. This is because there are a million and one ways to create a text file with data.
Nov 14, 2014 at 1:42 AM
Thank you for the quick response. I really appreciate it. Too bad; I was hoping it would work.
Nov 14, 2014 at 1:33 PM
Yes, only you know what to expect when reading a text file ... what are the delimiters, how many columns, what type of values are in each column, so it is quite a lot to expect software to parse a plain text file and get it right. Excel tries and gets it wrong a significant number of times. Too often eliminating leading zeros for example.

However, I will say that if you parse each row into a well-constructed object. You can pass a list of those objects to the InsertTable method and you have the whole Excel sheet created with one method call. Headings and all.
Such simplicity can't be beat.

sheet.Cell(1, 1).InsertTable(listOfRowObjects)

See this discussion:
Nov 14, 2014 at 6:17 PM
roberttanenbaum: I'm sorry, no disrespect intended, but that is simply incorrect. I was using another library which also uses Open XML as its backbone and was able to import text files directly to a spreadsheet without issue. The only requirement was establishing what the import options were, and that was a simple line of code for each field. The import itself was a single line of code. I didn't have to parse anything; the library did it for me.

Additionally, there are other "helper" libraries which can handle parsing text files just fine. I ask the question here because ClosedXML worked great for handling data down from a database, but I didn't see anything in the documentation addressing text file imports. Now I know ClosedXML doesn't do them, and that's fine. It does what it can do very well. But the other library I use can do this also, and handle the text imports, so I guess for doing those things I'll have to stay with that one.

I don't mean to be argumentative, but I know for a fact some Open XML libraries can do this, out of the box. I use one now.
Nov 14, 2014 at 7:22 PM
Thank you. That's good to know.