简体   繁体   English

如何下载使用打开的XML SDK创建的docx文件?

[英]how to download docx file created with the open xml sdk?

Document.cs: Document.cs:

using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml;

namespace GeneratedCode
{
    public class GeneratedClass
    {
        // Creates an Document instance and adds its children.
        public Document GenerateDocument()
        {
            Document document1 = new Document(){ MCAttributes = new MarkupCompatibilityAttributes(){ Ignorable = "w14 w15 wp14" }  };
            document1.AddNamespaceDeclaration("wpc", "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas");
            document1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
            document1.AddNamespaceDeclaration("o", "urn:schemas-microsoft-com:office:office");
            document1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
            document1.AddNamespaceDeclaration("m", "http://schemas.openxmlformats.org/officeDocument/2006/math");
            document1.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");
            document1.AddNamespaceDeclaration("wp14", "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing");
            document1.AddNamespaceDeclaration("wp", "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing");
            document1.AddNamespaceDeclaration("w10", "urn:schemas-microsoft-com:office:word");
            document1.AddNamespaceDeclaration("w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
            document1.AddNamespaceDeclaration("w14", "http://schemas.microsoft.com/office/word/2010/wordml");
            document1.AddNamespaceDeclaration("w15", "http://schemas.microsoft.com/office/word/2012/wordml");
            document1.AddNamespaceDeclaration("wpg", "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup");
            document1.AddNamespaceDeclaration("wpi", "http://schemas.microsoft.com/office/word/2010/wordprocessingInk");
            document1.AddNamespaceDeclaration("wne", "http://schemas.microsoft.com/office/word/2006/wordml");
            document1.AddNamespaceDeclaration("wps", "http://schemas.microsoft.com/office/word/2010/wordprocessingShape");

            Body body1 = new Body();

            Paragraph paragraph1 = new Paragraph(){ RsidParagraphMarkRevision = "00100E91", RsidParagraphAddition = "009D5F75", RsidRunAdditionDefault = "00100E91" };

            ParagraphProperties paragraphProperties1 = new ParagraphProperties();

            ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();
            Languages languages1 = new Languages(){ Val = "en-US" };

            paragraphMarkRunProperties1.Append(languages1);

            paragraphProperties1.Append(paragraphMarkRunProperties1);

            Run run1 = new Run();

            RunProperties runProperties1 = new RunProperties();
            Languages languages2 = new Languages(){ Val = "en-US" };

            runProperties1.Append(languages2);
            Text text1 = new Text();
            text1.Text = "Hello";

            run1.Append(runProperties1);
            run1.Append(text1);
            BookmarkStart bookmarkStart1 = new BookmarkStart(){ Name = "_GoBack", Id = "0" };
            BookmarkEnd bookmarkEnd1 = new BookmarkEnd(){ Id = "0" };

            paragraph1.Append(paragraphProperties1);
            paragraph1.Append(run1);
            paragraph1.Append(bookmarkStart1);
            paragraph1.Append(bookmarkEnd1);

            SectionProperties sectionProperties1 = new SectionProperties(){ RsidRPr = "00100E91", RsidR = "009D5F75" };
            PageSize pageSize1 = new PageSize(){ Width = (UInt32Value)11906U, Height = (UInt32Value)16838U };
            PageMargin pageMargin1 = new PageMargin(){ Top = 1134, Right = (UInt32Value)850U, Bottom = 1134, Left = (UInt32Value)1701U, Header = (UInt32Value)708U, Footer = (UInt32Value)708U, Gutter = (UInt32Value)0U };
            Columns columns1 = new Columns(){ Space = "708" };
            DocGrid docGrid1 = new DocGrid(){ LinePitch = 360 };

            sectionProperties1.Append(pageSize1);
            sectionProperties1.Append(pageMargin1);
            sectionProperties1.Append(columns1);
            sectionProperties1.Append(docGrid1);

            body1.Append(paragraph1);
            body1.Append(sectionProperties1);

            document1.Append(body1);
            return document1;
        }


    }
}

I want to get the document created on this code (code from Open XML SDK). 我想在此代码(来自Open XML SDK的代码)上创建文档。

how by pressing the button to get ready document (get hello.docx)? 如何通过按下按钮准备好文档(获取hello.docx)?

Created project: asp.net mvc 4 add: 1) Controller 2) View-> code: 创建的项目:asp.net mvc 4添加:1)控制器2)视图->代码:

    @using (Html.BeginForm())
    {
        <input type="submit" value="Get docx file" />
    }

Thank you all. 谢谢你们。

The problem is solved. 问题已经解决了。

Method: 方法:

    public ActionResult GetFile()
    {
        try
        {
            byte[] document = new GeneratedClass().CreateDocumenBytes();
            return File(document, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Contract.docx");
        }
        catch (Exception ex)
        {
            ViewBag.ErrorMessage = ex.Message;
            throw;
        }
    }

View: 视图:

    @using (Html.BeginForm())
    {
        @Html.ActionLink("Экспорт договора в Word","GetFile","get")
    }

You simply need to create an action that returns a FileResult or a ContentResult . 您只需要创建一个返回FileResultContentResult Ie your action should make a call to 即您的操作应致电给

return File(...);

or 要么

return Content(...);

You must specify parameters for the file name, file content, encoding... Look at the documentation of: 您必须为文件名,文件内容,编码指定参数。请参阅以下文档:

[FileResult][1]
[ContentResult][2]

And the documentation of the Content and File methods of the Controller class (mentioned above). 以及Controller类ContentFile方法的文档(如上所述)。

If you want to do it using a submit button, your action must be decorated with [HttpPost] . 如果要使用提交按钮进行操作,则必须用[HttpPost]装饰您的动作。 You can also expose it as a link, and implement a GET action. 您还可以将其公开为链接,并实施GET操作。 In any case, you should send extra information so that the servers know whic document is being requested (form values, if you use POST, or Url parameters, if you use GET). 无论如何,您应该发送额外的信息,以便服务器知道正在请求哪个文档(如果使用POST,则为表单值;如果使用GET,则为Url参数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM