程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> C#代碼將html樣式文件轉為Word文檔,

C#代碼將html樣式文件轉為Word文檔,

編輯:關於.NET

C#代碼將html樣式文件轉為Word文檔,


首先有個這樣的需求,將以下網頁內容下載為Word文件。

html代碼:

<div class="modal-body">
    <div>ZY1703220001號標的開標結果為貴方中標,現通知如下:</td>
            </tr>
        </table>
        <table border="1" cellspacing="0" cellpadding="10">ZY1703220001</td>
                <td>25.725</td>
                <td>47500.00</td>
                <td>XX</td>
                <td>XXXXXX</td>
            </tr>
            <tr>
                <td colspan="6">備注:XXXXXX</td>
            </tr>
        </table>
        <table>

第一步:封裝一個方法

1:在控制器BiddingNoticeManageController創建一個DownBiddingNoticeModal方法。(采用的MVC模式)

2:根據id查詢當前中標信息(EF)

3:建一個中標通知書的html模板頁(數據字段自定義占位符)

  3-1:注:html模板中不需要<html>、<head>、<title>、<body>等標簽。只是單純的div布局標簽

  3-2:布局標簽中的樣式必須是內聯,就是寫在標簽中,不能寫在外部.css中。

4:通過Stream、StreamReader兩個類來讀取這個模板文件(返回的是html字符串)。

5:2中查詢出數據(對應字段)替換4中返回的html字符串中的占位符。

6:關鍵代碼

    StringBuilder sb = new StringBuilder();
            sb.Append(
                "<html xmlns:v=\"urn:schemas-microsoft-com:vml\"  xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\"xmlns = \"http://www.w3.org/TR/REC-html40\">");
            sb.Append(html);
            sb.Append("</html>");

7:用法:在頁面前端寫一個a標簽指向這個方法即可下載為Word文件了。

HTML模板文件:

<div class="modal-body">
    <div >
        <table >
            <tr>
                <td >中標通知書<hr /></td>
            </tr>
            <tr>
                <td >@BidderName</td>
            </tr>
            <tr>
                <td > “XXXX物資平台”@ResourceCode號標的開標結果為貴方中標,現通知如下:</td>
            </tr>
        </table>
        <table border="1" cellspacing="0" cellpadding="10" >
            <tr >
                <th>品名</th>
                <th>資源編號</th>
                <th>數量(@Unit)</th>
                <th>中標價格(含稅總金額:元)</th>
                <th>鋼廠</th>
                <th>存放地(提貨地)</th>
            </tr>
            <tr >
                <td>@ResourceName</td>
                <td>@ResourceCode</td>
                <td>@Count</td>
                <td>@TenderPrice</td>
                <td>@BrandName</td>
                <td>@InventoryPlace</td>
            </tr>
            <tr>
                <td colspan="6">備注:@Remarks</td>
            </tr>
        </table>
        <table >
            <tr>
                <td >
                    請貴方在收到通知書的5個工作日內交齊全額貨款並簽訂合同。
                </td>
            </tr>
            <tr>
                <td >
                    特此通知。
                </td>
            </tr>
            <tr>
                <td >
                    XXXX物資平台
                </td>
            </tr>
            <tr>
                <td >
                    @Year 年 @Moth 月 @Day 日
                </td>
            </tr>
        </table>

    </div>
</div>

 

       /// <summary>
        /// 下載中標通知書
        /// 用法:前端一個a標簽指向這個控制器的這個方法
        /// </summary>
        /// <param name="id">中標通知書Id</param>
        [AbpMvcAuthorize(BiddingNoticeAppPermissions.BiddingNotice)]

        public ActionResult DownBiddingNoticeModal(long id)
        {
            #region 讀取模板
            var html = GetBidTempStrng();
            #endregion

            #region 根據ID讀取中標內容 替換數據
            var model = _biddingNoticeRepository.FirstOrDefault(id);
            if (model != null)
            {
                html = html.Replace("@BrandName", model.BrandName).Replace("@ResourceCode", model.ResourceCode)
                    .Replace("@ResourceName", model.ResourceName).Replace("@Count", model.Count.ToString())
                    .Replace("@TenderPrice", model.TenderPrice.ToString()).Replace("@BidderName", model.BidderName)
                    .Replace("@InventoryPlace", model.InventoryPlace).Replace("@Remarks", model.Remarks)
                    .Replace("@Year", model.CreationTime.Year.ToString()).Replace("@Moth", model.CreationTime.Month.ToString())
                    .Replace("@Day", model.CreationTime.Day.ToString()).Replace("@Unit", model.Unit);
            }
            else
            {
                html = html.Replace("@BrandName", "XXXXX").Replace("@ResourceCode", "ZYXXXXXXXX")
                    .Replace("@ResourceName", "XXXXX").Replace("@Count", "0")
                    .Replace("@TenderPrice", "0").Replace("@BidderName", "XXXXX")
                    .Replace("@InventoryPlace", "XXXXX").Replace("@Remarks", "XXXXXXXX")
                    .Replace("@Year", "XXXX").Replace("@Moth", "XX")
                    .Replace("@Day", "XX").Replace("@Unit", "X");
            }
            #endregion

            #region 轉換為Word文檔樣式

            StringBuilder sb = new StringBuilder();
            sb.Append(
                "<html xmlns:v=\"urn:schemas-microsoft-com:vml\"  xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:w=\"urn:schemas-microsoft-com:office:word\" xmlns:m=\"http://schemas.microsoft.com/office/2004/12/omml\"xmlns = \"http://www.w3.org/TR/REC-html40\">");
            sb.Append(html);
            sb.Append("</html>");
            return File(Encoding.UTF8.GetBytes(sb.ToString()), "application/msword", $"中標通知書.doc");

            #endregion
        }

        /// <summary>
        /// 讀取中標通知書模板
        /// </summary>
        /// <returns></returns>
        private string GetBidTempStrng()
        {
            StringBuilder sbHtml = new StringBuilder();
            // 讀取模板替換數據
            var path = Server.MapPath("~/Common/BidTemplace/BidTemp.html");
            using (Stream inStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
            using (StreamReader outStream = new StreamReader(inStream, Encoding.Default))
            {
                while (!outStream.EndOfStream)
                {
                    sbHtml.Append(outStream.ReadLine());
                }
            }
            var html = sbHtml.ToString();
            return html;
        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved