我想做一个在winform窗体里嵌入EXCEL,做出来的一直是另外窗口打开EXCEL那个文件,各位帮忙看看问题在哪捏

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我想做一个在winform窗体里嵌入EXCEL,做出来的一直是另外窗口打开EXCEL那个文件,各位帮忙看看问题在哪捏相关的知识,希望对你有一定的参考价值。

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;
using System.Reflection;

namespace WindowsFormsApplication2

public partial class Form1 : Form

public Form1()

InitializeComponent();

public void Form1_Load(object sender, System.EventArgs e)



public void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)

Object o = e.pDisp;
Object oDocument = o.GetType().InvokeMember("Document", BindingFlags.GetProperty, null, o, null);
Object oApplication = o.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, oDocument, null);
Object oName = o.GetType().InvokeMember("Name", BindingFlags.GetProperty, null, oApplication, null);
Microsoft.Office.Interop.Excel.Application eApp = (Microsoft.Office.Interop.Excel.Application)oApplication;
MessageBox.Show("File opened by: " + oName.ToString());


private void axWebBrowser1_Enter(object sender, EventArgs e)

string strFileName = @"C:\Documents and Settings\yangyy\桌面\文件.xlsx";
Object missing = System.Reflection.Missing.Value;
axWebBrowser1.Navigate(strFileName, ref missing, ref missing, ref missing, ref missing);



怎样显示EXCEL的工具栏捏?

给点代码你参考
const uint KEYEVENTF_KEYDOWN = 0x0;
const uint KEYEVENTF_KEYUP = 0x2;
Object refmissing = Missing.Value;
object axWebBrowser;
//ExcelWrapper excel;
Excel.Workbook book;
Excel.Application app;
Excel.Sheets sheets;
Excel.Workbooks books;

string fileName;//输入文件名
public string FileName

get return fileName;
set fileName = value;


string sheetID;//单据号
public string SheetID

get return sheetID;
set sheetID = value;


string outFileName;//输出文件名
public string OutFileName

get return outFileName;
set outFileName = value;


bool readOnly;//输入文本
public bool ReadOnly

get return readOnly;
set readOnly = value;


string outText;//输出文本
public string OutText

get return outText;
set outText = value;


bool hasModify;//EXCEL是否修改
public bool HasModify

get return hasModify;
set hasModify = value;


#region 发送键盘事件的API函数
[DllImport("user32.dll")]
protected static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
#endregion

public SpecialDialog()

InitializeComponent();


protected override void OnShown(EventArgs e)

base.OnShown(e);
this.FindForm().Text = "特殊工艺EXCEL编辑器";
//this.FindForm().ShowInTaskbar = false; //不在任务栏显示
//this.FindForm().TopMost = true; //顶层窗体
//this.FindForm().MinimizeBox = false;
//this.FindForm().ControlBox = false;
//this.FindForm().ShowIcon = false;
//this.FindForm().FormBorderStyle = FormBorderStyle.FixedDialog;//不可调整大小
this.FindForm().StartPosition = FormStartPosition.CenterScreen;
this.FindForm().Height = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height;
this.FindForm().Location = new Point(this.FindForm().Location.X, 0);

this.toolStrip1.Visible = false;
foreach (Control c in this.Controls)

if (c is TableLayoutPanel)

c.BackColor = System.Drawing.Color.MistyRose;




private void SpecialDialog_Load(object sender, EventArgs e)

this.wbExcel.Navigate(fileName);
axWebBrowser = this.wbExcel.ActiveXInstance;

object oApplication = axWebBrowser.GetType().InvokeMember("Document",
BindingFlags.GetProperty, null, axWebBrowser, null);
book = (Excel.Workbook)oApplication;
app = book.Application as Excel.Application;
//excel = new ExcelWrapper(fileName, app, book);
sheets = book.Sheets;
books = app.Workbooks;
app.DisplayAlerts = false;
if (readOnly)
//设置只读
btnOK.Enabled = false;
//下面这句可以锁住EXCEL,但是这样就不能复制了,故先去掉
//((Excel.Worksheet)book.Sheets["SpecialTech"]).EnableSelection = Microsoft.Office.Interop.Excel.XlEnableSelection.xlNoSelection;



private void tsbDisplayMenu_Click(object sender, EventArgs e)

keybd_event(27, 0, KEYEVENTF_KEYDOWN, 0);
keybd_event(27, 0, KEYEVENTF_KEYUP, 0);
keybd_event(27, 0, KEYEVENTF_KEYDOWN, 0);
keybd_event(27, 0, KEYEVENTF_KEYUP, 0);

if (MenuControl())

if (tsbDisplayMenu.Text == "隐藏EXCEL工具条")
tsbDisplayMenu.Text = "显示EXCEL工具条";
else
tsbDisplayMenu.Text = "隐藏EXCEL工具条";



private void wbExcel_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

MenuControl();


private bool MenuControl()

try

