[CustomAuthorize]
        public FileResult ExportQuestionCenterExcel(SearchBaseQuestion search)
        {
            List<EXPORT_BASE_QUESTION> exportBaseQuestionList = new List<EXPORT_BASE_QUESTION>();

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);

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;
                    }

exportBaseQuestionList.Add(new EXPORT_BASE_QUESTION() {
                        TITLE = baseQuestion.TITLE == null ? StringConst.NAConst : baseQuestion.TITLE.Count() < 10 ? baseQuestion.TITLE : baseQuestion.TITLE.Substring(0, 10) + "..",
                        MODULE = string.IsNullOrEmpty(baseQuestion.MODULE) ? "" : packageTreeCopy.GetOneById(baseQuestion.MODULE) == null ? "" : packageTreeCopy.GetOneById(baseQuestion.MODULE).FULLNAME,
                        strQUESTIONTYPE = AttributesUtils.GetEnumDescription<QuestionCenterTypeEnum>(baseQuestion.QUESTIONTYPE),
                        USERNAME = baseQuestion.USERNAME,
                        REALNAME = baseQuestion.REALNAME,
                        SOURCE = strSource,
                        ADMINID_Str = ADMINID_Str,
                        strSTATUS = AttributesUtils.GetEnumDescription<QuestionCenterStatusEnum>(baseQuestion.STATUS),
                        JIRAPATH = string.IsNullOrEmpty(baseQuestion.JIRAPATH) ? "" : "<a href='" + JiraIP + baseQuestion.JIRAPATH.Trim() + "' target='blank'>" + baseQuestion.JIRAPATH.Trim() + "</a>",
                        JIRASTATUSName = AttributesUtils.GetEnumDescription<QuestionJiraStatusEnum>(baseQuestion.JIRASTATUS),
                        MODIFYDATE = baseQuestion.MODIFYDATE,
                        KEYWORD = baseQuestion.KEYWORD,
                        customerManagerName = manager == null ? "N/A" : manager.NAME,
                        CREATEDATE = baseQuestion.CREATEDATE,
                        OVERDUETYPE = AttributesUtils.GetEnumDescription<CrmAccountBuyType>(baseQuestion.OVERDUETYPE),
                    });
                }
            }
            catch (Exception ex)
            {
                LogHelper.Instance.Error("ExportQuestionCenterExcel error:", ex);
            }

GridView gvExport = new GridView();
            gvExport.DataSource = exportBaseQuestionList;
            gvExport.AllowPaging = false;
            gvExport.RowDataBound += new System.Web.UI.WebControls.GridViewRowEventHandler(dgExport_RowDataBound);
            gvExport.DataBind();

TableCell[] header = GetTableHeader();
            if (gvExport.HeaderRow != null)
            {
                gvExport.HeaderRow.Cells.AddRange(header);
                GridViewRow row = gvExport.HeaderRow;
                if (row != null)
                {
                    row.Cells[0].Visible = false;
                    row.Cells[1].Visible = false;
                    row.Cells[2].Visible = false;
                    row.Cells[3].Visible = false;
                    row.Cells[4].Visible = false;
                    row.Cells[5].Visible = false;
                    row.Cells[6].Visible = false;
                    row.Cells[7].Visible = false;
                    row.Cells[8].Visible = false;
                    row.Cells[9].Visible = false;
                    row.Cells[10].Visible = false;
                    row.Cells[11].Visible = false;
                    row.Cells[12].Visible = false;
                    row.Cells[13].Visible = false;
                    row.Cells[14].Visible = false;
                }
            }

StringWriter strWriter = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter);
            if (exportBaseQuestionList.Count > 0)
            {
                gvExport.RenderControl(htmlWriter);
            }
            else
            {
                strWriter.WriteLine("没有数据!");
            }

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

protected static void dgExport_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[6].Text = HtmlDecode(e.Row.Cells[6].Text);
                
                //创建时间
                e.Row.Cells[9].Text = e.Row.Cells[9].Text == "&nbsp;" ? "" : (string.IsNullOrEmpty(e.Row.Cells[9].Text) ? "" : Convert.ToDateTime(e.Row.Cells[9].Text).ToString("yyyy-MM-dd HH:mm:ss"));
                //最后处理时间
                e.Row.Cells[10].Text = e.Row.Cells[10].Text == "&nbsp;" ? "" : (string.IsNullOrEmpty(e.Row.Cells[10].Text) ? "" : Convert.ToDateTime(e.Row.Cells[10].Text).ToString("yyyy-MM-dd HH:mm:ss"));

if (e.Row.Cells[9].Text == "0001-01-01")
                {
                    e.Row.Cells[9].Text = "";
                }

if (e.Row.Cells[10].Text == "0001-01-01")
                {
                    e.Row.Cells[10].Text = "";
                }

e.Row.Cells[13].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
            }
        }

