コード例 #1
0
        // Embeds the add-in into a file of the specified type.
        public static void EmbedAddin(string fileType, MemoryStream memoryStream, string snippetID)
        {
            // Each Office file type has its own *Document class in the OOXML SDK.
            switch (fileType)
            {
            case "Excel":
                using (var spreadsheet = SpreadsheetDocument.Open(memoryStream, true))
                {
                    spreadsheet.DeletePart(spreadsheet.WebExTaskpanesPart);
                    var webExTaskpanesPart = spreadsheet.AddWebExTaskpanesPart();
                    OOXMLHelper.CreateWebExTaskpanesPart(webExTaskpanesPart, snippetID);
                }
                break;

            case "Word":
                using (var document = WordprocessingDocument.Open(memoryStream, true))
                {
                    var webExTaskpanesPart = document.AddWebExTaskpanesPart();
                    OOXMLHelper.CreateWebExTaskpanesPart(webExTaskpanesPart, snippetID);
                }
                break;

            case "PowerPoint":
                using (var slidedeck = PresentationDocument.Open(memoryStream, true))
                {
                    var webExTaskpanesPart = slidedeck.AddWebExTaskpanesPart();
                    OOXMLHelper.CreateWebExTaskpanesPart(webExTaskpanesPart, snippetID);
                }
                break;

            default:
                throw new Exception("Invalid File Type");
            }
        }
コード例 #2
0
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if (FileUploadControl.HasFile)
            {
                try
                {
                    // The OOXML SDK has different classes for the different Office
                    // file types, so the code needs to know which classes to use.
                    // So cache the Office file type; for example "Excel".
                    var file = new FileInfo(FileUploadControl.FileName);
                    Session["FileType"] = GetFileType(file);

                    if (Session["FileType"] != null)
                    {
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            memoryStream.Write(FileUploadControl.FileBytes, 0, FileUploadControl.FileBytes.Length);

                            // Validate the file. Throws exception if the file is not valid.
                            OOXMLHelper.ValidateOfficeFile(Session["FileType"].ToString(), memoryStream);

                            lblUploadOutcome.Text       = "File uploaded successfully";
                            Session["ByteArray"]        = FileUploadControl.FileBytes;
                            Session["UploadedFileName"] = FileUploadControl.FileName;
                        }
                    }
                    else
                    {
                        lblUploadOutcome.Text       = "You must choose an Office file with an extension of .xlsx, .docx, or .pptx.";
                        Session["ByteArray"]        = null;
                        Session["UploadedFileName"] = null;
                    }
                }
                catch (Exception ex)
                {
                    lblUploadOutcome.Text       = "ERROR: " + ex.Message.ToString();
                    Session["ByteArray"]        = null;
                    Session["UploadedFileName"] = null;
                }
            }
            else
            {
                lblUploadOutcome.Text       = "You have not specified a file.";
                Session["ByteArray"]        = null;
                Session["UploadedFileName"] = null;
            }
        }