예제 #1
0
        /// <summary>
        /// 透過 HTML Tag 匯出 Word 文件
        /// </summary>
        /// <param name="result">回傳: 執行結果</param>
        /// <param name="msg">回傳: 訊息</param>
        /// <returns>串流資訊</returns>
        public byte[] ExportResumeByHtml(out bool result, out string msg)
        {
            result = true;
            msg    = "";
            MemoryStream ms = new MemoryStream();

            try
            {
                Spire.Doc.Document document = new Spire.Doc.Document();

                #region 文件內容

                //建立/取得要匯出的內容
                Resume        model = new Resume();
                StringBuilder html  = new StringBuilder();
                html.Append("Name: " + model.Name + "<br />");
                html.Append("Gender: " + model.Gender + "<br />");
                html.Append("Email: " + model.Email + "<br />");
                html.Append("Address: " + model.Address + "<br />");
                html.Append("Phone: " + model.Phone + "<br />");
                html.Append("Mobile: " + model.Mobile + "<br />");
                html.Append("Description1:<br />" + HttpUtility.HtmlDecode(model.Description1) + "<br /></p>");
                html.Append("Description2:<br />" + HttpUtility.HtmlDecode(model.Description2) + "<br /></p>");

                if (model.JobHistory.Count > 0)
                {
                    int i = 1;
                    model.JobHistory = model.JobHistory.OrderBy(x => x.StartDT).ToList();
                    html.Append("<p>簡歷</p>");
                    html.Append("<table><tr><th>項目</th><th>任職</th><th>職稱</th><th>開始時間</th><th>結束時間</th></tr>");
                    foreach (var h in model.JobHistory)
                    {
                        html.Append("<tr>");
                        html.Append("<td>" + i.ToString() + "</td>");
                        html.Append("<td>" + h.CompanyName + "</td>");
                        html.Append("<td>" + h.JobTitle + "</td>");
                        html.Append("<td>" + (h.StartDT.HasValue ? h.StartDT.Value.ToShortDateString() : "") + "</td>");
                        html.Append("<td>" + (h.EndDT.HasValue ? h.EndDT.Value.ToShortDateString() : "") + "</td>");
                        html.Append("</tr>");
                        i++;
                    }
                    html.Append("</table>");
                }

                #endregion

                //將 HTML 載入至 Document
                document.LoadHTML(new StringReader(html.ToString()), XHTMLValidationType.None);

                #region 設定樣式

                //一般段落文字
                ParagraphStyle style = new ParagraphStyle(document)
                {
                    Name = "BasicStyle"
                };
                //style.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Justify;
                style.CharacterFormat.FontName = "標楷體";
                style.CharacterFormat.FontSize = 12;
                document.Styles.Add(style);

                #endregion

                #region 套用樣式

                for (int s = 0; s < document.Sections.Count; s++)
                {
                    Spire.Doc.Section section = document.Sections[s];
                    //套用文章段落樣式
                    for (int p = 0; p < section.Paragraphs.Count; p++)
                    {
                        Spire.Doc.Documents.Paragraph pgh = section.Paragraphs[p];
                        pgh.ApplyStyle("BasicStyle");
                        pgh.Format.BeforeSpacing = 10;
                    }

                    //套用表格樣式
                    for (int t = 0; t < document.Sections[s].Tables.Count; t++)
                    {
                        Spire.Doc.Table table = (Spire.Doc.Table)document.Sections[s].Tables[t];
                        table.PreferredWidth            = new PreferredWidth(WidthType.Percentage, 100);
                        table.TableFormat.IsAutoResized = true;

                        //set table border
                        table.TableFormat.Borders.Right.BorderType      = Spire.Doc.Documents.BorderStyle.Thick;
                        table.TableFormat.Borders.Left.BorderType       = Spire.Doc.Documents.BorderStyle.Thick;
                        table.TableFormat.Borders.Top.BorderType        = Spire.Doc.Documents.BorderStyle.Thick;
                        table.TableFormat.Borders.Bottom.BorderType     = Spire.Doc.Documents.BorderStyle.Thick;
                        table.TableFormat.Borders.Horizontal.BorderType = Spire.Doc.Documents.BorderStyle.Thick;
                        table.TableFormat.Borders.Vertical.BorderType   = Spire.Doc.Documents.BorderStyle.Thick;

                        for (int tr = 0; tr < table.Rows.Count; tr++)
                        {
                            for (int td = 0; td < table.Rows[tr].Cells.Count; td++)
                            {
                                for (int t_ph = 0; t_ph < table.Rows[tr].Cells[td].Paragraphs.Count; t_ph++)
                                {
                                    table.Rows[tr].Cells[td].Paragraphs[t_ph].ApplyStyle("BasicStyle");
                                }
                            }
                        }
                    }
                }

                #endregion

                //匯出
                document.SaveToStream(ms, FileFormat.Docx);
            }
            catch (Exception ex)
            {
                msg    = ex.Message;
                result = false;
            }

            if (result)
            {
                return(ms.ToArray());
            }
            else
            {
                return(null);
            }
        }