永发信息网

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

答案:4  悬赏:70  手机版
解决时间 2021-04-04 14:19
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的工具栏捏?
最佳答案
你的代码在我这里运行时,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());
}
全部回答

首先在你的项目里添加引用 microsoft.office.interop.excel.dll

然后 //创建对象     microsoft.office.interop.excel.application ecl = new microsoft.office.interop.excel.application();     ecl.workbooks.add(true);//允许工作区域放东西

//举例子

  ecl.cells[1, 1] = "姓名";     ecl.cells[1, 2] = "学号";     ecl.cells[1, 3] = "性别";     ecl.cells[1, 4] = "年龄";     ecl.cells[1, 5] = "座位";     ecl.cells[1, 6] = "住址";

    //////////////////////     for (int i = 2; i < ds.tables["stuinfo"].rows.count + 2; i++)     {     ecl.cells[i, 1] = ds.tables["stuinfo"].rows[i - 2][0].tostring();     ecl.cells[i, 2] = ds.tables["stuinfo"].rows[i - 2][1].tostring();     ecl.cells[i, 3] = ds.tables["stuinfo"].rows[i - 2][2].tostring();     ecl.cells[i, 4] = ds.tables["stuinfo"].rows[i - 2][3].tostring();     ecl.cells[i, 5] = ds.tables["stuinfo"].rows[i - 2][4].tostring();     ecl.cells[i, 6] = ds.tables["stuinfo"].rows[i - 2][5].tostring();     }

 ecl.visible = true;//显示excel

去微软网站下载个补丁,是专门解决这个问题的。 MicrosoftFixit50298.msi
给点代码你参考 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方法
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
求情侣名:倾一世红颜,来下半句。分男女用。
重庆电信宽带价格贵不贵
在农行预约孙中山纪念币,为什么最后一栏不好
梦幻西游有鬼魂术的BB带个高鬼魂的套装这样可
Thepolicemanwarnedthedriver_______toofast.
刊物的卷标是什么意思,期刊的卷号怎么看??
请问一共怀了6个小孩第一次剖腹产,人流五次
请问如何用FL STUDIO做出这样的效果
6splus为什么玩微信这么卡?
标致307离合器踩下就没有声抬起就声呢
近水阁足浴会馆怎么去啊,有知道地址的么
坦克世界M系火炮怎么玩?(M4043和M5355)
lim(x→∞)[(x+2a)/(x-a)]∧x/3=8,求
到底什么角度什么光线下照出照片才是最真实的
白鹿原一句评论,朋友评论放纵了用什么句子回
推荐资讯
校泵油是什么油?在什么泵上用的?
用有时我想,有时,我有幻想造句
若尔盖大草原的门票多少钱
父亲姓董,母亲姓何怎样取名字
描写逆境中成长的诗句,有关不怕困难,意志顽强
日文在什么情况下使用汉字?
台式电脑找不到驱动程序怎么办?
2017款牧马人2,0排量卖多少钱
把句子运用修辞手法,运用修辞手法的句子
神医嫡妃:邪王宠妃别心急txt
生于欲望是什么意思
面包车 那种牌子的性价比最好,3万元以下的。
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?