首页 文章

用openXML下载文件

提问于
浏览
0

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

[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);
}

我不知道如何下载该文件,基本上我想创建一个带有表的文档,并在用户对该访问点执行请求时下载它 .

1 回答

  • 2

    使用您定义的 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);
    }
    

    希望这可以帮助

相关问题