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;
            }
        }
时间: 2024-08-28 07:24:19

MVC4 导出word的相关文章

iText导出word入门

最近一个项目需要导出word文档,网上了解了一下,发现poi导出excel比较好,但是导出word就力不从心了,大家都比较推荐iText,于是试了试,感觉还不错. 我用的是2.1.7版本. 1. 使用maven导入依赖 <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> <

C#第三方Aspose.Words.dll导出Word(书签模板)方式说明

项目有遇到需要导出Word,在别人写好的基础上去修改样式,导出后发现样式不正确不整齐,于是采用模板的方式重新导出 1.模板word文件的制作,本人用office2013,在设计好需要的整个表格之后,在你需要替换的位置"插入"--书签 并命名,此命名需要在程序中进行替换 将做好的模板word文件放在程序目录下 2.引用Aspose.Words.dll 3.新建类WordOpAp.cs 1 public class WordOpAp 2 { 3 4 static public object

freemarker导出word——让表格数据行数 列数自动变化

行数.列数变化只需定义一个List<List<T>> freemarker遍历的话,只需要使用freemarker的标记性语言<#list report.qc_third_agentTable as  table2_tr>遍历即可,如图 实现的效果 freemarker导出word--让表格数据行数 列数自动变化,布布扣,bubuko.com

java导出word文档

使用freemarker模板导出word文档,用的比较多.这里也是采用的这种方式. 1  编辑一个word文件的模板,用于在程序中需要读入填充在模板中的数据先用字母代替,注意word版本为word2003或高于这个版本: 如:test.doc 2 把模板另存为xml文件: 如:test.xml 3 在xml中查找之前用字母代替的值 加上${} ,如${test}  ${guojia},把文件名改为test.ftl; 4 在项目的WebContent目录下新建一个文件夹,把test.xml放进去:

Java用freemarker导出word

最近一个项目要导出word文档,折腾老半天,发现还是用freemarker的模板来搞比较方便省事,现总结一下关键步骤,供大家参考,这里是一个简单的试卷生成例子. 一.模板的制作 先用Word做一个模板,如下图: (注意,上面是有表格的,我设置了边框不可见)然后另存为XML文件,之后用工具打开这个xml文件,有人用firstobject XML Editor感觉还不如notepad++,我这里用notepad++,主要是有高亮显示,和元素自动配对,效果如下: 上面黑色的地方基本是我们之后要替换的地

Npoi导出Word

Npoi导出Word 参考网上大神们笔记,完成Word导出. //创建文档 XWPFDocument doc = new XWPFDocument(); //标题 XWPFParagraph p1 = doc.CreateParagraph(); XWPFRun r1 = p1.CreateRun(); r1.SetBold(true); r1.FontSize = 23; r1.SetText("先进个人基本信息"); r1.SetTextPosition(30); CT_P doc

.net core 导出word文档

Npoi导出word(Peanuts) 标签: C#npoi导出word合并列列样式 2015-10-05 22:16 2896人阅读 评论(0) 收藏 举报  分类: C#(13)  版权声明:本文为博主原创文章,未经博主允许不得转载. 一个项目,要做一个从数据库读取数据,然后导出到word,因为涉及到后台数据库的读取,决定用npoi来导出word. NPOI源码地址:http://npoi.codeplex.com/ NPOI 2.0 api文档: http://www.npoi.info/

Java使用velocity导出word

效果展示: 使用word编辑好模板 先在模板中填充部分数据(之所以先写好两条数据,是为了在保存为xml用文本编辑器打开后快速定位到需要出传入的数据.增加不同颜色的两条数据,是为了判断颜色),然后保存文件为 然后再用普通的文本编辑器打开 以上是模板的创建. 导出后的word文档展示 开发过程记录: 1,  首先创建工程,导入 2,  创建模板文件,将修改好的xml文件用普通文本工作打开,然后复制到改文件中. 3,  新建 4,  执行该类,即可导出word. 代码展示: Person.java i

NPOI 2.0导出word(docx格式)

大名鼎鼎的NPOI用来导出EXCEL的文章园子里面有很多,可是用来导出WORD文档的文章大都含糊不清,最近刚好完成一个导出WORD文档的需求,在此分享下. NPOI里面认为word文档的最基本的结构是段落,代表这个段落的类就是XWPFParagraph,使用这个类可以设置段落里面的字体.大小.以及是否加粗等. 代表整个文档的的类XWPFDocument,是最外层的容器相当于一个word文档的实例,代表表格的类XWPFTable,是较小级别的段落容器相当于一个表格的实例. 一个XWPFDocume