使用Excel.Interop C#保存覆盖和更改 [英] Save Overwrite and changes using Excel.Interop C#
问题描述
全部
我有下面的代码可以正常工作,但是我想保存(覆盖更改)而不是SaveAs.就我所知,Close(True)应该可以实现这一点,但是它仍然为用户提供了另存为选项.:S
I have the below code which works okay, however I would like to save (Overwrite changes) instead of SaveAs. To my knowledge the Close(True) Should achieve this however it still provides the user with a save as option. :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();
}
}
}
推荐答案
因此您可以执行以下操作以阻止DisplayAlerts出现:
So you can do this to stop DisplayAlerts from appearing:
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);
这篇关于使用Excel.Interop C#保存覆盖和更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!