Excel Interop 0×80010105 [英] Excel Interop 0×80010105

查看:31
本文介绍了Excel Interop 0×80010105的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个转换Excel文件的工具.当用户转换excel文件时,代码首先创建一个Excel文件.当我在系统上(Excel 2007)时,它可以正常工作.当我在具有Excel 98的系统上安装程序时,它将引发异常.我遇到的第一个异常是另一个异常,但同时也是HResult错误.我通过将"SaveAs"更改为"SaveCopyAs"来解决此问题.然后它被修正了!也适用于安装了Excel 98的其他系统,但是现在我又遇到了另一个HResult错误.这是什么问题:

I created a tool to convert Excel files. When the user convert a excel file the code create a Excel file first. When I'm on my system (Excel 2007) it's working without problems. When I install the program on a system with Excel 98 then it's throwing an exception. The first exception I got was another one, but also a HResult error. I fixed this through change the "SaveAs" to "SaveCopyAs". Then it was FIXED! Also for the other systems where Excel 98 is installed, but now I have another HResult error. What is the problem here:

            _savePath = sfd.FileName;

            MessageBox.Show("GOOD1");
            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();

            MessageBox.Show("GOOD2");
            // The exception is here on the workbook
            // HResult 8x00010105 (COMException)
            Microsoft.Office.Interop.Excel.Workbook workbook = excelApp.Workbooks.Add(Missing.Value);

            MessageBox.Show("GOOD3");
            workbook.SaveCopyAs(_savePath);

            MessageBox.Show("GOOD4");
            lblSavePath.Text = _savePath;
            workbook.Close(false, _savePath, Type.Missing);
            excelApp.Quit();

我希望有人可以帮助我解决这个问题.

I hope someone could help me with this problem.

谢谢

杰米

推荐答案

您可以尝试:

_savePath = sfd.FileName;
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
MessageBox.Show("GOOD2");
excelApp.SheetsInNewWorkbook = 1;      
try
{
   // Must be surrounded by try catch to work.
   excelApp.Visible = true;
}
catch (Exception e)
{
    Console.WriteLine("-------Error hiding the application-------");
    Console.WriteLine("Occured error might be: " + e.StackTrace);
} 
Microsoft.Office.Interop.Excel.Workbook workbook
workbook = excelApp.Workbooks.Open("your excel file", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); // if working with another excel
excelApp.Workbooks.Add();
MessageBox.Show("GOOD3");
Excel.Worksheet sheetC = excelApp.Sheets.get_Item(1);   
sheetC.Name = "name-of-sheet";

workbook.SaveCopyAs(_savePath); // SaveAs should work actually.
workbook.Close();
excelApp.Quit(); 

我接受了您的解决方案,并修改了不正确的 Missing.Value 部分.另外,您实际上不需要为 workbook.Close 提供参数.在此处找到解决方案:

I took your solution and modified the Missing.Value part which wasn't correct. Plus you don't really need to give parameters to the workbook.Close. Solution found here: I want to add only one sheet after creating an Excel workbook through C#

这篇关于Excel Interop 0×80010105的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