添加程序包 DocX

using System.IO;
using Novacode;

  1. /// <summary>
  2. /// 导出Word
  3. /// </summary>
  4. public FileResult GetExportWord()
  5. {
  6. var items = _service.GetAll();
  7.  
  8. //string path = @"~/Content/Templates/Vote.docx";
  9. //string path = @"/Content/Templates/Vote.docx";
  10.  
  11. string path = Server.MapPath("/Content/Templates/Vote.docx");
  12. string fileName = "票决结果.docx";
  13. DocX doc;
  14. try
  15. {
  16. // Store a global reference to the loaded document.
  17. doc = DocX.Load(path);
  18. var table = doc.Tables[];
  19.  
  20. var row = table.Rows[];
  21. for (var i = ; i < items.Count; i++)
  22. {
  23. table.InsertRow(row, i + );
  24. }
  25. for (var i = ; i < items.Count; i++)
  26. {
  27. var item = items[i];
  28. var curRow = table.Rows[i + ];
  29.  
  30. curRow.Cells[].MarginTop = ;
  31. curRow.Cells[].MarginBottom = ;
  32. curRow.Cells[].Paragraphs[].InsertText((i + ).ToString());
  33. curRow.Cells[].MarginTop = ;
  34. curRow.Cells[].MarginBottom = ;
  35. curRow.Cells[].Paragraphs[].InsertText(item.Name ?? "");
  36. curRow.Cells[].MarginTop = ;
  37. curRow.Cells[].MarginBottom = ;
  38. curRow.Cells[].Paragraphs[].InsertText(item.Company ?? "");
  39. curRow.Cells[].MarginTop = ;
  40. curRow.Cells[].MarginBottom = ;
  41. curRow.Cells[].Paragraphs[].InsertText(item.Email ?? "");
  42. curRow.Cells[].MarginTop = ;
  43. curRow.Cells[].MarginBottom = ;
  44. curRow.Cells[].Paragraphs[].InsertText(item.Location ?? "");
  45. curRow.Cells[].MarginTop = ;
  46. curRow.Cells[].MarginBottom = ;
  47. curRow.Cells[].Paragraphs[].InsertText(item.Location ?? "");
  48. curRow.Cells[].MarginTop = ;
  49. curRow.Cells[].MarginBottom = ;
  50. curRow.Cells[].Paragraphs[].InsertText(item.Location ?? "");
  51. curRow.Cells[].MarginTop = ;
  52. curRow.Cells[].MarginBottom = ;
  53. curRow.Cells[].Paragraphs[].InsertText(item.Location ?? "");
  54. }
  55.  
  56. var stream = new MemoryStream();
  57. doc.SaveAs(stream);
  58. stream.Position = ;
  59.  
  60. return File(stream, "application/vnd.ms-word", fileName);
  61. }
  62.  
  63. // The template 'InvoiceTemplate.docx' does not exist, so create it.
  64. catch (Exception er)
  65. {
  66. throw er;
  67. }
  68. }

MVC4 导出word的更多相关文章

  1. java 导出word 并下载

    记录一下导出操作 源码: /************ * 导出word 并下载 * @param id 房号记录编号 * ***********************/ @RequestMappin ...

  2. Asp.net通过模板(.dot/Html)导出Word,同时导出图片

    一.Office组件导出Word(服务器配置麻烦) 需要引用Office的DLL,在下文的附件中,不同的Offic版本用的不一样,虽然高级版本可以兼容低级的,不过,还是统一版本最好 贴上核心代码(转载 ...

  3. 导出Excel And 导出word

      <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx. ...

  4. 使用NPOI2.1.3.1版本导出word附带表格和图片

    原文:http://www.cnblogs.com/afutureBoss/p/4074397.html?utm_source=tuicool&utm_medium=referral 最近项目 ...

  5. Java使用velocity导出word

    效果展示: 使用word编辑好模板

  6. 【吉光片羽】MVC 导出Word的两种方式

    1.直接将Html转成Word.MVC自带FileResult很好用.Html中我们也可以嵌入自己的样式. html: <div id="target"> <st ...

  7. java导出word的6种方式(复制来的文章)

    来自: http://www.cnblogs.com/lcngu/p/5247179.html 最近做的项目,需要将一些信息导出到word中.在网上找了好多解决方案,现在将这几天的总结分享一下. 目前 ...

  8. 【MVC】 非常简单的页面导出 WORD, EXCEL方法

    [MVC] 页面导出 WORD, EXCEL 前端 js function output() { var para = new Object(); para.html = getHtml(" ...

  9. 网页内容导出word/excel的js代码

    IE设置: 工具-> Internet选项-> 安全->自定义级别-> 对没有标记安全级别的ActiveX控件进行初始化  设为启用! 1.导出word //指定区域导出到Wo ...

随机推荐

  1. nodejs爬虫

    前言 几个月之前,有同事找我要PHP CI框架写的OA系统.他跟我说,他需要学习PHP CI框架,我建议他学习大牛写的国产优秀框架QeePHP. 我上QeePHP官网,发现官方网站打不开了,GOOGL ...

  2. HBase的二级索引,以及phoenix的安装(需再做一次)

    一:HBase的二级索引 1.讲解 uid+ts 11111_20161126111111:查询某一uid的某一个时间段内的数据 查询某一时间段内所有用户的数据:按照时间 索引表 rowkey:ts+ ...

  3. 【Java 进阶篇】【第二课】异常处理

    概念 异常处理的存在是为了:   允许程序员跳过无法处理的问题,继续开发后续的工作,或根据异常做出更加聪明的处理方式. Java中存在一类对象叫“异常对象”. 当异常情况发生时,就会暗战预先的设定,抛 ...

  4. zepto源码--fragment--学习笔记

    文档片段fragment函数默认传递三个参数: html文档片段字符串 name标签 properties额外添加的属性 函数内部实现过程: var dom, nodes, container; 中间 ...

  5. EChars学习-1

    Echarts,编写来自Enterprise Charts,商业级数据图表,是百度的一个开源的数据可视化工具 官网地址:http://echarts.baidu.com/ 一.引入Echarts &l ...

  6. MVC4怎么设置@Html.TextBoxFor这样的输入框的css样式

    在传统webForm中,输入框的这样的: <input id="userName" name="userName" type="text&quo ...

  7. https://www.zhihu.com/question/52020960#answer-47024535

    https://www.zhihu.com/question/52020960#answer-47024535

  8. RHEL6解决无法使用YUM源问题

    RHEL的YUM源需要注册用户才能更新使用,由于CentOS和RHEL基本没有区别,并且CentOS已经被REHL收购.所以将RHEL的YUM源替换为CentOS即可.问题如下:[root@bogon ...

  9. 加州wonders教材扫盲

    加州语文教材主要包含以下内容: 1.主教材Reading/Writing Workshop(读写研讨) 2.拓展教材Literature Anthology(文学选集) 3.延伸阅读材料Leveled ...

  10. ubuntu 更新 rtl8192cu 驱动

    ubuntu 更新 rtl8192cu 驱动 手上有个 tplink wr823n 的 usb 无线网卡,在 windows 下工作正常,但在 linux 上的比较差,速度没有 windows 上的快 ...