简体   繁体   English

Aspose.Cells-文件已损坏

[英]Aspose.Cells - The File is Corrupted

When I try to open a text file to a xlsx format from Aspose.Cells with this code: 当我尝试使用以下代码从Aspose.Cells将文本文件打开为xlsx格式时:

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. 我觉得没有任何可能影响Aspose.Cells库正常运行的更改。

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" 起初我以为文本文件是问题,但是我尝试创建一个带有以下文本的新文本文件: “ 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 = " " } 编辑 :在进一步检查和测试之后,通过删除参数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. 谢谢。

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. 在我的情况下,由于要在应用格式后将文件保存回xlsx,因此我将文本文件加载为.xlsx扩展名。

Turns out that the Aspose.cells API is not able to load a .txt as xlsx. 原来Aspose.cells API无法将.txt加载为xlsx。 However, you can load it as a CSV than save it as an xlsx after like so: 但是,可以像这样将其加载为CSV而不是将其另存为xlsx:

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

Hope this helps someone in the future. 希望这对以后的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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