Excel保存更改原件

Excel保存更改原件

本文介绍了使用Interop与C#,Excel保存更改原件。如何否定这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:加载excel电子表格模板。使用保存命令使用不同的文件名,然后退出互操作对象。这将最终保存原始模板文件。

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(date).xls

Original file opened is c:\testing\template.xlsThe 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互操作非常痛苦。我挖了一个老项目,做了一点点,我觉得这是你要找的。其他评论者是对的,但至少在我的经验中,如果您在VBA中使用了相同的对象(没有interop包装器),那么调用SaveAs()可能会比您预期的要多得多。

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.

这篇关于使用Interop与C#,Excel保存更改原件。如何否定这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:21