程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> Aspose.Words簡單生成word文檔,aspose.wordsword

Aspose.Words簡單生成word文檔,aspose.wordsword

編輯:關於.NET

Aspose.Words簡單生成word文檔,aspose.wordsword


Aspose.Words簡單生成word文檔

Aspose.Words.Document doc = new Aspose.Words.Document();
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(doc);

builder.Writeln("試卷一-Title");
builder.Writeln("試卷一-Des");
string subject = @"<p>111-1<img src='/UploadFiles/UEditor/image/20161206/6361662975800625528094831.jpg' title='QQ圖片20161107105516.jpg' _src='/UploadFiles/UEditor/image/20161206/6361662975800625528094831.jpg' alt='QQ圖片20161107105516.jpg' style='float: right;'/></p>";
builder.Writeln(TextNoHTML(subject));
string[] imgurls = GetHtmlImageUrlList(subject);
string imgtargeturl = "";
foreach (string imgurl in imgurls)
{
    imgtargeturl = Server.MapPath(imgurl);
    if (File.Exists(imgtargeturl))
    {
        builder.InsertImage(imgtargeturl, 400, 300);
    }
}
//builder.InsertImage(@"C:\Users\Sale\Pictures\1366-768壁紙\1357640083366.jpg",400,300);
doc.Save(@"D:\1.docx");

C#獲取純文本 去除html標簽

/// <summary>
/// 將html文本轉化為 文本內容方法NoHTML
/// </summary>
/// <param name="Htmlstring">HTML文本值</param>
/// <returns></returns>
public string TextNoHTML(string Htmlstring)
{
    //刪除腳本   
    Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
    //刪除HTML   
    Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"([/r/n])[/s]+", "", RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "/", RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "   ", RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "/xa1", RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "/xa2", RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "/xa3", RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "/xa9", RegexOptions.IgnoreCase);
    Htmlstring = Regex.Replace(Htmlstring, @"&#(/d+);", "", RegexOptions.IgnoreCase);
    //替換掉 < 和 > 標記
    Htmlstring.Replace("<", "");
    Htmlstring.Replace(">", "");
    Htmlstring.Replace("/r/n", "");
    //返回去掉html標記的字符串
    return Htmlstring;
}

C#獲取img src

/// <summary>  
/// 獲取Img的路徑  
/// </summary>  
/// <param name="htmlText">Html字符串文本</param>  
/// <returns>以數組形式返回圖片路徑</returns>  
public static string[] GetHtmlImageUrlList(string htmlText)
{
    Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase);
    //新建一個matches的MatchCollection對象 保存 匹配對象個數(img標簽)  
    MatchCollection matches = regImg.Matches(htmlText);
    int i = 0;
    string[] sUrlList = new string[matches.Count];
    //遍歷所有的img標簽對象  
    foreach (Match match in matches)
    {
        //獲取所有Img的路徑src,並保存到數組中  
        sUrlList[i++] = match.Groups["imgUrl"].Value;
    }
    return sUrlList;
}

 

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