using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Prototype
{
public partial class Form1 : Form
{
object oDocument;
Microsoft.Office.Interop.Word._Application wordApp;
Microsoft.Office.Interop.Word._Document doc;
public Form1()
{
InitializeComponent();
}
private void button9_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Office Documents " + " " + "(*.doc, *.docx)|*.doc;*.docx";
openFileDialog1.FilterIndex = 1;
System.Windows.Forms.HtmlDocument document;
string sFileName;
openFileDialog1.FileName = "";
openFileDialog1.ShowDialog();
sFileName = openFileDialog1.FileName;
if (sFileName.Length != 0)
{
oDocument = null;
webBrowser1.Navigate(sFileName);
document = webBrowser1.Document;
wordApp = webBrowser1.Document;
}
}
private void Form1_Load(object sender, EventArgs e)
{
/*
if (sender.Equals(button9))
{
openFileDialog1.Filter = "Office Documents " + " " + "(*.doc, *.docx)|*.doc;*.docx";
}
else
openFileDialog1.Filter = "Office Documents " + " " + "(*.doc, *.docx, *.xls, *.pptx, *.pdf, *.odt)|*.doc;*.docx;*.xls;*.pptx;*.pdf;*.odt"; ;
openFileDialog1.FilterIndex = 1;
*/
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
oDocument = webBrowser1.Document;
}
private void button8_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Office Documents " + " " + "(*.xls, *.xlsx)|*.xls;*.xlsx";
openFileDialog1.FilterIndex = 1;
System.Windows.Forms.HtmlDocument document;
string sFileName;
openFileDialog1.FileName = "";
openFileDialog1.ShowDialog();
sFileName = openFileDialog1.FileName;
if (sFileName.Length != 0)
{
oDocument = null;
webBrowser1.Navigate(sFileName);
document = webBrowser1.Document;
}
}
private void button7_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Office Documents " + " " + "(*.ppt, *.pptx)|*.ppt;*.pptx";
openFileDialog1.FilterIndex = 1;
System.Windows.Forms.HtmlDocument document;
string sFileName;
openFileDialog1.FileName = "";
openFileDialog1.ShowDialog();
sFileName = openFileDialog1.FileName;
if (sFileName.Length != 0)
{
oDocument = null;
webBrowser1.Navigate(sFileName);
document = webBrowser1.Document;
}
}
private void button10_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "Office Documents " + " " + "(*.pdf)|*.pdf";
openFileDialog1.FilterIndex = 1;
System.Windows.Forms.HtmlDocument document;
string sFileName;
openFileDialog1.FileName = "";
openFileDialog1.ShowDialog();
sFileName = openFileDialog1.FileName;
if (sFileName.Length != 0)
{
oDocument = null;
webBrowser1.Navigate(sFileName);
document = webBrowser1.Document;
}
}
}
}
我的代码使用网络浏览器来显示 word、excel 和 powerpoint。不幸的是,每次它试图打开它时,首先会出现一个警告框,询问我是要保存、打开还是取消。我应该如何删除它?
最佳答案
您遇到了浏览器安全问题。您不能在没有用户输入的情况下打开其中可能包含恶意项目的文档。
另外...您需要重构您的代码。您拥有的代码太多了,它们完全相同。
private string GetFileFromUser(string aFilter){
openFileDialog1.Filter = aFilter
openFileDialog1.FilterIndex = 1;
System.Windows.Forms.HtmlDocument document;
openFileDialog1.FileName = "";
openFileDialog1.ShowDialog();
return openFileDialog1.FileName;
}
fileName = GetFileFromUser("Office Documents (*.xls, *.xlsx)|*.xls;*.xlsx");
关于c# - 在 c# 中的网络浏览器中删除 word 文档中的警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9784197/