国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

JavaScript打開word文檔的實現代碼(c#)

2024-05-06 14:22:08
字體:
來源:轉載
供稿:網友
在C#中打開word文檔其實不算太難,方法也比較多。
一.C#中打開word文檔方法
代碼如下:
//在項目引用里添加上對Microsoft Word 11.0 object library的引用
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文檔
我們新建一個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,就可以打開了。
希望對你有幫助。
呵呵!~。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 清水县| 中江县| 西平县| 沙田区| 桃园市| 武穴市| 浦北县| 松潘县| 建水县| 抚顺市| 炎陵县| 建水县| 鄂州市| 苍山县| 阜阳市| 喀喇沁旗| 林甸县| 湾仔区| 樟树市| 时尚| 鹤峰县| 鄂尔多斯市| 屯留县| 图木舒克市| 申扎县| 神池县| 滕州市| 读书| 报价| 吴桥县| 东丽区| 徐水县| 新巴尔虎右旗| 宜良县| 沈丘县| 皮山县| 莱西市| 南皮县| 徐汇区| 大宁县| 仙居县|