I have an Excel 2007 spreadsheet that I edit with the OpenXML SDK 2. I remove some rows etc. I would like to know how to save that Spreadsheetdocument to another filename.
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
You could save that Spreadsheetdocument to another filename then open it to edit.
References:How to: Copy, Delete, and Move Files and Folders (C# Programming Guide)
Here's my solution for a save as, tested with complicated workbook that has macros and conditional formatting, etc. (this method is in a wrapper that has a SpreadsheetDocument property "Document"):
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.
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:
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.
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.
I don't know what version this functionality was introduced, but I use
OpenXmlPackage.SaveAs
, whereSpreadsheetDocument
, is a subclass ofOpenXmlPackage
.This function returns the new document, so you can switch to the copied one and apply your changes to it: