使用 openXML 下载文件

download File with openXML

我正在向这个控制器发出请求:

[ResponseType(typeof(Book))]
public async Task<IHttpActionResult> PostBook(Book book)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    string path = "C:\Users\af-costa\Desktop\TEST.docx";
    var stream = new MemoryStream();

    db.Books.Add(book);
    await db.SaveChangesAsync();

    // New code:
    // Load author name
    db.Entry(book).Reference(x => x.author).Load();

    var dto = new BookDTO()
    {
        Id = book.Id,
        Title = book.Title,
        AuthorName = book.author.name
    };

    Table customTable = createTable(book.author.name, book.Title);
    stream = createDocument(customTable);

    return CreatedAtRoute("DefaultApi", new { id = book.Id }, dto);
}

createDocument 为我检索了一个 memoryStream,我基本上需要从 openXml 获取的流上下载文件,在这个方法中:

private void createDocument(Table customTable)
{
    var stream = new MemoryStream();
    // Create a Wordprocessing document. 
    using (WordprocessingDocument package = WordprocessingDocument.Create("Test1", WordprocessingDocumentType.Document))
    {
        // Add a new main document part. 
        package.AddMainDocumentPart();

        // Create the Document DOM. 
        package.MainDocumentPart.Document =
          new Document(
            new Body(
              new Paragraph(
                new Run(
                  new Table(customTable)))));

        // Save changes to the main document part. 
        package.MainDocumentPart.Document.Save();
    }
    stream.Seek(0, SeekOrigin.Begin);
}

我不太清楚如何下载那个文件,基本上我想创建一个带有 table 的文档,并在用户执行请求时下载它 那个接入点。

使用您定义的MemoryStream创建word文档

private MemoryStream createDocument(Table customTable)
{
    var stream = new MemoryStream();

    // Create a Wordprocessing document. 
    using (WordprocessingDocument package = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document))
    {
        // Add a new main document part. 
        package.AddMainDocumentPart();

        // Create the Document DOM. 
        package.MainDocumentPart.Document =
          new Document(
            new Body(
              new Paragraph(
                new Run(
                  new Table(customTable)))));

        // Save changes to the main document part. 
        package.MainDocumentPart.Document.Save();
    }
    stream.Seek(0, SeekOrigin.Begin);
    return stream;
}

前面的函数解决了创建Word文件和缓冲数据的需求。现在我们需要获取缓冲数据并将其作为文本文件发送到客户端。以下代码段显示了这是如何完成的。

private void PageLoad(object sender, System.EventArgs e)
{
    System.IO.MemoryStream mstream = createDocument();
    byte[] byteArray = mstream.ToArray();
    mstream.Flush();
    mstream.Close();
    Response.Clear();
    // Add a HTTP header to the output stream that specifies the default filename
    // for the browser's download dialog
    Response.AddHeader("Content", "attachment; filename=foo.docx");
    // Add a HTTP header to the output stream that contains the 
    // content length(File Size). This lets the browser know how much data is being transfered
    Response.AddHeader("Content", byteArray.Length.ToString());
    // Set the HTTP MIME type of the output stream
    Response.ContentType = "application/octet-stream";
    // Write the data out to the client.
    Response.BinaryWrite(byteArray);
}

希望对您有所帮助