導出到 word 並且具有一定的格式

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

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章