1

When I try to open a text file to a xlsx format from Aspose.Cells with this code:

Excel.Workbook workbook = new Excel.Workbook(filePath, new Excel.TxtLoadOptions(Excel.LoadFormat.Xlsx) { SeparatorString = " " });

it crashes with the exception being The file is corrupted. This is only happening since yesterday.

I feel like nothing changed that could possibly affect the Aspose.Cells library from functionning correctly.

I have searched quite a bit online, but nothing really relates to a file being corrupted when opening it.

I thought at first that the text files were the issue, but I tried creating a new text file with the text: "test test1 test2"

Even with this new file it is still telling me it is corrupted.

Any ideas what the issue could be?

Thank you for your help.

EDIT: After further review and testing, by removing the parameter new Excel.TxtLoadOptions(Excel.LoadFormat.Xlsx) { SeparatorString = " " }

It works, but the text only gets put into one cell instead of splitting it between multiple cells. Any ideas of a workaround? Thanks.

Zaehos
  • 175
  • 2
  • 13
  • Well, a file containing random text is not a file in the Excel format, especially not Xlsx which is a particular zipped file. Why would you expect this to work? – CodeCaster Aug 06 '19 at 14:34
  • Because it did. This loads a text file into a xlsx format. The content of the file does not matter. – Zaehos Aug 06 '19 at 14:36
  • What happens when you split your text with commas instead of spaces and remove the portion `{ SeparatorString = " " }` – Dumisani Aug 06 '19 at 14:42
  • Then why do you pass `Excel.LoadFormat.Xlsx`? You're not loading an XSLX file. – CodeCaster Aug 06 '19 at 14:46
  • It is txtLoadOptions object. You are passing the file type you want to load the text file to. @CodeCaster – Zaehos Aug 06 '19 at 14:54
  • I will try asap @Dumisani – Zaehos Aug 06 '19 at 14:55
  • It still does the same when separated with commas and removing the property SeparatorString. @Dumisani – Zaehos Aug 06 '19 at 19:21

1 Answers1

0

Answering my own question...

In my case I was loading the text file into a .xlsx extension due to me wanting to save the file back to xlsx after applying formatting.

Turns out that the Aspose.cells API is not able to load a .txt as xlsx. However, you can load it as a CSV than save it as an xlsx after like so:

Excel.Workbook workbook = new Excel.Workbook(filePath, new Excel.TxtLoadOptions(Excel.LoadFormat.CSV) { SeparatorString = " " });

Hope this helps someone in the future.

Zaehos
  • 175
  • 2
  • 13