关于在.net中嵌入word的一些小总结

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

关于在.net中嵌⼊word的⼀些⼩总结
第⼀种:是我⾃⼰⼀开始找这⽅⾯资料的最终⽬的,仅仅在⽹页⾥显⽰Word内容,⽽不显⽰⼯具栏
⼤概思想,转换word->html,然后在iframe中嵌⼊这个html
注意,都需要事先⾃⾏在⽬录下添加doc⽂件
还有问题请问⼀下⼤侠。

那个aoc中间件是怎么样的效果,谢谢
public class docx2html
{
public docx2html()
{
}
public void translate(string ServerPath,string filename)
{
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.ApplicationClass();
//Word.ApplicationClass word = new Word.ApplicationClass();
Type wordType = word.GetType();
Microsoft.Office.Interop.Word.Documents docs = word.Documents;
// 打开⽂件
Type docsType = docs.GetType();
object fileName = ServerPath + "\\doc\\" + filename + ".docx";
Microsoft.Office.Interop.Word.Document doc =
(Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",System.Reflection.BindingFlags.InvokeMethod,null, (object)docs,new Object[]{ fileName, true, true });
// 转换格式,另存为
Type docType = doc.GetType();
// 可以看到下⾯的路径和上⾯的路径不⼀样,因为如果也是放到⽂件夹⾥,在iframe⾥添加的也要加上“”但我试验的时候就是打不开~ object saveFileName = ServerPath + "\\"+filename+".html";
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,null, doc, new object[] {saveFileName,Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML});
// 退出 Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod,null, word, null);
}
}
上⾯是⽅法,注意要先“添加引⽤->com组件->Microsoft Word 12.0 Object Library” ,低版本的也⾏
如果没有,则表明的需要重新安装完整的office了。

调⽤⽅法,在page_load中加⼊以下代码
if (!IsPostBack)
{
string ServerPath = Server.MapPath("~");
string filename = "⽂件名";
docx2html d2h = new docx2html();
d2h.translate(ServerPath, filename);
}
然后,aspx⽂件中添加iframe代码
<iframe src="⽂件名.html"style="width: 693px; height: 489px"/>
⼩功告成
别的⽅法:我试验的都是跳出⼀个框,点确定后,打开⼀个word,就相当⼀个链接
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置⽤户代码以初始化页⾯
object Missing = Type.Missing;
//取得Word⽂件路径
string strTemp = "doc/test.doc";
//新Word⽂件保存路径
string newFileName = "doc/test2.doc";
//创建⼀个名为WordApp的组件对象
Application WordApp = new ApplicationClass();
//必须设置为不可见
WordApp.Visible = false;
try
{
//创建以strTemp为模板的⽂档
object oTemplate = Server.MapPath(strTemp);
Document WordDoc = WordApp.Documents.Add(ref oTemplate, ref Missing, ref Missing, ref Missing);
WordDoc.Activate();
//对标签"Title"进⾏填充
string strBM = "Title";
object objBM = strBM;
if (WordApp.ActiveDocument.Bookmarks.Exists(strBM) == true)
{
WordApp.ActiveDocument.Bookmarks.get_Item(ref objBM).Select();
WordApp.Selection.TypeText("公⽂标题");
}
//保存为新⽂件
object oNewFileName = Server.MapPath(newFileName);
WordDoc.SaveAs(ref oNewFileName, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref
Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing);
WordDoc.Close(ref Missing, ref Missing, ref Missing);
WordApp.Quit(ref Missing, ref Missing, ref Missing);
}
catch (Exception Ex)
{
throw new Exception(Ex.Message);
}
//浏览器弹出下载框
Page.RegisterStartupScript("", "<script>window.open('" + newFileName + "')</script>");
}
其他⽅法
private void Page_Load(object sender, System.EventArgs e)
{
//FileStream fs = new FileStream("c:\\1.doc", FileMode.Open, FileAccess.Read);
FileStream fs = new FileStream(Server.MapPath("doc\\test.doc"),FileMode.Open,FileAccess.Read); byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/msword";
//Response.OutputStream(bytes);
Response.OutputStream.Write(bytes, 0, bytes.Length);
Response.End();
}
还有~别的~⾛弯路的⼼⾎啊
protected void Page_Load(object sender, EventArgs e)
{
/*这⾥注释的代码可能也⾏,感兴趣的话可以⾃⾏试验~
string path = Server.MapPath("test.doc");
FileInfo file = new FileInfo(path);
FileStream myfileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
byte[] filedata = new Byte[file.Length];
myfileStream.Read(filedata, 0, (int)(file.Length));
myfileStream.Close();
Response.Clear();
Response.ContentType="application/msword";
Response.AddHeader("Content-Disposition", "attachment;filename=⽂件名.doc");
Response.Flush();
Response.BinaryWrite(filedata);
Response.End();
* */
Response.ContentType = "Application/msword";
string filepath = Server.MapPath("test.doc");
Response.WriteFile(filepath);
Response.End();
}
再来⼀条弯路
protected void Page_Load(object sender, EventArgs e)
{
try
{
//if (openFileDialog1.ShowDialog() == DialogResult.OK)
if(1==1)
{
//object file = "D:\\⽯油⾼层平台\\⽯油⾼层(New6)\\webpart\\UpLoadFile\\file.doc";
object file = "~\\doc\\test.doc";
object nullobj = System.Reflection.Missing.Value;
object filename = file;
object confirmConversions = Type.Missing;
object readOnly = Type.Missing;
object addToRecentFiles = Type.Missing;
object passwordDocument = Type.Missing;
object passwordTemplate = Type.Missing;
object revert = Type.Missing;
object writePasswordDocument = Type.Missing;
object writePasswordTemplate = Type.Missing;
object format = Type.Missing;
object encoding = Type.Missing;
object visible = Type.Missing;
object openConflictDocument = Type.Missing;
object openAndRepair = Type.Missing;
object documentDirection = Type.Missing;
object noEncodingDialog = Type.Missing;
object missing = Type.Missing;
Microsoft.Office.Interop.Word.ApplicationClass wordApp = new ApplicationClass(); Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(
ref filename,
ref confirmConversions,
ref readOnly,
ref addToRecentFiles,
ref passwordDocument,
ref passwordTemplate,
ref revert,
ref writePasswordDocument,
ref writePasswordTemplate,
ref format,
ref encoding,
ref visible,
ref openConflictDocument,
ref openAndRepair,
ref documentDirection,
ref noEncodingDialog);
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
IDataObject data = Clipboard.GetDataObject();
doc.Activate();
doc.Close(ref missing, ref missing, ref missing);
TextBox1.Text = data.GetData(DataFormats.Text).ToString();
}
}
catch (Exception ex)
{
//MessageBox.Show("错误提⽰:" + ex.Message);
}
}
* */
差不多了,还有⼀些,可能被我扔了,这些应该差不多了吧
Ps:谢谢发哥的帮助,谢谢
Pps:祝⽑⽑考雅思拿到7.5分。

相关文档
最新文档