c#如何打开EXCEL在程序界面上显示?
3个回答
展开全部
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
namespace InnderWord
{
public class ExcelEmbed:OfficeBase
{
public ExcelEmbed()
{
}
private int HandleId = 0;
private ApplicationClass exClass;
private Workbook wkBook;
private IntPtr exWnd = IntPtr.Zero;
private string FileName;
private void exClass_WorkbookBeforeClose(Workbook wkBook, ref bool bCancel)
{
}
/// <summary>
/// 加载word文件
/// </summary>
/// <param name="fileName">文件路径</param>
/// /// <param name="handleId">父窗口句柄ID</param>
public void LoadFile(string fileName, int handleId)
{
try
{
if (exClass == null)
{
exClass = new ApplicationClass();
}
FileName = fileName;
HandleId = handleId;
exClass.CommandBars.AdaptiveMenus = false;
exClass.WorkbookBeforeClose +=new AppEvents_WorkbookBeforeCloseEventHandler(exClass_WorkbookBeforeClose);
//exClass.docum
if (wkBook != null)
{
exClass.Workbooks.Close();
}
///加载文档
if (exWnd == IntPtr.Zero)
{
exWnd = WinAPI.FindWindow("XLMAIN", null);
WinAPI.SetParent(exWnd.ToInt32(), handleId);
//exClass.Workbooks.Add(true);
object filename = fileName;
object newTemplate = false;
object docType = 0;
object readOnly = true;
object isVisible = true;
object MissingValue=System.Reflection.Missing.Value;
if (exClass != null && exClass.Workbooks != null)
{
wkBook =exClass.Workbooks.Open(fileName,
MissingValue, MissingValue,MissingValue, MissingValue,
MissingValue, MissingValue,MissingValue,MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue);
}
if (wkBook == null)
{
//throw new Exception("Word文档加载失败!");
}
}
SetExcelStyle(handleId);
//wkBook.SendForReview();
//wkBook.SendForReview();
// wkBook.SheetBeforeDoubleClick+=new WorkbookEvents_SheetBeforeDoubleClickEventHandler(wkBook_SheetBeforeDoubleClick);
//wkBook.EndReview();
//wkBook.Activate();
// wkBook.CanCheckIn();
// wkBook.CheckIn();
//wkBook.ExclusiveAccess();
//wkBook.ForwardMailer();
// wkBook.NewWindow();
//wkBook.Route();
// wkBook.Worksheets.Application.ActivateMicrosoftApp();
//SetCommandBars();
//((Worksheet)exClass.Workbooks[1].Worksheets.get_Item(1)).get_Range("A1", "B2").Value = "";
// WinAPI.SendMessage(exWnd, WM_KEYDOWN, 65, 0);
// WinAPI.SendMessage(exWnd, WM_KEYUP, 65, 0);
//WinAPI.mouse_event(WinAPI.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
//System.Threading.Thread.Sleep(100);
// WinAPI.mouse_event(WinAPI.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
//IntPtr sheetWnd = WinAPI.FindWindow("XLDESK", null);//"XLDESK"
//WinAPI.SendMessage(sheetWnd, WM_KEYDOWN, 65, 0);
//WinAPI.SendMessage(sheetWnd, WM_KEYUP, 65, 0);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 设置打开word文档的样式
/// </summary>
/// <param name="exClass"></param>
private void SetExcelStyle(int handleId)
{
if (exClass != null)
{
//exClass.ActiveWindow.DisplayHorizontalScrollBar = false;
//exClass.ActiveWindow.DisplayFormulas = false;
//exClass.ActiveWindow.DisplayGridlines = false;
//exClass.ActiveWindow.DisplayHeadings = false;
//exClass.ActiveWindow.DisplayOutline = false;
//exClass.ActiveWindow.DisplayRightToLeft = false;
//exClass.ActiveWindow.DisplayZeros = false;
//exClass.ActiveWindow.DisplayVerticalScrollBar = false;
// exClass.ActiveWorkbook.AutoUpdateSaveChanges = true;
//exClass.AutoCorrect =
//exClass.AutoFormatAsYouTypeReplaceHyperlinks = true;
// exClass.AskToUpdateLinks = true;
//exClass.CalculationInterruptKey = XlCalculationInterruptKey.xlAnyKey;
// exClass.ActiveWindow.View = XlWindowView.xlNormalView;
// exClass.Workbooks[0].Activate();
exClass.Visible = true;
//wkBook.IsAddin = false;
//wkBook.HasRoutingSlip = true;
//wkBook.AutoUpdateFrequency = 1;
//wkBook.Activate();
wkBook.InactiveListBorderVisible = false;
WinAPI.SetWindowPos(exWnd, handleId, 1, 1,
Form.FromHandle((IntPtr)handleId).Bounds.Width,
Form.FromHandle((IntPtr)handleId).Bounds.Height,
SetWindowPosFlags.SWP_NOZORDER |
SetWindowPosFlags.SWP_NOMOVE |
SetWindowPosFlags.SWP_DRAWFRAME |
SetWindowPosFlags.SWP_NOSIZE);
OnResize(handleId);
}
}
/// <summary>
/// 设置word上面工具栏
/// </summary>
private void SetCommandBars()
{
if (exClass != null)
{
int count = exClass.CommandBars.Count;
for (int j = 1; j <= count; j++)
{
string menuName = exClass.CommandBars[j].Name;
if (menuName == "Standard")
{
int count_control = exClass.ActiveWindow.Application.CommandBars[j].Controls.Count;
for (int k = 1; k <= 2; k++)
{
exClass.ActiveWindow.Application.CommandBars[j].Controls[k].Enabled = false;
}
}
if (menuName == "Menu Bar")
{
exClass.ActiveWindow.Application.CommandBars[j].Enabled = false;
}
menuName = "";
}
///移除按钮
int hMenu = WinAPI.GetSystemMenu(exWnd, false);
if (hMenu > 0)
{
int menuItemCount = WinAPI.GetMenuItemCount(hMenu);
for (int m = 1; m <= 8; m++)
{
WinAPI.RemoveMenu(hMenu, menuItemCount - m, WinAPI.MF_REMOVE | WinAPI.MF_BYPOSITION);
}
WinAPI.DrawMenuBar(exWnd);
}
}
}
/// <summary>
/// 保存打开的文件
/// </summary>
public void Save()
{
if (wkBook != null)
{
wkBook.Save();
}
}
/// <summary>
/// 关闭打开的文件
/// </summary>
public void Close()
{
Save();
//exClass.Workbooks.Close();
if (wkBook != null)
{
exClass.Workbooks.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wkBook);
}
if (exClass != null)
{
exClass.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(exClass);
exClass = null;
}
GC.Collect();
}
/// <summary>
/// 当大小发生改变时
/// </summary>
public void OnResize(int handleId)
{
int borderWidth = SystemInformation.Border3DSize.Width;
int borderHeight = SystemInformation.Border3DSize.Height;
int captionHeight = SystemInformation.CaptionHeight;
int statusHeight = SystemInformation.ToolWindowCaptionHeight;
WinAPI.MoveWindow(
exWnd,
-2 * borderWidth,
-2 * borderHeight - captionHeight,
Form.FromHandle((IntPtr)handleId).Bounds.Width + 4 * borderWidth,
Form.FromHandle((IntPtr)handleId).Bounds.Height + captionHeight + 4 * borderHeight + statusHeight,
true);
}
/// <summary>
///文档关闭前
/// </summary>
/// <param name="doc"></param>
/// <param name="bCancel"></param>
private void exClass_wkBookBeforeClose(Workbook doc, ref bool bCancel)
{
Save();//确保文档关闭前能保存
bCancel = true;
}
}
}
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Windows.Forms;
namespace InnderWord
{
public class ExcelEmbed:OfficeBase
{
public ExcelEmbed()
{
}
private int HandleId = 0;
private ApplicationClass exClass;
private Workbook wkBook;
private IntPtr exWnd = IntPtr.Zero;
private string FileName;
private void exClass_WorkbookBeforeClose(Workbook wkBook, ref bool bCancel)
{
}
/// <summary>
/// 加载word文件
/// </summary>
/// <param name="fileName">文件路径</param>
/// /// <param name="handleId">父窗口句柄ID</param>
public void LoadFile(string fileName, int handleId)
{
try
{
if (exClass == null)
{
exClass = new ApplicationClass();
}
FileName = fileName;
HandleId = handleId;
exClass.CommandBars.AdaptiveMenus = false;
exClass.WorkbookBeforeClose +=new AppEvents_WorkbookBeforeCloseEventHandler(exClass_WorkbookBeforeClose);
//exClass.docum
if (wkBook != null)
{
exClass.Workbooks.Close();
}
///加载文档
if (exWnd == IntPtr.Zero)
{
exWnd = WinAPI.FindWindow("XLMAIN", null);
WinAPI.SetParent(exWnd.ToInt32(), handleId);
//exClass.Workbooks.Add(true);
object filename = fileName;
object newTemplate = false;
object docType = 0;
object readOnly = true;
object isVisible = true;
object MissingValue=System.Reflection.Missing.Value;
if (exClass != null && exClass.Workbooks != null)
{
wkBook =exClass.Workbooks.Open(fileName,
MissingValue, MissingValue,MissingValue, MissingValue,
MissingValue, MissingValue,MissingValue,MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue);
}
if (wkBook == null)
{
//throw new Exception("Word文档加载失败!");
}
}
SetExcelStyle(handleId);
//wkBook.SendForReview();
//wkBook.SendForReview();
// wkBook.SheetBeforeDoubleClick+=new WorkbookEvents_SheetBeforeDoubleClickEventHandler(wkBook_SheetBeforeDoubleClick);
//wkBook.EndReview();
//wkBook.Activate();
// wkBook.CanCheckIn();
// wkBook.CheckIn();
//wkBook.ExclusiveAccess();
//wkBook.ForwardMailer();
// wkBook.NewWindow();
//wkBook.Route();
// wkBook.Worksheets.Application.ActivateMicrosoftApp();
//SetCommandBars();
//((Worksheet)exClass.Workbooks[1].Worksheets.get_Item(1)).get_Range("A1", "B2").Value = "";
// WinAPI.SendMessage(exWnd, WM_KEYDOWN, 65, 0);
// WinAPI.SendMessage(exWnd, WM_KEYUP, 65, 0);
//WinAPI.mouse_event(WinAPI.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
//System.Threading.Thread.Sleep(100);
// WinAPI.mouse_event(WinAPI.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
//IntPtr sheetWnd = WinAPI.FindWindow("XLDESK", null);//"XLDESK"
//WinAPI.SendMessage(sheetWnd, WM_KEYDOWN, 65, 0);
//WinAPI.SendMessage(sheetWnd, WM_KEYUP, 65, 0);
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 设置打开word文档的样式
/// </summary>
/// <param name="exClass"></param>
private void SetExcelStyle(int handleId)
{
if (exClass != null)
{
//exClass.ActiveWindow.DisplayHorizontalScrollBar = false;
//exClass.ActiveWindow.DisplayFormulas = false;
//exClass.ActiveWindow.DisplayGridlines = false;
//exClass.ActiveWindow.DisplayHeadings = false;
//exClass.ActiveWindow.DisplayOutline = false;
//exClass.ActiveWindow.DisplayRightToLeft = false;
//exClass.ActiveWindow.DisplayZeros = false;
//exClass.ActiveWindow.DisplayVerticalScrollBar = false;
// exClass.ActiveWorkbook.AutoUpdateSaveChanges = true;
//exClass.AutoCorrect =
//exClass.AutoFormatAsYouTypeReplaceHyperlinks = true;
// exClass.AskToUpdateLinks = true;
//exClass.CalculationInterruptKey = XlCalculationInterruptKey.xlAnyKey;
// exClass.ActiveWindow.View = XlWindowView.xlNormalView;
// exClass.Workbooks[0].Activate();
exClass.Visible = true;
//wkBook.IsAddin = false;
//wkBook.HasRoutingSlip = true;
//wkBook.AutoUpdateFrequency = 1;
//wkBook.Activate();
wkBook.InactiveListBorderVisible = false;
WinAPI.SetWindowPos(exWnd, handleId, 1, 1,
Form.FromHandle((IntPtr)handleId).Bounds.Width,
Form.FromHandle((IntPtr)handleId).Bounds.Height,
SetWindowPosFlags.SWP_NOZORDER |
SetWindowPosFlags.SWP_NOMOVE |
SetWindowPosFlags.SWP_DRAWFRAME |
SetWindowPosFlags.SWP_NOSIZE);
OnResize(handleId);
}
}
/// <summary>
/// 设置word上面工具栏
/// </summary>
private void SetCommandBars()
{
if (exClass != null)
{
int count = exClass.CommandBars.Count;
for (int j = 1; j <= count; j++)
{
string menuName = exClass.CommandBars[j].Name;
if (menuName == "Standard")
{
int count_control = exClass.ActiveWindow.Application.CommandBars[j].Controls.Count;
for (int k = 1; k <= 2; k++)
{
exClass.ActiveWindow.Application.CommandBars[j].Controls[k].Enabled = false;
}
}
if (menuName == "Menu Bar")
{
exClass.ActiveWindow.Application.CommandBars[j].Enabled = false;
}
menuName = "";
}
///移除按钮
int hMenu = WinAPI.GetSystemMenu(exWnd, false);
if (hMenu > 0)
{
int menuItemCount = WinAPI.GetMenuItemCount(hMenu);
for (int m = 1; m <= 8; m++)
{
WinAPI.RemoveMenu(hMenu, menuItemCount - m, WinAPI.MF_REMOVE | WinAPI.MF_BYPOSITION);
}
WinAPI.DrawMenuBar(exWnd);
}
}
}
/// <summary>
/// 保存打开的文件
/// </summary>
public void Save()
{
if (wkBook != null)
{
wkBook.Save();
}
}
/// <summary>
/// 关闭打开的文件
/// </summary>
public void Close()
{
Save();
//exClass.Workbooks.Close();
if (wkBook != null)
{
exClass.Workbooks.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wkBook);
}
if (exClass != null)
{
exClass.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(exClass);
exClass = null;
}
GC.Collect();
}
/// <summary>
/// 当大小发生改变时
/// </summary>
public void OnResize(int handleId)
{
int borderWidth = SystemInformation.Border3DSize.Width;
int borderHeight = SystemInformation.Border3DSize.Height;
int captionHeight = SystemInformation.CaptionHeight;
int statusHeight = SystemInformation.ToolWindowCaptionHeight;
WinAPI.MoveWindow(
exWnd,
-2 * borderWidth,
-2 * borderHeight - captionHeight,
Form.FromHandle((IntPtr)handleId).Bounds.Width + 4 * borderWidth,
Form.FromHandle((IntPtr)handleId).Bounds.Height + captionHeight + 4 * borderHeight + statusHeight,
true);
}
/// <summary>
///文档关闭前
/// </summary>
/// <param name="doc"></param>
/// <param name="bCancel"></param>
private void exClass_wkBookBeforeClose(Workbook doc, ref bool bCancel)
{
Save();//确保文档关闭前能保存
bCancel = true;
}
}
}
更多追问追答
追问
OfficeBase是什么类?
WinAPI又在哪里?
追答
using Microsoft.Office.Interop.Excel;
你要在COM组件中引用OFFICE EXCEL。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询