c# html 导出word

[CustomAuthorize]
        public FileResult ExportQuestionCenterWord(SearchBaseQuestion search)
        {
            StringWriter strWriter = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter);
            StringBuilder sb = null;
            string html = "";

try
            {
                search.PageSize = Int32.MaxValue;
                search.Status = DeleteMarkEnum.Active;
                search.isAnswer = IsAnswerEnum.Total;
                search.isReview = IsReviewEnum.Total;
                search.AddOrderBy("MODIFYDATE", SearchOrderType.Desc);
                
                if (search.SOURCE == "0")
                    search.SOURCE = "";
                if (search.MODULE == "0")
                    search.MODULE = "";
                if (search.QUESTIONTYPE == "0")
                    search.QUESTIONTYPE = "";
                if (search.QSTATUS == "0")
                    search.QSTATUS = "";
                if (search.ADMINID == "0")
                    search.ADMINID = "";
                if (!string.IsNullOrEmpty(search.IsSaleQuestion) && search.IsSaleQuestion == "1")
                    search.CUSTOMERMANAGERIDs = GetInPermissionCustomerMannagerIDs();
                 
                IList<BASE_QUESTION> baseQuestionList = questionManager.SearchQuestionList(search);

IList<BASE_CHOICECUSTOMERMANAGER> managerList = customerManagerService.GetChoiceCusManageList();
                SearchChoiceCusManager search_msg = new SearchChoiceCusManager();
                search_msg.SearchType = SearchTypeEnum.Total;
                search_msg.Status = DeleteMarkEnum.Active;
                search_msg.parentIdMany = "samedatakefu";
                IList<BASE_CHOICECUSTOMERMANAGER> choiceManagerList = choiceCusManagerManager.Search(search_msg);

if (baseQuestionList.Count > 0)
                {
                    foreach (var baseQuestion in baseQuestionList)
                    {
                        //获取受理人
                        string ADMINID_Str = "";
                        if (choiceManagerList != null && choiceManagerList.Count > 0)
                        {
                            BASE_CHOICECUSTOMERMANAGER em = choiceManagerList.Where(m => m.USERID == baseQuestion.ADMINID).FirstOrDefault();
                            if (em != null)
                            {
                                ADMINID_Str = em.NAME;
                            }
                        }
                        BASE_CHOICECUSTOMERMANAGER manager = managerList.FirstOrDefault(t => t.ID == baseQuestion.CUSTOMERMANAGERID);

string strSource = string.Empty;
                        switch (baseQuestion.SOURCE)
                        {
                            case 1:
                                strSource = "QQ";
                                break;
                            case 2:
                                strSource = "Email";
                                break;
                            case 3:
                                strSource = "Online";
                                break;
                            case 4:
                                strSource = "Sales";
                                break;
                            case 5:
                                strSource = "用户直通车";
                                break;
                            case 6:
                                strSource = "需求直通车";
                                break;
                            case 7:
                                strSource = "回访";
                                break;
                            case 8:
                                strSource = "QQ群";
                                break;
                            case 9:
                                strSource = "社区";
                                break;
                            case 10:
                                strSource = "微信群";
                                break;
                            default:
                                strSource = "全部";
                                break;
                        }

sb = new StringBuilder();
                        sb.Append("<table border=‘1‘ cellspacing=‘0‘ style=\"width;1200px;\">");
                        sb.Append("<tbody>");
                        //第一行
                        sb.Append("<tr align=‘center‘>");
                        sb.Append("<td colspan=‘3‘ style=‘width:1200px;‘>");
                        sb.Append(baseQuestion.TITLE == null ? StringConst.NAConst : baseQuestion.TITLE);
                        sb.Append("</td>");
                        sb.Append("</tr>");

//第二行
                        BASE_USER user = userManager.GetUserByUniqueId(baseQuestion.USERID);
                        if (user == null)
                            user = new BASE_USER();

sb.Append("<tr>");
                        sb.Append("<td style=‘width:400px;‘>");
                        sb.Append("姓名:" + user.REALNAME);
                        sb.Append("</td>");

sb.Append("<td style=‘width:400px;‘>");
                        sb.Append("账号:" + baseQuestion.USERNAME);
                        sb.Append("</td>");

sb.Append("<td style=‘width:400px;‘>");
                        sb.Append("联系方式:" + baseQuestion.MOBILE);
                        sb.Append("</td>");
                        sb.Append("</tr>");

//第三行
                        sb.Append("<tr>");
                        sb.Append("<td style=‘width:400px;‘>");
                        sb.Append("模块:" + (string.IsNullOrEmpty(baseQuestion.MODULE) ? "" : packageTreeCopy.GetOneById(baseQuestion.MODULE) == null ? "" : packageTreeCopy.GetOneById(baseQuestion.MODULE).FULLNAME));
                        sb.Append("</td>");

sb.Append("<td style=‘width:400px;‘>");
                        sb.Append("问题类型:" + AttributesUtils.GetEnumDescription<QuestionCenterTypeEnum>(baseQuestion.QUESTIONTYPE));
                        sb.Append("</td>");

sb.Append("<td style=‘width:400px;‘>");
                        sb.Append("状态:" + AttributesUtils.GetEnumDescription<QuestionCenterStatusEnum>(baseQuestion.STATUS));
                        sb.Append("</td>");
                        sb.Append("</tr>");

//第四行
                        sb.Append("<tr>");
                        sb.Append("<td style=‘width:400px;‘>");
                        sb.Append("受理人:" + ADMINID_Str);
                        sb.Append("</td>");

sb.Append("<td style=‘width:400px;‘>");
                        sb.Append("产品对接人:" + baseQuestion.PRODUCTMANAGERNAME);
                        sb.Append("</td>");

sb.Append("<td style=‘width:400px;‘>");
                        sb.Append("下次跟踪时间:" + (baseQuestion.NEXTVIEWDATE.ToString() == "0001-01-01 00:00:00" ? "" : baseQuestion.NEXTVIEWDATE.ToString()));
                        sb.Append("</td>");
                        sb.Append("</tr>");

//第五行
                        sb.Append("<tr>");
                        sb.Append("<td style=‘width:400px;‘>");
                        sb.Append("标签:" + baseQuestion.KEYWORD);
                        sb.Append("</td>");

sb.Append("<td colspan=‘2‘ style=‘width:800px;‘>");
                        sb.Append("来源:" + strSource);
                        sb.Append("</td>");
                        sb.Append("</tr>");

//第六行
                        sb.Append("<tr‘>");
                        sb.Append("<td colspan=‘3‘ style=‘width:1200px;‘>");
                        sb.Append("内容:" + baseQuestion.CONTENT);
                        sb.Append("</td>");
                        sb.Append("</tr>");

//第七行
                        string questionID = baseQuestion.ID.ToString();
                        BASE_ANSWER answer = answerManager.GetAnswerByQuestionsID(questionID);
                        sb.Append("<tr‘>");
                        sb.Append("<td colspan=‘3‘ style=‘width:1200px;‘>");
                        sb.Append("回复:" + (answer == null ? "" : HtmlDecode(answer.CONTENT)));
                        sb.Append("</td>");
                        sb.Append("</tr>");

sb.Append("</tbody>");
                        sb.Append("</table>");

sb.Append("<br/>");
                        sb.Append("<br/>");

html += sb.ToString();
                    }

strWriter.Write(html);
                }
                else
                {
                    strWriter.WriteLine("没有数据!");
                }
            }
            catch (Exception ex)
            {
                LogHelper.Instance.Error("ExportQuestionCenterWord error:", ex);
                strWriter.WriteLine("导出出现问题:" + ex.Message);
            }

byte[] fileContents = Encoding.GetEncoding("gb2312").GetBytes("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=gb2312\"><body>" + strWriter.ToString() + "</body></html>");
            return File(fileContents, "application/vnd.ms-word", string.Format("{0}.doc", "问题中心" + DateTime.Now.Date.ToString("yyyyMMdd")));
        }

时间: 2024-10-03 02:22:00

c# html 导出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

关于java中用itext导出word的一点想法

这几天在项目组只做了很少的事情,主要还是自己不认真地说.我的部分是要负责用itext导出word文档这一块,之前看到大佬们做出了EXCEL部分觉得很是惊奇,就像刚刚接触HTML一样的感觉.但是毕竟自己的任务就要好好的去完成,所以经过努力还是取得了一点的成果.的. 先上一个效果图 接下来就是代码了,码字不易,还有事情要做~~ 从前台利用Ajax传入到后台部分后,利用itext方法导出word,需要注意的是document.close和out.close这两个部分,没有close很容易出错. pub