object[] args = new object[4];
args[0] = OLECMDID.OLECMDID_HIDETOOLBARS;
args[1] = OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER;
args[2] = refmissing;
args[3] = refmissing;
axWebBrowser.GetType().InvokeMember("ExecWB", BindingFlags.InvokeMethod, null, axWebBrowser, args);
return true;

catch

MessageBox.Show(tsbDisplayMenu.Text + "发生了错误,\r\n请将光标移出", "提示");
return false;



private void sbtLoadTemplate_Click(object sender, EventArgs e)



protected override void OnOkClick()

hasModify = !book.Saved;
string a = ((Excel.Worksheet)book.Worksheets[1]).Name;
object c = ((Excel.Worksheet)book.Worksheets[1]).get_Range("A1", "A1").ColumnWidth;
if (((Excel.Worksheet)book.Worksheets[1]).Name != "SpecialTech")

MessageBox.Show("文件不能超过100K", "提示");
ISValid = false;
return;

outFileName = fileName;
fileName = FileUtilities.GetUniqueTempFileName(".xls");
book.Save();
book.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
outText = "";
for (int j = 1; j <= 100; j++)

outText += app.get_Range(app.Cells[j, 1], app.Cells[j, 1]).Text + "\r\n";

outText = outText.TrimEnd(new char[] '\r', '\n' );
using (FileStream stream = new FileStream(outFileName, FileMode.Open, FileAccess.Read))

if (stream.Length <= 100000)

byte[] SendData = null;
int len = (int)stream.Length;
SendData = new byte[len];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(SendData, 0, len);
OutParameter = (object)SendData;
base.OnOkClick();

else

MessageBox.Show("文件不能超过100K", "提示");
ISValid = false;
return;


base.OnOkClick();


private void SpecialDialog_FormClosing(object sender, FormClosingEventArgs e)

ExcelExit();
if (wbExcel != null)
wbExcel.Dispose();


private void ExcelExit()

book.Save();
int i = books.Count;
NAR(sheets);
//books.Close();
NAR(book);
app.DisplayAlerts = true;
if (i <= 1)

NAR(books);
app.Quit();
NAR(app);



private void NAR(Object o)

try

Marshal.ReleaseComObject(o);

catch


finally

o = null;



显示EXCEL工具栏查看MenuControl方法
参考技术A 你的代码在我这里运行时,EXCEL是在本窗口的axWebBrowser1中打开的啊,并没有在另外窗口打开

可以用下面这句显示工具栏
axWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER);

将这句话加到axWebBrowser1_NavigateComplete2事件代码中,如下:

public void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)

axWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER); //显示工具栏
Object o = e.pDisp;
Object oDocument = o.GetType().InvokeMember("Document", BindingFlags.GetProperty, null, o, null);
Object oApplication = o.GetType().InvokeMember("Application", BindingFlags.GetProperty, null, oDocument, null);
Object oName = o.GetType().InvokeMember("Name", BindingFlags.GetProperty, null, oApplication, null);
Microsoft.Office.Interop.Excel.Application eApp = (Microsoft.Office.Interop.Excel.Application)oApplication;
MessageBox.Show("File opened by: " + oName.ToString());
参考技术B 去微软网站下载个补丁,是专门解决这个问题的。
MicrosoftFixit50298.msi
参考技术C 楼主有没有解决这个问题?我运行的结果也是两个窗体,Excel只是单独被打开了,并没有嵌入窗体中

WinForm中如何实现嵌入form窗体(panel与子窗体)

参考技术A 今天在做项目时候遇到一个问题,窗体分为左右两部分,要求在左边栏点击按钮时,右边动态加载窗体最后想到用panel实现,经历几次失败,并查找资料后,终于搞定说明:如果多次切换需加入 panel.clear();清空原有panel内容环境:C# VS2008方法一:通过给panel加载子元素实现Form2 form =new Form2();form.FormBorderStyle = FormBorderStyle.None; //隐藏子窗体边框(去除最小花,最大化,关闭等按钮)form.TopLevel =false; //指示子窗体非顶级窗体this.panel1.Controls.Add(form);//将子窗体载入panelform.Show();方法二:指定父容器实现Form2 form=new Form2();form.MdiParent=this;//指定当前窗体为顶级Mdi窗体form.Parent=this.Panel1;//指定子窗体的父容器为Frm.FormBorderStyle = FormBorderStyle.None;//隐藏子窗体边框,当然也可以在子窗体的窗体加载事件中实现本回答被提问者采纳

以上是关于我想做一个在winform窗体里嵌入EXCEL,做出来的一直是另外窗口打开EXCEL那个文件,各位帮忙看看问题在哪捏的主要内容,如果未能解决你的问题,请参考以下文章

Delphi 窗体中怎样嵌入网页

winform窗体问题`急等`请高手们帮忙解决

如何在C# Winform中嵌套一个播放器,就好像PPS那种我想做一个播放视频的软件,请高手指点啊

C#窗体中嵌入excel的模板

C#将exe运行程序嵌入到自己的winform窗体中

C# WinForm splitContainer 问题