private TableCell[] GetTableHeader()
        {
            TableCell[] header = new TableCell[15];
            for (int i = 0; i < header.Length; i++)
            {
                header[i] = new TableHeaderCell();
            }

header[0].Text = "标题";
            header[1].Text = "来源";
            header[2].Text = "模块";
            header[3].Text = "类型";
            header[4].Text = "账号";
            header[5].Text = "真实姓名";
            header[6].Text = "JIRA地址";
            header[7].Text = "JIRA状态";
            header[8].Text = "账号的状态";
            header[9].Text = "创建时间";
            header[10].Text = "最后处理时间";
            header[11].Text = "受理人";
            header[12].Text = "状态";
            header[13].Text = "标签";
            header[14].Text = "客户经理</th>";

return header;
        }

}

也可以跨列

  header[0].ColumnSpan = 9;
            header[0].Text = "业务信息";

header[1].ColumnSpan = 6;
            header[1].Text = "购买信息";

header[2].ColumnSpan = 5;
            header[2].Text = "查账信息";

header[3].ColumnSpan = 7;
            header[3].Text = "开票信息";

header[4].ColumnSpan = 6;
            header[4].Text = "状态</th></tr><tr>";

//第二行
            header[5].Text = "订单号";

C#通过gridview导出excel的更多相关文章

  1. Asp.net Gridview导出Excel

    前台页面放一个GridView什么的就不说了,要注意的是在 <%@ Page Language="C#" AutoEventWireup="true" C ...

  2. GridView导出Excel的超好样例

    事实上网上有非常多关于Excel的样例,可是不是非常好,他们的代码没有非常全,读的起来还非常晦涩.经过这几天的摸索,最终能够完毕我想要导出报表Excel的效果了.以下是我的效果图. 一.前台的页面图 ...

  3. C#实现GridView导出Excel

    using System.Data;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System. ...

  4. ASP.NET gridview导出excel,防止繁体产生有乱码的方式

    //1.先引用比如 : using System; using System.Collections.Generic; using System.Linq; using System.Web; usi ...

  5. GridView导出excel格式问题

    在导出的点击事件中,代码如下: //指定导出对应单元格为文本样式 string style = @"<style> .test { vnd.ms-excel.numberform ...

  6. C# GridView 导出Excel表

    出错1:类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内解决方案:在后台文件中重载VerifyRenderingInServerForm方法,如 ...

  7. GridView导出Excel(中文乱码)

    public void OUTEXCEL(string items,string where) { DataSet ds = new StudentBLL().GetTable(items,where ...

  8. GridView导出Excel

    public void OUTEXCEL() { DataSet ds = new GW_T_DemandDAL().GetWzH(GetPersonInfoData(UserInfo), Reque ...

  9. GridView 导出Excel

    protected void btnExcel_Click(object sender, EventArgs e) { ) { ExportGridViewForUTF8(GridView1, Dat ...

随机推荐

  1. Spring入门第一课:Spring基础与配置Bean

    1.入门 Spring是简化java开发的一个框架,其中IoC和AOP是Spring的两个重要核心.由于Spring是非侵入性的,通过Ioc容器来管理bean的生命周期,还整合了许多其他的优秀框架,所 ...

  2. QT 防止FTP 上传软件在断连处 Crash

    前段时间发现项目中的上传FTP软件有可能会在从服务器申请断连时Crash, 所以加了一个Timer. 由于项目代码行数过大, 此处上传部分代码片段. timeoutTimer = new QTimer ...

  3. vue的$emit 与$on父子组件与兄弟组件的之间通信

    本文主要对vue 用$emit 与 $on 来进行组件之间的数据传输. 主要的传输方式有三种: 1.父组件到子组件通信 2.子组件到父组件的通信 3.兄弟组件之间的通信 一.父组件传值给子组件 父组件 ...

  4. Python对文件目录的操作

    python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd()返回指定目录下的所有文件和目录 ...

  5. docker-compose入门示例:一键部署 Nginx+Tomcat+Mysql

    整体环境配置 整体环境的配置,如果一个一个 Dockerfile 去写,那么是相当麻烦的,好在 Docker 有一个名为 Docker-Compose 的工具提供,我们可以使用它一次性完成整体环境的配 ...

  6. 《TCP/IP详解 卷1:协议》第3章 IP:网际协议

    3.1 引言 IP是TCP/IP协议族中最为核心的协议.所有的TCP.UDP.ICMP及IGMP数据都以IP数据报格式传输(见图1-4).许多刚开始接触TCP/IP的人对IP提供不可靠.无连接的数据报 ...

  7. Java异常链

    是什么 一种面向对象的编程技术,将捕获到的异常重新封装到一个新的异常中,并重新抛出. 有什么用 可以保留每一层的异常信息,用户查看异常的时候,能够从顶层异常信息看到底层异常信息. 怎么用 catch异 ...

  8. (数据科学学习手札45)Scala基础知识

    一.简介 由于Spark主要是由Scala编写的,虽然Python和R也各自有对Spark的支撑包,但支持程度远不及Scala,所以要想更好的学习Spark,就必须熟练掌握Scala编程语言,Scal ...

  9. R语言爬虫:爬取包含所有R包的名称及介绍

    第一种方法 library("rvest") page <- read_html("https://cran.rstudio.com/web/packages/av ...

  10. 引用ZXing生成二维码

    1.生成二维码 ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口. Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码.本文引用 ...