如何使用C#保存Excel文件 [英] How to save excel files using c#
问题描述
亲爱的所有人,
我正在Windows应用程序上工作.在此应用程序中,我正在使用Office2007.我可以使用以下代码打开.xlsx文件.
Dear all,
I am working on windows application. In this application i am working office 2007. i can open .xlsx file using following code.
Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
excelApp.Visible = true;
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(varFileName,0, false, 5, """, """, false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, """,
true, false, 0, true, false, false);
Microsoft.Office.Interop.Excel.Worksheet workSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelWorkbook.ActiveSheet;
workSheet.Activate();
之后,我将对相同的xlsx文件进行一些修改.
现在问题开始于当我要保存文件时.
我正在使用以下代码进行此操作.
After that I will do some modification on same xlsx file.
Now problem start with, when I want to save the file.
I am doing this using following code.
excelApp.ActiveWorkbook.Save();
excelApp.ActiveWorkbook.Close((Object)false, (Object)oMissing, (Object)oMissing);
excelApp.Quit();
它显示异常,因为对象引用未设置为对象的实例.它不会显示任何活动文档.它显示ActiveWorkbook=null;
任何形式的链接,建议和特别的专家建议都将受到高度赞赏.
谢谢&问候,
Balkrishna Raut
It shows exception as Object reference not set to an instance of an object. It will not show any active document. It shows ActiveWorkbook=null;
Any kind of link, suggestion and specially expert advice would be highly appreciated.
Thanks & Regards,
Balkrishna Raut
推荐答案
工作簿中是否有类似Activate()的函数.然后您可能需要调用excelWorkbook.Activate()
,使该工作簿成为活动工作簿.
http://msdn.microsoft .com/en-us/library/microsoft.office.tools.excel.workbook.activate(v = VS.90).aspx [
Is there a function like Activate() for the workbook. ThenYou probably need to callexcelWorkbook.Activate()
to make that workbook the active one.
http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.workbook.activate(v=VS.90).aspx[^]
Cheers,
Manfred
< small>< strike>< u< i>< b>< pre> gt;& amp;< a href ="></a>< a href ="></a> [< a href =" target ="_ blank"><< ;/a>]//code//pre//b</i</u>/strike</big</small>
<small><big><strike><u><i><b><pre><code><>&<a href=""></a><a href=""></a>[<a href="" target="_blank"></a>]</code></pre></b></i></u></strike></big></small>
这篇关于如何使用C#保存Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!