用C#互操作使用时,Excel保存改变原来的。如何否定呢? [英] Using Interop with C#, Excel Save changing original. How to negate this?
问题描述
问题:加载一个Excel电子表格模板。 save命令来使用不同的文件名,然后退出互操作对象。这结束了保存原始模板文件。这并不是说是喜欢的结果。
The problem: Loading an excel spreadsheet template. Using the Save command with a different filename and then quitting the interop object. This ends up saving the original template file. Not the result that is liked.
public void saveAndExit(string filename)
{
excelApplication.Save(filename);
excelApplication.Quit();
}
原始文件打开是c:\testing\template.xls
所传入的文件名是C:\testing\7777(日期)的.xls
Original file opened is c:\testing\template.xls The file name that is passed in is c:\testing\7777 (date).xls
有没有人有答案
(我选择的答案是最正确的,彻底虽然wbk.Close()需要传递给它的参数。谢谢。)
(The answer I chose was the most correct and thorough though the wbk.Close() requires parameters passed to it. Thanks.)
推荐答案
Excel的互操作是相当痛苦的。我挖了一个旧的项目我有,做了一个小摆弄,我认为这是你在找什么。其他评论者是正确的,但是,至少在我的经验,有很多更多的调用另存为()不是,如果你已经使用了相同的对象(不通过Interop包装)你所期望的VBA。
Excel interop is pretty painful. I dug up an old project I had, did a little fiddling, and I think this is what you're looking for. The other commenters are right, but, at least in my experience, there's a lot more to calling SaveAs() than you'd expect if you've used the same objects (without the interop wrapper) in VBA.
Microsoft.Office.Interop.Excel.Workbook wbk = excelApplication.Workbooks[0]; //or some other way of obtaining this workbook reference, as Jason Z mentioned
wbk.SaveAs(filename, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing);
wbk.Close();
excelApplication.Quit();
爱是爱所有这些Type.Missings。但我认为他们是必要的。
Gotta love all those Type.Missings. But I think they're necessary.
这篇关于用C#互操作使用时,Excel保存改变原来的。如何否定呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!