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='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='400px;'>");
                        sb.Append("姓名:" + user.REALNAME);
                        sb.Append("</td>");

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

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

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

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

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

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

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

                        sb.Append("<td style='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='400px;'>");
                        sb.Append("标签:" + baseQuestion.KEYWORD);
                        sb.Append("</td>");

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

                        //第六行
                        sb.Append("<tr'>");
                        sb.Append("<td colspan='3' style='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='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")));
        }

原文地址:https://www.cnblogs.com/sojastudio/p/6709679.html