none
How to implement "Save As" functionality on .XLSX file using Open XML SDK? RRS feed

  • Question

  • Hi Folks,

    When downloading the .xlsx file from the internet, it is in locked/protected state. We have seen that if we "save as" the file to some other file let us say from abc.xlsx to def.xlsx, the protection is removed. We want to implement this functionality programmatically. We have looked at Interop option which works but is not good enough as this means we need to deploy Excel on all our test agents which is not possible and is not scalable. Is there a way using Open XML SDK to implement this functionality?

    Thanks,

    Karan


    Karan Vohra

    Friday, January 22, 2016 1:25 AM

Answers

  • Hi Karan,

    For this requirement, you just read the content to the stream and write to a new file.

    For example:

    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 is an article that can help you:

    # Working with In-Memory Open XML Documents

    https://msdn.microsoft.com/en-us/library/office/ee945362%28v=office.11%29.aspx?f=255&MSPPError=-2147217396

    Regards

    Starain


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Sunday, January 24, 2016 5:04 AM
    Moderator