一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - C# - 利用C#代碼將html樣式文件與Word文檔互換的方法

利用C#代碼將html樣式文件與Word文檔互換的方法

2022-02-12 16:20begrateful C#

這篇文章主要給大家介紹了關(guān)于利用C#代碼將html樣式文件與Word文檔互換的方法,文中通過(guò)示例代碼將兩種轉(zhuǎn)換介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。

一、c#代碼將html樣式文件轉(zhuǎn)為word文檔

首先有個(gè)這樣的需求,將以下網(wǎng)頁(yè)內(nèi)容下載為word文件。

html代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<div class="modal-body">
 <div style="height:600px;width:550px; margin:0 auto;">
 <table style="border-collapse:separate;border-spacing:10px;width: 100%">
  <tr>
  <td style="text-align: center;font-size: 30px;font-weight: bold">中標(biāo)通知書(shū)<hr /></td>
  </tr>
  <tr>
  <td style="text-align: left;font-size:20px;">xx</td>
  </tr>
  <tr>
  <td style="text-align: left"> “xxxx物資平臺(tái)”zy1703220001號(hào)標(biāo)的開(kāi)標(biāo)結(jié)果為貴方中標(biāo),現(xiàn)通知如下:</td>
  </tr>
 </table>
 <table border="1" cellspacing="0" cellpadding="10" style="border-collapse:separate;height: 300px;">
  <tr style="text-align:center;">
  <th>品名</th>
  <th>資源編號(hào)</th>
  <th>數(shù)量(噸)</th>
  <th>中標(biāo)價(jià)格(含稅總金額:元)</th>
  <th>鋼廠</th>
  <th>存放地(提貨地)</th>
  </tr>
  <tr style="text-align:center;">
  <td>冷軋窄帶</td>
  <td>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 style="border-collapse:separate;border-spacing:10px;width: 100%">
  <tr>
  <td style="text-align: left">
   請(qǐng)貴方在收到通知書(shū)的5個(gè)工作日內(nèi)交齊全額貨款并簽訂合同。
  </td>
  </tr>
  <tr>
  <td style="text-align: left">
   特此通知。
  </td>
  </tr>
  <tr>
  <td style="text-align: right">
   xxxx物資平臺(tái)
  </td>
  </tr>
  <tr>
  <td style="text-align:right">
   2017 年 4月 16 日
  </td>
  </tr>
 </table>
 
 </div>
</div>

樣式展示:

利用C#代碼將html樣式文件與Word文檔互換的方法

第一步:封裝一個(gè)方法

1:在控制器biddingnoticemanagecontroller創(chuàng)建一個(gè)downbiddingnoticemodal方法。(采用的mvc模式)

2:根據(jù)id查詢當(dāng)前中標(biāo)信息(ef)

3:建一個(gè)中標(biāo)通知書(shū)的html模板頁(yè)(數(shù)據(jù)字段自定義占位符)

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

  3-2:布局標(biāo)簽中的樣式必須是內(nèi)聯(lián),就是寫(xiě)在標(biāo)簽中,不能寫(xiě)在外部.css中。

4:通過(guò)stream、streamreader兩個(gè)類來(lái)讀取這個(gè)模板文件(返回的是html字符串)。

5:2中查詢出數(shù)據(jù)(對(duì)應(yīng)字段)替換4中返回的html字符串中的占位符。

6:關(guān)鍵代碼

?
1
2
3
4
5
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:用法:在頁(yè)面前端寫(xiě)一個(gè)a標(biāo)簽指向這個(gè)方法即可下載為word文件了。

html模板文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<div class="modal-body">
 <div style="height:600px;width:550px; margin:0 auto;">
 <table style="border-collapse:separate;border-spacing:10px;width: 100%">
  <tr>
  <td style="text-align: center;font-size: 30px;font-weight: bold">中標(biāo)通知書(shū)<hr /></td>
  </tr>
  <tr>
  <td style="text-align: left;font-size:20px;">@biddername</td>
  </tr>
  <tr>
  <td style="text-align: left"> “xxxx物資平臺(tái)”@resourcecode號(hào)標(biāo)的開(kāi)標(biāo)結(jié)果為貴方中標(biāo),現(xiàn)通知如下:</td>
  </tr>
 </table>
 <table border="1" cellspacing="0" cellpadding="10" style="border-collapse:separate;height: 300px;">
  <tr style="text-align:center;">
  <th>品名</th>
  <th>資源編號(hào)</th>
  <th>數(shù)量(@unit)</th>
  <th>中標(biāo)價(jià)格(含稅總金額:元)</th>
  <th>鋼廠</th>
  <th>存放地(提貨地)</th>
  </tr>
  <tr style="text-align:center;">
  <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 style="border-collapse:separate;border-spacing:10px;width: 100%">
  <tr>
  <td style="text-align: left">
   請(qǐng)貴方在收到通知書(shū)的5個(gè)工作日內(nèi)交齊全額貨款并簽訂合同。
  </td>
  </tr>
  <tr>
  <td style="text-align: left">
   特此通知。
  </td>
  </tr>
  <tr>
  <td style="text-align: right">
   xxxx物資平臺(tái)
  </td>
  </tr>
  <tr>
  <td style="text-align:right">
   @year 年 @moth 月 @day 日
  </td>
  </tr>
 </table>
 
 </div>
