我正在创建一个用户必须上传excel文件的应用程序,使用下面的代码我验证文件是否有效或已损坏。为此我使用mircrosoft.Office.interop.excel dll。 在下面的代码中,我得到异常“来自HRESULT的异常:0x800A03EC ”
static void openfile()
{
try
{
string mySheet = @"E:\7.xlsx";
var excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Workbooks books = excelApp.Workbooks;
Excel.Workbook sheet = books.Open(mySheet);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
static void Main(string[] args)
{
openfile();
}
问题是什么?你能帮忙解决这个问题吗?
答案 0 :(得分:0)
尝试使用此代码:
string mySheet = @"E:\7.xlsx";
var excelApp = new Excel.Application();
excelApp.Visible = true;
Excel.Workbook sheet = excelApp.Workbooks.Open(mySheet,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, true);
我猜你的档案E:\7.xlsx
有点腐败。您也可以尝试正常打开它,看看会发生什么。
但是如果你只是想知道文件是否损坏,这个例外会给你提供信息:是的,文件已损坏。
答案 1 :(得分:0)
打开控制台项目并尝试此操作...这是正常工作。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using Microsoft.Office.Interop.Excel;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
string mySheet = @"E:\7.xlsx";
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
excelApp.Visible = true;
Workbook wb = excelApp.Workbooks.Open(mySheet);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}