发布网友
共2个回答
懂视网
private void button1_Click(object sender, System.EventArgs e)
{
//调用打开文件对话框获取要打开的文件WORD文件,RTF文件,文本文件路径名称
OpenFileDialog opd = new OpenFileDialog();
opd.InitialDirectory = "c:\\";
opd.Filter = "Word文档(*.doc)|*.doc|文本文档(*.txt)|*.txt|RTF文档(*.rtf)|*.rtf|所有文档(*.*)|*.*";
opd.FilterIndex = 1;
if (opd.ShowDialog() == DialogResult.OK && opd.FileName.Length > 0)
{
//建立Word类的实例,缺点:不能正确读取表格,图片等等的显示
Word.ApplicationClass app = new Word.ApplicationClass();
Word.Document doc = null;
object missing = System.Reflection.Missing.Value;
object FileName = opd.FileName;
object readOnly = false;
object isVisible = true;
object index = 0;
try
{
doc = app.Documents.Open(ref FileName, ref missing, ref readOnly,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref isVisible, ref missing,
ref missing, ref missing, ref missing);
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
//从剪切板获取数据
IDataObject data=Clipboard.GetDataObject();
this.richTextBox1.Text=data.GetData(DataFormats.Text).ToString();
}
finally
{
if (doc != null)
{
doc.Close(ref missing, ref missing, ref missing);
doc = null;
}
if (app != null)
{
app.Quit(ref missing, ref missing, ref missing);
app = null;[Page]
}
}
}
}
但是,如果我们怎么用javascript怎么打开呢?其实,也不难。
二.在javascript打开word文档 www.2cto.com
我们新建一个html文件,并且写一个FileUpLoad以及button控件。
<input id="flUpload" type="file" />flUpload
<input id="btnOpenFile" type="button" value="button" onclick="OpenFile()" />
然后,在写一个javascript OpenFile方法。
function OpenFile()
{
if (document.getElementById("flUpload").value.toUpperCase().indexOf(".XLS") != -1)
{
var objExcel;
objExcel = new ActiveXObject("Excel.Application");
objExcel.Visible = true;
objExcel.Workbooks.Open(document.getElementById("flUpload").value);
}
else if (document.getElementById("flUpload").value.toUpperCase().indexOf(".DOC") != -1)
{
var objDoc;
objDoc = new ActiveXObject("Word.Application");
objDoc.Visible = true;
objDoc.Documents.Open(document.getElementById("flUpload").value);
}
else
{
alert("Please select Word/Excel file only");
return false;
}
}
OK。然后 在IE中就能先选入一个doc文档,然后点open,就可以打开了。
希望对你有帮助。
热心网友
你看下下面这段代码!
<script language="javascript" type="text/javaScript">
function readWord(){
var worldObj=null; //操作Word的ActiveX对象
try{
wordObj=new ActiveXObject("Word.Application");
}
catch(e){
window.alert("创建ActiveX对象失败!");
return;
}
var doc=wordObj.Documents.open("E://test//月下独酌.doc"); //得到Word文档
doc.Windows(1).selection.WholeStory(); //选中整个文档
document.getElementById("content").innerText=doc.Windows(1).selection.Text;
//将文档的内容赋给页面中的元素
wordObj.activeDocument.Close(0); //关闭word文档
wordObj.Quit(); //退出ActiveX控件
/*
Documents.Open()方法有3个参数:目标文件路径,文件编辑器,以及是否已读写模式打开目标文件,如果后两个未指定,
系统将默认以当前系统中注册的编辑器以及读写模式打开目标文件
*/
}
</script>