</div>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/// <summary>
/// 下載中標(biāo)通知書(shū)
/// 用法:前端一個(gè)a標(biāo)簽指向這個(gè)控制器的這個(gè)方法
/// </summary>
/// <param name="id">中標(biāo)通知書(shū)id</param>
[abpmvcauthorize(biddingnoticeapppermissions.biddingnotice)]
 
public actionresult downbiddingnoticemodal(long id)
{
 #region 讀取模板
 var html = getbidtempstrng();
 #endregion
 
 #region 根據(jù)id讀取中標(biāo)內(nèi)容 替換數(shù)據(jù)
 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 轉(zhuǎn)換為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", $"中標(biāo)通知書(shū).doc");
 
 #endregion
}
 
/// <summary>
/// 讀取中標(biāo)通知書(shū)模板
/// </summary>
/// <returns></returns>
private string getbidtempstrng()
{
 stringbuilder sbhtml = new stringbuilder();
 // 讀取模板替換數(shù)據(jù)
 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;
}

二、c# 將word文檔轉(zhuǎn)換為html

日常生活中,我們總是在word中進(jìn)行文字的編輯,它不僅能夠保存text文本,還可以保存文本的格式等等。那么如果我要將一word文檔上的內(nèi)容展示在網(wǎng)頁(yè)上,該怎么做呢?這里我提供了一個(gè)小工具,你可以將word轉(zhuǎn)換為html,需要顯示的話,可以直接訪問(wèn)該html,廢話不多說(shuō),下面看代碼。

頁(yè)面代碼:

?
1
2
3
4
<span style="font-size:18px;"><div>
<input id="file1" type="file" runat="server"/>
<asp:button id="btnconvert" runat="server" text="轉(zhuǎn)換" onclick="btnconvert_click" />
</div></span>

c#代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<span style="font-size:18px;" deep="5">using system;
using system.data;
using system.configuration;
using system.collections;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.io;
 
protected void page_load(object sender, eventargs e)
{
 
}
 
/// <summary>
/// 將word轉(zhuǎn)換為html
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnconvert_click(object sender, eventargs e)
{
try
{
 
//上傳
//uploadword(file1);
//轉(zhuǎn)換
wordtohtml(file1);
}
catch (exception ex)
{
throw ex;
}
finally
{
response.write("恭喜,轉(zhuǎn)換成功!");
}
 
}
 
