C#,创建excel 97-2003(.xls)Interop

时间:2023-01-15 10:41:14

using Excel = Microsoft.Office.Interop.Excel;

使用Excel = Microsoft.Office.Interop.Excel;

I need to save a excel file (.xls) from my datagridview. All works fine, writing data, changing cell format etc. But problem appears while saving the doc.

我需要从我的datagridview中保存一个excel文件(.xls)。一切正常,写入数据,更改单元格格式等。但保存文档时出现问题。

string format = "yyyy_MM_dd_HH-mm";
string pathtoexport = @"C:\DM\SMS\Przychodzace\Znalezione_SMS_" + DateTime.Now.ToString(format) + ".xls";
oWB.SaveAs(pathtoexport, Excel.XlFileFormat.xlWorkbookNormal, missing, missing,
      false, false, Excel.XlSaveAsAccessMode.xlNoChange,
      missing, missing, missing, missing, missing);
    }

Well some days ago it was working, but now it can only say

好几天前它正在运作,但现在它只能说

HRESULT: 0x800A03EC"}

HRESULT:0x800A03EC“}

I need .xls to import it to SQL. Any advices?

我需要.xls将它导入SQL。有什么建议吗?


Before the problem was at Excel.XlFileFormat.xlWorkbookDefault, when Iv changed it to Normal it was doing well. Now it doesnt work. LOst much time to handle it on my own with google, but I failed.

在问题出现在Excel.XlFileFormat.xlWorkbookDefault之前,当Iv将其更改为Normal时,它表现良好。现在它不起作用。我有足够的时间自己处理谷歌,但我失败了。

oWB.SaveAs(MyFile + @"C:\SMS\XMLCopy.xls",
      Excel.XlFileFormat.xlWorkbookNormal, missing, missing,
      false, false, Excel.XlSaveAsAccessMode.xlNoChange,
      missing, missing, missing, missing, missing);

5 个解决方案

#1


3  

 string format = "yyyy_MM_dd_HH-mm";
        string pathtoexport = "C:\\DM\\SMS\\Przychodzace\\Znalezione_SMS_" + DateTime.Now.ToString(format) + ".xls";

        oWB.SaveAs(pathtoexport, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 

Now it works. Thanks for help.

现在它有效。感谢帮助。

#2


2  

Try with Excel.XlFileFormat.xlWorkbookDefault

尝试使用Excel.XlFileFormat.xlWorkbookDefault

#3


0  

I can't see anything obviously wrong with your code. Is it possible that there is some problem with accessing that path? If you try to save a file manually to that exact path (assuming that you're logged in under the same account as the interop process runs under), does it work?

我的代码看不出任何明显错误。访问该路径是否有可能存在问题?如果您尝试手动将文件保存到该确切路径(假设您在interop进程运行的同一帐户下登录),它是否有效?

Otherwise, are you using some kind of Excel template file or something that could have become corrupted?

否则,您使用某种Excel模板文件或可能已损坏的东西?

#4


0  

If you/your customer can afford, there is Aspose.Cells which is great for generating Excel documents from scratch, without the need for Office being installed on a machine.

如果您/您的客户可以负担得起,Aspose.Cells非常适合从头开始生成Excel文档,而无需在计算机上安装Office。

On the other hand, the solution costs money.

另一方面,解决方案需要花钱。

#5


0  

Looks to me like an Interop version conflict. Are you experiencing the problem on the same machine as it used to work on or have you moved to another? If you're on the same try to back track and figure out any changes or updates. If you're on another then verify that you have the correct version installed there.

在我看来就像Interop版本冲突。您是否在以前使用的同一台机器上遇到问题或者您是否已转移到另一台机器上?如果您正在尝试回溯并找出任何更改或更新。如果您在另一个上,请验证您是否安装了正确的版本。

This might sound stupid but I've encountered all sorts of wierdness when moving from dev -> test -> prod due to inconsistencies between the environments.

这可能听起来很愚蠢,但是当我从dev - > test - > prod移动时,由于环境之间的不一致,我遇到了各种各样的诡计。

If you haven't already, try removing the reference from the project and reapplying it.

如果您还没有,请尝试从项目中删除引用并重新应用它。

It could also be due to permissions (or lack there of). Have you tried creating a plain text file from the same piece of code?

这也可能是由于权限(或缺乏权限)。您是否尝试过使用同一段代码创建纯文本文件?

#1


3  

 string format = "yyyy_MM_dd_HH-mm";
        string pathtoexport = "C:\\DM\\SMS\\Przychodzace\\Znalezione_SMS_" + DateTime.Now.ToString(format) + ".xls";

        oWB.SaveAs(pathtoexport, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 

Now it works. Thanks for help.

现在它有效。感谢帮助。

#2


2  

Try with Excel.XlFileFormat.xlWorkbookDefault

尝试使用Excel.XlFileFormat.xlWorkbookDefault

#3


0  

I can't see anything obviously wrong with your code. Is it possible that there is some problem with accessing that path? If you try to save a file manually to that exact path (assuming that you're logged in under the same account as the interop process runs under), does it work?

我的代码看不出任何明显错误。访问该路径是否有可能存在问题?如果您尝试手动将文件保存到该确切路径(假设您在interop进程运行的同一帐户下登录),它是否有效?

Otherwise, are you using some kind of Excel template file or something that could have become corrupted?

否则,您使用某种Excel模板文件或可能已损坏的东西?

#4


0  

If you/your customer can afford, there is Aspose.Cells which is great for generating Excel documents from scratch, without the need for Office being installed on a machine.

如果您/您的客户可以负担得起,Aspose.Cells非常适合从头开始生成Excel文档,而无需在计算机上安装Office。

On the other hand, the solution costs money.

另一方面,解决方案需要花钱。

#5


0  

Looks to me like an Interop version conflict. Are you experiencing the problem on the same machine as it used to work on or have you moved to another? If you're on the same try to back track and figure out any changes or updates. If you're on another then verify that you have the correct version installed there.

在我看来就像Interop版本冲突。您是否在以前使用的同一台机器上遇到问题或者您是否已转移到另一台机器上?如果您正在尝试回溯并找出任何更改或更新。如果您在另一个上,请验证您是否安装了正确的版本。

This might sound stupid but I've encountered all sorts of wierdness when moving from dev -> test -> prod due to inconsistencies between the environments.

这可能听起来很愚蠢,但是当我从dev - > test - > prod移动时,由于环境之间的不一致,我遇到了各种各样的诡计。

If you haven't already, try removing the reference from the project and reapplying it.

如果您还没有,请尝试从项目中删除引用并重新应用它。

It could also be due to permissions (or lack there of). Have you tried creating a plain text file from the same piece of code?

这也可能是由于权限(或缺乏权限)。您是否尝试过使用同一段代码创建纯文本文件?