MVC4 导出word

添加程序包 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;
            }
        }

 

转载于:https://www.cnblogs.com/yechangzhong-826217795/p/4213912.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值