示例地址 代码下载
其实很简单,就是用我上次说的那个使用c#打开word2007文档的的方法,用xslt格式化一下就可以了,不过,由于对ooxml格式不太了解,只分析了最简单的那几个标签,图片,表格等目前还不知道怎么格式化,有xslt高手可以指导一下,
代码如下,在在页面加多一个xml控件就可以了,嘿嘿,居然还能通过w3
1
static string savName = string.Empty;
2
3
4
protected void button1_Click(object sender, EventArgs e)
5
{
6
7
if (FileUpload1.HasFile)
8
{
9
if (FileUpload1.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
10
{
11
savName = Server.MapPath("/DocToDocx/" + FileUpload1.FileName);
12
FileUpload1.SaveAs(savName);
13
//有条件可以在服务器上安装office2007直接对2007以前的doc文档进行文档转换
14
//ConvertToDocx();
15
DispPlayOnWeb();
16
}
17
else
18
{
19
Response.Write("对不起,只能上传office2007以docx为后缀的文件");
20
}
21
}
22
}
23
24
//void ConvertToDocx()
25
//{
26
// object fileName = savName;
27
// object missing = Type.Missing;
28
// object isFalse = false;
29
// object isTrue = true;
30
// MSWord.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
31
// wordApp.Visible = false;
32
// MSWord.Document wordDocument = wordApp.Documents.Open(
33
// ref fileName,//文件名称
34
// ref isFalse,//如果文件有错误是否提示转换,我们在服务器运行,出现也看不到,就不用出现提示了
35
// ref isFalse,//是否以只读形式打开,我们要对其转换,不能只读
36
// ref isFalse,//是否增加到最精打开的文件
37
// ref missing,
38
// ref missing,
39
// ref isFalse,
40
// ref missing,
41
// ref missing,
42
// ref missing,
43
// ref missing,
44
// ref isFalse,
45
// ref isTrue,
46
// ref missing,
47
// ref isFalse,
48
// ref missing
49
// );
50
51
// //直接使用其转换方法转换文档
52
// wordDocument.Convert();
53
54
// wordDocument.Save();
55
56
// wordDocument.Close(ref missing, ref missing, ref missing);
57
58
// wordApp.Quit(ref missing, ref missing, ref missing);
59
// wordApp = null;
60
//}
61
62
void DispPlayOnWeb()
63
{
64
//string fileName = savName.Substring(0, savName.LastIndexOf('.')) + ".docx";
65
using (Package package = Package.Open(savName, FileMode.Open, FileAccess.Read))
66
{
67
Uri docxUri = new Uri("/word/document.xml", UriKind.Relative);
68
PackagePart docxPart = package.GetPart(docxUri);
69
70
XmlDocument docxXml = new XmlDocument();
71
docxXml.Load(docxPart.GetStream());
72
73
Xml1.Document = docxXml;
74
75
package.Close();
76
77
File.Delete(savName);
78
}
79
80
81
}
82
static string savName = string.Empty;2

3

4
protected void button1_Click(object sender, EventArgs e)5
{6

7
if (FileUpload1.HasFile)8
{9
if (FileUpload1.PostedFile.ContentType == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")10
{11
savName = Server.MapPath("/DocToDocx/" + FileUpload1.FileName);12
FileUpload1.SaveAs(savName);13
//有条件可以在服务器上安装office2007直接对2007以前的doc文档进行文档转换14
//ConvertToDocx();15
DispPlayOnWeb();16
}17
else18
{19
Response.Write("对不起,只能上传office2007以docx为后缀的文件");20
}21
}22
}23

24
//void ConvertToDocx()25
//{26
// object fileName = savName;27
// object missing = Type.Missing;28
// object isFalse = false;29
// object isTrue = true;30
// MSWord.ApplicationClass wordApp = new Microsoft.Office.Interop.Word.ApplicationClass();31
// wordApp.Visible = false;32
// MSWord.Document wordDocument = wordApp.Documents.Open(33
// ref fileName,//文件名称34
// ref isFalse,//如果文件有错误是否提示转换,我们在服务器运行,出现也看不到,就不用出现提示了35
// ref isFalse,//是否以只读形式打开,我们要对其转换,不能只读36
// ref isFalse,//是否增加到最精打开的文件37
// ref missing,38
// ref missing,39
// ref isFalse,40
// ref missing,41
// ref missing,42
// ref missing,43
// ref missing,44
// ref isFalse,45
// ref isTrue,46
// ref missing,47
// ref isFalse,48
// ref missing49
// );50

51
// //直接使用其转换方法转换文档52
// wordDocument.Convert();53

54
// wordDocument.Save();55

56
// wordDocument.Close(ref missing, ref missing, ref missing);57

58
// wordApp.Quit(ref missing, ref missing, ref missing);59
// wordApp = null;60
//}61

62
void DispPlayOnWeb()63
{64
//string fileName = savName.Substring(0, savName.LastIndexOf('.')) + ".docx";65
using (Package package = Package.Open(savName, FileMode.Open, FileAccess.Read))66
{67
Uri docxUri = new Uri("/word/document.xml", UriKind.Relative);68
PackagePart docxPart = package.GetPart(docxUri);69

70
XmlDocument docxXml = new XmlDocument();71
docxXml.Load(docxPart.GetStream());72

73
Xml1.Document = docxXml;74

75
package.Close();76

77
File.Delete(savName);78
}79

80

81
}82


浙公网安备 33010602011771号