110,549
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Excel = Microsoft.Office.Interop.Excel;
using PPT = Microsoft.Office.Interop.PowerPoint;
using WORD = Microsoft.Office.Interop.Word;
using System.IO;
using System.Runtime.InteropServices;
namespace WebbrawsersAddExcel
{
public partial class WebBrawserUserControl : UserControl
{
public delegate void OpenComplete(object obj, OfficeType type);
/// <summary>
/// 设置打开完毕后的处理
/// </summary>
public OpenComplete openComplete;
private AxSHDocVw.AxWebBrowser axWebBrowser1;
public WebBrawserUserControl()
{
InitializeComponent();
//创建一个axwebbrawser
//System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(WebBrawserUserControl));
axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit();
// this.axWebBrowser1.Enabled = true;
//this.axWebBrowser1.Location = new System.Drawing.Point(0, 0);
this.Controls.Add(axWebBrowser1);
// this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState")));
this.axWebBrowser1.Dock = DockStyle.Fill;
//this.axWebBrowser1.Silent = true; 禁止弹出错误窗口
thOpen = new Thread(new ThreadStart(OpenOfficeByPath));
this.axWebBrowser1.NavigateComplete2 += new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(this.axWebBrowser1_NavigateComplete2);
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();
}
/// <summary>
/// 打开进程
/// </summary>
private Thread thOpen;
public string OpenPath = string.Empty;
/// <summary>
/// 打开指定的office文件
/// </summary>
/// <param name="path"></param>
public void OpenOffice(string path)
{
try
{
if (string.IsNullOrEmpty(path))
{
throw new NullReferenceException();
}
OpenPath = path;
if (thOpen.ThreadState == ThreadState.Stopped)
{
thOpen = new Thread(OpenOfficeByPath);
}
if (thOpen.ThreadState != ThreadState.Running)
{
thOpen.IsBackground = true;
thOpen.Start();
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 打开指定的文件
/// </summary>
private void OpenOfficeByPath()
{
try
{
lock (axWebBrowser1)
{
string s = OpenPath;
object sobj = (object)s;
object noObject = string.Empty;
object mm = "about:blank";
//先打开一个空的 再打开指定文件
this.axWebBrowser1.Navigate2(ref mm, ref noObject, ref noObject, ref noObject, ref noObject);
this.axWebBrowser1.Navigate2(ref sobj, ref noObject, ref noObject, ref noObject, ref noObject);
}
}
catch (Exception ex)
{
throw ex;
}
}
public void CloseWebBrowser()
{
try
{
object noObject = string.Empty;
object mm = "about:blank";
OpenPath = mm.ToString();
this.axWebBrowser1.Navigate2(ref mm, ref noObject, ref noObject, ref noObject, ref noObject);
}
catch (Exception ex)
{
throw ex;
}
}
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
// 结束 Excel 进程
public static void KillExcel(Excel.Application excel)
{
IntPtr t = new IntPtr(excel.Hwnd);
int k = 0;
GetWindowThreadProcessId(t, out k);
System.Diagnostics.Process p = System.Diagnostics.Process.GetProcessById(k);
p.Kill();
}
private OfficeType curType = OfficeType.OTHER;
/// <summary>
/// 赋值当前打开的对象
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
{
try
{
curType = CheckTypeByPath();
Document = axWebBrowser1.GetType().InvokeMember("Document", System.Reflection.BindingFlags.GetProperty, null, axWebBrowser1, null);
if (openComplete != null)
{
openComplete(Document, curType);
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 获取打开的文件类型
/// </summary>
/// <returns></returns>
public OfficeType CheckTypeByPath()
{
try
{
OfficeType offtype = OfficeType.OTHER;
string type = Path.GetExtension(OpenPath);
if (type.Equals(".xlsx") || type.Equals(".xls"))
{
offtype = OfficeType.EXCEL;
}
else if (type.Equals(".pptx") || type.Equals(".ppt"))
{
offtype = OfficeType.PPT;
}
else if (type.Equals(".docx") || type.Equals(".doc"))
{
offtype = OfficeType.WORD;
}
return offtype;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 当前打开的对象 excel=workbook ppt=presiontation word=document
/// </summary>
public object Document;
/// <summary>
/// 设置是否显示工具菜单
/// </summary>
/// <param name="showorhide"></param>
/// <returns></returns>
public void ToolBar(bool showorhide)
{
try
{
if (curType == OfficeType.OTHER) { return; }
//显示菜单
if (showorhide)
{
axWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER);
}
else
{
axWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER);
}
this.axWebBrowser1.Refresh2();
}
catch (Exception ex)
{
throw ex;
}
}
}
/// <summary>
/// 当前打开的文件类型
/// </summary>
public enum OfficeType
{
EXCEL,
PPT,
WORD,
OTHER
}
}