MVC4 匯出word

來源:互聯網
上載者:User

標籤:

添加程式包 DocX

using System.IO;
using Novacode;

        /// <summary>        /// 匯出Word        /// </summary>        public FileResult GetExportWord()        {            var items = _service.GetAll();            //string path = @"~/Content/Templates/Vote.docx";            //string path = @"/Content/Templates/Vote.docx";            string path = Server.MapPath("/Content/Templates/Vote.docx");            string fileName = "票決結果.docx";            DocX doc;            try            {                // Store a global reference to the loaded document.                doc = DocX.Load(path);                var table = doc.Tables[0];                var row = table.Rows[1];                for (var i = 1; i < items.Count; i++)                {                    table.InsertRow(row, i + 1);                }                for (var i = 0; i < items.Count; i++)                {                    var item = items[i];                    var curRow = table.Rows[i + 1];                    curRow.Cells[0].MarginTop = 5;                    curRow.Cells[0].MarginBottom = 5;                    curRow.Cells[0].Paragraphs[0].InsertText((i + 1).ToString());                    curRow.Cells[1].MarginTop = 5;                    curRow.Cells[1].MarginBottom = 5;                    curRow.Cells[1].Paragraphs[0].InsertText(item.Name ?? "");                    curRow.Cells[2].MarginTop = 5;                    curRow.Cells[2].MarginBottom = 5;                    curRow.Cells[2].Paragraphs[0].InsertText(item.Company ?? "");                    curRow.Cells[3].MarginTop = 5;                    curRow.Cells[3].MarginBottom = 5;                    curRow.Cells[3].Paragraphs[0].InsertText(item.Email ?? "");                    curRow.Cells[4].MarginTop = 5;                    curRow.Cells[4].MarginBottom = 5;                    curRow.Cells[4].Paragraphs[0].InsertText(item.Location ?? "");                    curRow.Cells[5].MarginTop = 5;                    curRow.Cells[5].MarginBottom = 5;                    curRow.Cells[4].Paragraphs[0].InsertText(item.Location ?? "");                    curRow.Cells[6].MarginTop = 5;                    curRow.Cells[6].MarginBottom = 5;                    curRow.Cells[4].Paragraphs[0].InsertText(item.Location ?? "");                    curRow.Cells[7].MarginTop = 5;                    curRow.Cells[7].MarginBottom = 5;                    curRow.Cells[4].Paragraphs[0].InsertText(item.Location ?? "");                }                var stream = new MemoryStream(256);                doc.SaveAs(stream);                stream.Position = 0;                return File(stream, "application/vnd.ms-word", fileName);            }                // The template ‘InvoiceTemplate.docx‘ does not exist, so create it.            catch (Exception er)            {                throw er;            }        }

 

MVC4 匯出word

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.