我想做一个在winform窗体里嵌入EXCEL,做出来的一直是另外窗口打开EXCEL那个文件,各位帮忙看看问题在哪捏
解决时间 2021-04-04 14:19
- 提问者网友:活着好累
- 2021-04-04 00:57
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的工具栏捏?
最佳答案
- 五星知识达人网友:青尢
- 2021-04-04 02:33
你的代码在我这里运行时,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());
}
全部回答
- 1楼网友:骨子里都是戏
- 2021-04-04 05:15
首先在你的项目里添加引用 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
- 2楼网友:梦中风几里
- 2021-04-04 03:39
去微软网站下载个补丁,是专门解决这个问题的。
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方法
我要举报
大家都在看
推荐资讯