openxml spreadsheat save-as
You could save that Spreadsheetdocument to another filename then open it to edit.
string sourceFile = "C:\Users\***.xlsx";
string destFile = "C:\Users\*****.xlsx";
System.IO.File.Copy(sourceFile, destFile, true);
using (SpreadsheetDocument spreadsheetDoc = SpreadsheetDocument.Open(destFile, true))
{
//remove some rows etc
//save workbookpart
}
References:How to: Copy, Delete, and Move Files and Folders (C# Programming Guide)
I strongly recommend ClosedXML - just used it for the first time and initially I've done an "Import from excel" functionality in few hours using standard Open XML 2.0 SDK - I've rewrite it in less then 15 minutes and also I've done the export to excel in 10 minutes using this tool.
Have a look at this library ClosedXML which is based on OpenXML; it simplifies a lot of document operations and gives you the SaveAs method. Here is an example of what you can do.
var workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add("Sample Sheet");
worksheet.Cell("A1").Value = "Hello World!";
workbook.SaveAs("HelloWorld.xlsx");
To my knowledge there is no built in way to change the filename, but since one way to edit the file is to use streams you can easily give the name of the file you want when writing out the contents of a stream:
byte[] byteArray = File.ReadAllBytes("C:\\temp\\oldName.xltx");
using (MemoryStream stream = new MemoryStream())
{
stream.Write(byteArray, 0, (int)byteArray.Length);
using (SpreadsheetDocument spreadsheetDoc = SpreadsheetDocument.Open(stream, true))
{
// Do work here
}
File.WriteAllBytes("C:\\temp\\newName.xlsx", stream.ToArray());
}
There are other ways to change the name as well, like when returning the file to a user in an ASP.NET MVC web application, but it depends on what you are trying to do.