简体   繁体   English

使用Excel.Interop C#保存覆盖和更改

[英]Save Overwrite and changes using Excel.Interop C#

All, 所有,

I have the below code which works okay, however I would like to save (Overwrite changes) instead of SaveAs. 我有下面的代码可以正常工作,但是我想保存(覆盖更改)而不是SaveAs。 To my knowledge the Close(True) Should achieve this however it still provides the user with a save as option. 就我所知,Close(True)应该可以实现这一点,但是它仍然为用户提供了另存为选项。 :S :S

Any help / recommendations on the below would be much appreciated. 在下面的任何帮助/建议将不胜感激。

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using Microsoft.Office.Interop.Excel;

namespace Dynamic.Script_8D58F1E2C23B36A
{
    // Script generated by Pega Robotics Studio 8.0.1072.0
    // Please use caution when modifying class name, namespace or attributes
    [OpenSpan.TypeManagement.DynamicTypeAttribute()]
    [OpenSpan.Design.ComponentIdentityAttribute("Script-8D58F1E2C23B36A")]
    public sealed class Script
    {
        public void deleterow(string WorkbookName2)
        {
            Microsoft.Office.Interop.Excel.Application myApp;
            Microsoft.Office.Interop.Excel.Workbook myWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet myWorkSheet;
            Microsoft.Office.Interop.Excel.Range range;
            myApp = new Microsoft.Office.Interop.Excel.Application();
            myWorkBook = myApp.Workbooks.Open(WorkbookName2, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            myWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)myWorkBook.Worksheets.get_Item(1);
            range = (Microsoft.Office.Interop.Excel.Range)myWorkSheet.Application.Rows[1, Type.Missing];
            range.Select();
            range.Delete(Microsoft.Office.Interop.Excel.XlDirection.xlUp);
            myWorkBook.Close(true);
            myApp.Quit();
        }
    }
}

So you can do this to stop DisplayAlerts from appearing: 因此,您可以执行以下操作以阻止DisplayAlerts出现:

myApp.DisplayAlerts = false;

And then if you want to save with specifying a file name, you can do this: 然后,如果要保存并指定文件名,则可以执行以下操作:

//specifying a file name
myWorkSheet.SaveAs(filename, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing, true, false, XlSaveAsAccessMode.xlNoChange, XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing);
myWorkBook.Close(true);

Or you can do it short hand by doing the following: 或者,您可以通过以下操作简化操作:

myWorkBook.Close(SaveChanges:=True, Filename:=YourFileDirectory/FileName)

If you just want to save a file that exists without specifying the file directory/file name. 如果只想保存一个存在的文件而不指定文件目录/文件名。 Just do this: 只要这样做:

myApp.DisplayAlerts = false;
myWorkBook.Save();
myWorkBook.Close(true);

You can "silence" Excel temporarily 您可以暂时“沉默” Excel

myApp.DisplayAlerts = false;
myWorkBook.Close(true);
myApp.DisplayAlerts = true;

Well and you should think about releasing your COM objects 好了,您应该考虑释放COM对象

Example: 例:

if (obj != null && Marshal.IsComObject(obj))
{
    Marshal.ReleaseComObject(obj);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM