以下為程式碼有問題問我



  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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<%@ Page Title="首頁" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script language="javascript" type="text/javascript">
// <![CDATA[

function File1_onclick() {

}

// ]]>
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
歡迎使用 ASP.NET!
</h2>
<p>
若要進一步了解 ASP.NET,請造訪 <a href="http://www.asp.net" title="ASP.NET 網站">www.asp.net</a>
</p>
<p>
您也可以尋找 <a href="http://go.microsoft.com/fwlink/?LinkID=152368"
title="MSDN ASP.NET 文件">MSDN 上有關 ASP.NET 的文件</a>
</p>

<div>
<input id="File1" type="file" runat="server" onclick="return File1_onclick()" />
<asp:Button ID="btnConvert" runat="server" Text="转换" OnClick="btnConvert_Click" />
</div>

</asp:Content>










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;







namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{






protected void Page_Load(object sender, EventArgs e)
{

}


protected void btnConvert_Click(object sender, EventArgs e)
{
try
{


wordToHtml(File1);
}
catch (Exception ex)
{
throw ex;
}
finally
{
Response.Write("恭喜,转换成功!");
}

}


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;


Type docsType = docs.GetType();


string filePath = uploadWord(wordFilePath);


if (filePath == "0")
return "0";

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


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


if (!Directory.Exists(Server.MapPath("~\\html")))
{

Directory.CreateDirectory(Server.MapPath("~\\html"));
}


string ConfigPath = HttpContext.Current.Server.MapPath("html/" + filename + ".html");
object saveFileName = ConfigPath;


docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });




docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[] { null, null, null });


wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);

string text = System.IO.File.ReadAllText(ConfigPath, System.Text.Encoding.Default);
text = text.Replace("</head>", @"<link href=""./iie/css/rwd.css"" rel=""stylesheet"" type=""text/css""></head>");
text = text.Replace("<img", @"<img class=""rwd-img""");
text = text.Replace("<table class=MsoNormalTable", @"<table class=""rwd-table""");
System.IO.File.WriteAllText(ConfigPath, text, System.Text.Encoding.Default);

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
{

if (extendName == ".doc" || extendName == ".docx")
{
DateTime now = DateTime.Now;
newName = now.DayOfYear.ToString() + uploadFiles.PostedFile.ContentLength.ToString();

if (!Directory.Exists(Server.MapPath("~\\wordTmp")))
{

Directory.CreateDirectory(Server.MapPath("~\\wordTmp"));
}

uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName));
}
else
{
return "1";
}
}
catch
{
return "0";
}
return System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName);
}
else
{
return "0";
}
}







}
}



  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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;






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;







namespace WebApplication2
{
public partial class _Default : System.Web.UI.Page
{






protected void Page_Load(object sender, EventArgs e)
{

}


protected void btnConvert_Click(object sender, EventArgs e)
{
try
{


wordToHtml(File1);
}
catch (Exception ex)
{
throw ex;
}
finally
{
Response.Write("恭喜,转换成功!");
}

}


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;


Type docsType = docs.GetType();


string filePath = uploadWord(wordFilePath);


if (filePath == "0")
return "0";

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


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


if (!Directory.Exists(Server.MapPath("~\\html")))
{

Directory.CreateDirectory(Server.MapPath("~\\html"));
}


string ConfigPath = HttpContext.Current.Server.MapPath("html/" + filename + ".html");
object saveFileName = ConfigPath;


docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });




docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[] { null, null, null });


wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);

string text = System.IO.File.ReadAllText(ConfigPath, System.Text.Encoding.Default);
text = text.Replace("</head>", @"<link href=""./iie/css/rwd.css"" rel=""stylesheet"" type=""text/css""></head>");
text = text.Replace("<img", @"<img class=""rwd-img""");
text = text.Replace("<table class=MsoNormalTable", @"<table class=""rwd-table""");
System.IO.File.WriteAllText(ConfigPath, text, System.Text.Encoding.Default);

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
{

if (extendName == ".doc" || extendName == ".docx")
{
DateTime now = DateTime.Now;
newName = now.DayOfYear.ToString() + uploadFiles.PostedFile.ContentLength.ToString();

if (!Directory.Exists(Server.MapPath("~\\wordTmp")))
{

Directory.CreateDirectory(Server.MapPath("~\\wordTmp"));
}

uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName));
}
else
{
return "1";
}
}
catch
{
return "0";
}
return System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName);
}
else
{
return "0";
}
}







}
}

參考:
http://blog.csdn.net/happylee6688/article/details/8594339

http://blog.miniasp.com/post/2010/06/21/dot-net-4-Interop-type-cannot-be-embedded-Use-the-applicable-interface-instead.aspx
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()