Here I should create an AttachedTemplate object and assign a relationship id to it, which also implies Using (WordprocessingDocument doc = WordprocessingDocument.Open(templateStream, true))ĭoc.ChangeDocumentType(WordprocessingDocumentType.Document) TemplateStream.Write(templateBytes, 0, (int)templateBytes.Length) Using (MemoryStream templateStream = new MemoryStream())
Simplified, my code looks like this: byte result = null īyte templateBytes = System.IO.File.ReadAllBytes("template.dotx") I'm getting " Microsoft office cannot open this file because some parts are missing or invalid". Now, my problem is that if I use a MemoryStream and not a physical path to the file, the resulting docx is corrupted. What I don t understand is why should I reference the template file in the new document (Actually, inspecting the xml files in the docx archive, it just adds a xml node containing the name of the template. I don't want to use temporary files on disk and so on. Copy the array to a stream and open it as a WordProcessingDocument. Get my template from the DB as byte array.
Open the new file as a WordProcessingDocument, change its type to Document and add a reference to the template (I'm not sure of why is this necessary. So in the link above, the approach is this: Copy the template file in a new file on disk.
More exactly, I'm trying to follow this, but I want to open the document using a MemoryStream and not the path to the file. The "SLExcelUtility\SLExcelData.cs" file implements the data class to keep the data in the Excel file.Ĭopy Code using So this example web application shows you both reading from and writing to Excel files.
If the users click on the link, the web application will use the data in the session to recreate an Excel file and download it back to the user. The web application then shows the users a hyperlink. When the Excel file is uploaded, the application will read the data in the file and save the data in the web session. The purpose of the example web application is to allow users to upload the Excel file to the web server. The following picture shows the example Excel file attached in this article, which has the information of 1000 randomly generated students. The attached Visual Studio solution comes with the core SDK "", so you can simply download the attached solution and open it in your Visual Studio and compile to run the example application. You can download the complete Open XML SDK from here.
The "Views\Home\Index.aspx" is the application's view.The "Controllers\HomeController.cs" is the application's controller.The purpose to keep the example application in a lower version MVC and Visual Studio 2010 is to make the readers easier to download and run the example, because a lot of the readers do not have the most recent version of the Visual Studio. I am well aware that the current version of the ASP.Net MVC is 4. I am also aware that not all the people are using the most recent version.
To keep things simple, the example only reads the first sheet of the Excel files, and the Excel files created by the example also have only a single sheet. The example is a MVC 2 web application written in Visual Studio 2010. The article will show you an example to read from and write to Microsoft Excel files with the Open XML SDK.
This article presents an example on how to read and write Microsoft Excel with Open XML SDK.