Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
153 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...