简体   繁体   English

在VB.NET中以编程方式从Excel文件中删除垃圾文本

[英]Programmatically Removing Garbage Text from Excel File in VB.NET

I have an Excel file that downloads automatically, but for some reason the binary XLS file contains some garbage HTML text at the end of it. 我有一个自动下载的Excel文件,但是由于某种原因,二进制XLS文件的末尾包含一些垃圾HTML文本。

When opening the file in the Excel application, it shows a warning but proceeding will automatically remove the garbage HTML text. 在Excel应用程序中打开文件时,它会显示警告,但继续操作会自动删除垃圾HTML文本。

However, I need to open the file programmatically. 但是,我需要以编程方式打开文件。 When doing so via 通过这样做时

Dim wb As Workbook = Excel.Workbooks.Open(ExcelFileName)

It throws: 它抛出:

Exception from HRESULT: 0x800A03EC 来自HRESULT的异常:0x800A03EC

Any advice on how to either: 关于以下方面的任何建议:

a) Get Excel to perform a similar action as if I manually opened it and remove the garbage HTML automatically. a)让Excel执行类似的操作,就像我手动打开它并自动删除垃圾HTML一样。 NOTE: Tried setting the XlCorruptLoad.xlRepairFile parameter and it didn't work. 注意:尝试设置XlCorruptLoad.xlRepairFile参数,但它不起作用。

OR b) Remove the garbage text from the XLS binary file (FileStream?) and resave it before attempting to open the file with the code mentioned above. 或b)在尝试使用上述代码打开文件之前,从XLS二进制文件(FileStream?)中删除垃圾文本并重新保存。

The garbage html always comes at the very end of the file and starts with 垃圾html始终位于文件的末尾,并以

Where are you downloading the file from? 您从哪里下载文件? My guess is that the server is written in ASP.NET, and has just failed to include a call to Response.End() after writing the Excel file to the response. 我的猜测是该服务器是用ASP.NET编写的,并且在将Excel文件写入Response.End()后才未能包含对Response.End()的调用。 If you're responsible for the server, that's definitely where you should fix it. 如果您负责服务器,那肯定是应该修复它的地方。

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

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