Excel|web first, briefly review how to manipulate Excel tables
To add a reference to Excel first. Select Project-〉 Add Reference-〉com-〉 add Microsoft Excel 9.0. (Different office will have different versions of DLL files).
Using Excel;
Using System.Reflection;
Create a new process of excel.application
Excel.Application app = new Excel.Application ();
if (app = null)
{
Statusbar1.text = "Error:excel couldn ' t be started!";
return;
}
App. Visible = true; Set to False if you want to use program control only for this Excel and do not want the user to operate
App. UserControl = true;
Workbooks workbooks =app. workbooks;
_workbook workbook = workbooks. ADD (Xlwbatemplate.xlwbatworksheet); Generate new Workbook from template
_workbook workbook = workbooks. ADD ("C:\\a.xls"); or open the workbook file based on the absolute path A.xls
This is paragraph puts the value 5 to the cell G1
Range Range1 = Worksheet.get_range ("A1", Missing.Value);
if (Range1 = null)
{
Statusbar1.text = "Error:range = null";
Return
}
const int ncells = 2345;
Range1. Value2 = Ncells;
Embedding the Excel user interface into your Windows Form
Since neither C # nor vb.net supports OLE technology (see Microsoft Support Center info:304562), only the WebBrowser control is used to complete this function. (See Microsoft Support Center howto:304662)
1. Right-click the Toolbox, select Custom Toolbox, add COM components, select "Microsoft Web Browser" (corresponding file is \winnt\system32\shdocvw.dll), OK. The Webbroser control icon with the text as Explorer appears in the Toolbox.
2, add the WebBrowser control in the Form1. (Object name but province is AxWebBrowser1)
3. Assume that the Excel file you want to open is: C:\a.xls.
String strFileName = @ "C:\a.xls";
Object refmissing = System.Reflection.Missing.Value;
Axwebbrowser1.navigate (strFileName, ref refmissing, ref refmissing, ref refmissing, ref Refmissin g);
It is worth noting that the WebBrowser control does not support menu merging, which means that the menu of Excel tables cannot be brought into our program. This is a major drawback relative to the OLE implementation approach. Fortunately, it provides the ability to add a toolbar to the toolbar, which allows you to do many Excel-specific operations through the toolbar.
The following sentence can be added to Excel's own tool tuning
AXWEBBROWSER1.EXECWB (SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER , ref refmissing, ref refmissing);
Third, back to the question raised in this article, how to operate the embedded Excel?
First you need to understand that using WebBrowser to "load the Excel" table is actually still running Excel.exe in the new process space. You can use Task Manager to observe. So, as long as we can get the Excel.Application object, we can manipulate the Excel data as mentioned in the previous one.
Fortunately, you can access Excel.Application via the WebBrowser method navigatecomplete the event parameter E provided.
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);
Because the Excel file is open, the oapplication here is actually excel.application
Excel.Application Eapp = (excel.application) oapplication;//This allows you to manipulate Excel as described earlier.
}
Iv. How do I ensure that the Excel process exits when the form that contains the WebBrowser exits? (See Microsoft Help Center KB317109)
Because WebBrowser is simply browsing the Excel table, Excel runs in a separate process. So make sure that you release the reference to the Excel object Eapp and all its corresponding member variables to ensure that the Excel process exits when the form exits. This is especially important when a program needs to turn off Excel tables more than once.
Excel.Application oapp;
Excel.Workbooks obooks;
Excel.Workbook obook;
Excel.Worksheet osheet;
...........
private void Excelexit ()
{
NAR (osheet);
Obook.close (False);
NAR (obook);
NAR (obooks);
Oapp.quit ();
NAR (oapp);
Debug.WriteLine ("Sleeping ...");
System.Threading.Thread.Sleep (5000);
Debug.WriteLine ("End Excel");
}
private void NAR (Object o)
{
Try{system.runtime.interopservices.marshal.releasecomobject (o);}
catch{}
Finally{o = null;}
}
After testing, I found that in addition to releasing these variables, the axWebBroswer1 must also be destroyed, the Excel process to exit. Otherwise, the Excel process will not exit even if you let AxWebBroser1 navigate empty content "About:blank". Therefore, you should close the form where the AxWebBroser1 is located, or call Axwebbrowser1.dispose () directly;
If there are other problems that cause a failure to exit normally, only garbage collection is invoked.
Gc. Collect ();
Problem:
Problem:
With Axwebbrowser1.navigate (strFileName, ref refmissing, ref refmissing, ref refmissing, ref Refmissi NG); When you reopen another Excel file, you are prompted to save it, how do you keep it from being prompted to save and cancel saving???
1 floor
up!!!!!!!!!
2 floor
Up
3 floor
Obook.close (False) It seems wrong, I used to keep the error.
4 floor
Obook.close (false,type.missing,type.missing);
Debug successful
5 Floor
To determine if a file exists, delete it.
6 floor
Up
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.