导出到 word 并且具有一定的格式

来源:互联网 发布:php 字符串分割 编辑:程序博客网 时间:2024/05/23 00:00

1.因为导出到word 是将页面上的数据 必须变成HTML 格式

使用方法

 /// <summary>
        /// 生成Table的HTML
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private string CreateTableHtml(DataTable dt)
        {
            if (dt == null || dt.Rows.Count == 0)
            {
                return string.Empty;
            }
            StringBuilder strHtmlBuilder = new StringBuilder();
            //Table 标题
            strHtmlBuilder.Append("<table width='100%'><tr>");
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                strHtmlBuilder.Append(@"<td><b>" + dt.Columns[i].ColumnName + "</b></td>");
            }
            strHtmlBuilder.Append("</tr>");

            //数据列
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                strHtmlBuilder.Append("<tr>");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    strHtmlBuilder.Append("<td>" + ObjectToString(dt.Rows[i][j]) + "</td>");
                }
                strHtmlBuilder.Append("</tr>");
            }
            // table 尾行
            strHtmlBuilder.Append("</table>");
            return strHtmlBuilder.ToString();
        }

 

..................................///导出按钮事件

 protected void btnExportToWord_Click(object sender, EventArgs e)
        {
            try
            {
                ProjectComponent pjc = new ProjectComponent();
                string strProjectID = Request["ProjectID"];
                DataTable dtBase = pjc.GetItemsByKeyFilter(SPContext.Current.Web.Url, T_PROJECT, null, new KeyValuePair<string, object>("ID", strProjectID), "Counter");
                if (dtBase == null || dtBase.Rows.Count <= 0)
                {
                    return;
                }
                string strProjectTilte = ObjectToString(dtBase.Rows[0]["Title"]);
                DataTable dtSPrject = new ProjectSummaryComponent().GetAllSubProject(SPContext.Current.Web.Url, T_PROJECT, null, strProjectTilte);
                if (dtSPrject == null)
                {
                    dtSPrject = new DataTable();
                    dtSPrject.Columns.Add("ID");
                }

                DataTable dtSubPrject = dtSPrject.Clone();

                dtSubPrject.Rows.Add(strProjectID);
                for (int i = 0; i < dtSPrject.Rows.Count; i++)
                {
                    dtSubPrject.ImportRow(dtSPrject.Rows[i]);
                }

                StringBuilder sbSolution = new StringBuilder();
                sbSolution.AppendLine(@"<html><head><title></title> <style type='text/css'> table { border-collapse: collapse; }td{
                height: 26px;
                width: 100px;
                border: 1px solid #CCC;
            }
        </style></head><body>");
                for (int i = 0; i < dtSubPrject.Rows.Count; i++)
                {


                    // 标题
                    sbSolution.AppendLine("<div style='text-align:center;font-weight:bold;font-size:25px;'>");
                    string strProjectTitle = string.Empty;
                    strProjectID = ObjectToString(dtSubPrject.Rows[i]["ID"]);
                    DataTable dtProject = pjc.GetItemsByKeyFilter(SPContext.Current.Web.Url, T_PROJECT, null, new KeyValuePair<string, object>("ID", strProjectID), "Counter");
                    if (dtProject == null || dtProject.Rows.Count <= 0)
                    {
                        continue;

                    }
                    strProjectTitle = ObjectToString(dtProject.Rows[0]["Title"]);
                    sbSolution.AppendLine(strProjectTitle);
                    sbSolution.AppendLine("</div>");

                    //费用汇总
                    KeyValuePair<string, object> key = new KeyValuePair<string, object>("ProjectID", strProjectID);
                    DataTable dtMarkets = new ProjectComponent().GetItemsByKeyFilter(SPContext.Current.Web.Url, T_MARKET_SUMMARY, strProjectID, key, "Number");
                    if (dtMarkets != null && dtMarkets.Rows.Count > 0)
                    {
                        sbSolution.AppendLine("<div style='text-align:left;font-weight:bold; margin-top:15px;  margin-bottom:5px;'>");
                        sbSolution.AppendLine("费用汇总:");
                        sbSolution.AppendLine("</div>");
                        DataTable dtable = new DataTable();
                        dtable.Columns.Add("市场");
                        dtable.Columns.Add("城市");
                        dtable.Columns.Add("负责人");
                        dtable.Columns.Add("市场预算");
                        dtable.Columns.Add("实际发生");
                        // 获取项目负责人
                        SPListItemCollection items = new ProjectComponent().GetListItems(T_PROJECT, null, new KeyValuePair<string, object>("ID", strProjectID), "Counter");
                        string strResPersonName = string.Empty;
                        if (items != null && items.Count > 0)
                        {
                            SPFieldUserValueCollection users = items[0]["Leader"] as SPFieldUserValueCollection;
                            if (users != null)
                            {
                                List<string> lstUserName = new List<string>();
                                for (int j = 0; j < users.Count; j++)
                                {
                                    if (users[j].User!=null)
                                    {
                                        lstUserName.Add(users[j].User.Name);
                                    }                              
                                }
                                strResPersonName = string.Join(";", lstUserName.ToArray());
                            }
                        }
                      
                        // 市场总结获取数据
                        if (dtMarkets != null)
                        {
                            dtMarkets.Columns.Add("AssignToPA");
                            for (int k = 0; k < dtMarkets.Rows.Count; k++)
                            {
                                //dtMarkets.Rows[index]["AssignToPA"] = strResPersonName;
                                DataRow newRow = dtable.NewRow();
                                newRow["市场"] = dtMarkets.Rows[k]["Market"];
                                newRow["城市"] = dtMarkets.Rows[k]["Cities"];
                                newRow["负责人"] = strResPersonName;
                                newRow["市场预算"] = dtMarkets.Rows[k]["EstimatedCost"];
                                newRow["实际发生"] = dtMarkets.Rows[k]["ActualCost"];
                                dtable.Rows.Add(newRow);
                            }
                            sbSolution.AppendLine(CreateTableHtml(dtable));
                        }
                    }

                sbSolution.AppendLine("</body></html>");
                Response.ContentType = "application/msword";
                Response.ContentEncoding = Encoding.Default;
                Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strProjectTilte + "项目总结.doc", Encoding.UTF8));
                Response.Write(sbSolution.ToString());
                Response.End();
                Response.Flush();

            }
            catch (Exception ex)
            {
                Log.LogException("", ex);
            }
        }

 

 

 

原创粉丝点击