//上傳文件并轉(zhuǎn)換為html wordtohtml(wordfilepath)
///<summary>
///上傳文件并轉(zhuǎn)存為html
///</summary>
///<param name="wordfilepath">word文檔在客戶機(jī)的位置</param>
///<returns>上傳的html文件的地址</returns>
public string wordtohtml(system.web.ui.htmlcontrols.htmlinputfile wordfilepath)
{
microsoft.office.interop.word.applicationclass word = new microsoft.office.interop.word.applicationclass();
type wordtype = word.gettype();
microsoft.office.interop.word.documents docs = word.documents;
 
// 打開(kāi)文件
type docstype = docs.gettype();
 
//應(yīng)當(dāng)先把文件上傳至服務(wù)器然后再解析文件為html
string filepath = uploadword(wordfilepath);
 
//判斷是否上傳文件成功
if (filepath == "0")
return "0";
//判斷是否為word文件
if (filepath == "1")
return "1";
 
object filename = filepath;
 
microsoft.office.interop.word.document doc = (microsoft.office.interop.word.document)docstype.invokemember("open",
system.reflection.bindingflags.invokemethod, null, docs, new object[] { filename, true, true });
 
// 轉(zhuǎn)換格式,另存為html
type doctype = doc.gettype();
 
string filename = system.datetime.now.year.tostring() + system.datetime.now.month.tostring() + system.datetime.now.day.tostring() +
system.datetime.now.hour.tostring() + system.datetime.now.minute.tostring() + system.datetime.now.second.tostring();
 
// 判斷指定目錄下是否存在文件夾,如果不存在,則創(chuàng)建
if (!directory.exists(server.mappath("~\\html")))
{
// 創(chuàng)建up文件夾
directory.createdirectory(server.mappath("~\\html"));
}
 
//被轉(zhuǎn)換的html文檔保存的位置
string configpath = httpcontext.current.server.mappath("html/" + filename + ".html");
object savefilename = configpath;
 
/*下面是microsoft word 9 object library的寫(xiě)法,如果是10,可能寫(xiě)成:
* doctype.invokemember("saveas", system.reflection.bindingflags.invokemethod,
* null, doc, new object[]{savefilename, word.wdsaveformat.wdformatfilteredhtml});
* 其它格式:
* wdformathtml
* wdformatdocument
* wdformatdostext
* wdformatdostextlinebreaks
* wdformatencodedtext
* wdformatrtf
* wdformattemplate
* wdformattext
* wdformattextlinebreaks
* wdformatunicodetext
*/
doctype.invokemember("saveas", system.reflection.bindingflags.invokemethod,
null, doc, new object[] { savefilename, microsoft.office.interop.word.wdsaveformat.wdformatfilteredhtml });
 
//關(guān)閉文檔
doctype.invokemember("close", system.reflection.bindingflags.invokemethod,
null, doc, new object[] { null, null, null });
 
// 退出 word
wordtype.invokemember("quit", system.reflection.bindingflags.invokemethod, null, word, null);
//轉(zhuǎn)到新生成的頁(yè)面
return ("/" + filename + ".html");
 
}
 
 
public string uploadword(system.web.ui.htmlcontrols.htmlinputfile uploadfiles)
{
if (uploadfiles.postedfile != null)
{
string filename = uploadfiles.postedfile.filename;
 
int extendnameindex = filename.lastindexof(".");
string extendname = filename.substring(extendnameindex);
string newname = "";
try
{
//驗(yàn)證是否為word格式
if (extendname == ".doc" || extendname == ".docx")
{
 
datetime now = datetime.now;
newname = now.dayofyear.tostring() + uploadfiles.postedfile.contentlength.tostring();
 
// 判斷指定目錄下是否存在文件夾,如果不存在,則創(chuàng)建
if (!directory.exists(server.mappath("~\\wordtmp")))
{
// 創(chuàng)建up文件夾
directory.createdirectory(server.mappath("~\\wordtmp"));
}
 
//上傳路徑 指當(dāng)前上傳頁(yè)面的同一級(jí)的目錄下面的wordtmp路徑
uploadfiles.postedfile.saveas(system.web.httpcontext.current.server.mappath("wordtmp/" + newname + extendname));
}
else
{
return "1";
}
}
catch
{
return "0";
}
//return "http://" + httpcontext.current.request.url.host + httpcontext.current.request.applicationpath + "/wordtmp/" + newname + extendname;
return system.web.httpcontext.current.server.mappath("wordtmp/" + newname + extendname);
}
else
{
return "0";
}
}</span>

效果圖:

轉(zhuǎn)換后的html文件

利用C#代碼將html樣式文件與Word文檔互換的方法

這樣就可以簡(jiǎn)單的在html中展示word文檔中的內(nèi)容,而不需要在自己進(jìn)行編輯了。當(dāng)然,如果有需要的話,可以將轉(zhuǎn)換的html的路徑存入數(shù)據(jù)庫(kù),根據(jù)不同的條件直接進(jìn)行訪問(wèn)。

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。   

原文鏈接:http://www.cnblogs.com/wendj/p/6699885.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩一区二区三区四区五区 | 国产麻豆精品免费视频 | 五月桃花网婷婷亚洲综合 | 欧美性4khd720 | 欠操h| 欧美一二区| 91久久碰国产| 久久国产精品无码视欧美 | 欧美在线视频 一区二区 | 欧美在线视频7777kkkk | 国内自拍网红在线综合 | 性xxxx直播放免费 | 久久精视频 | 四虎成人网 | 亚州综合网 | 女bbwxxxx非洲黑人 | 五月天婷婷精品免费视频 | 国产欧美一区二区精品性色99 | 亚洲伦理影院 | 精品欧美一区二区在线观看欧美熟 | 99国产精品 | 果冻传媒天美传媒乌鸦传媒 | 国产馆在线观看免费的 | 亚洲午夜精品久久久久久人妖 | 二区三区在线观看 | 国产久热香蕉在线观看 | 久久伊人免费 | 蹭蹭妈妈的朋友小说 | 色橹橹| 糖心在线观看网 | 二次元美女扒开内裤露尿口 | 星星动漫在线观看免费 | 欧洲久久| 经典三级四虎在线观看 | 嗯啊在线观看免费影院 | 久久99re热在线播放7 | 美女一级ba大片免色 | 憋尿调教绝望之岛 | 亚洲成人中文 | 香蕉成人国产精品免费看网站 | 人人做人人爽人人爱 |