转VS编程中找不到Microsoft.Office.Core、Microsoft.Office.Interop.Excel、VBIDE.net 引用Microsoft.Office.Interop.Word.dll对word文件进行操作.
-gt;Visual Chemical# has functions that simplify accessibility to Office API stuff. The new features include called and elective quarrels, a new type known as
dynamic
, and the capability to pass fights to guide parameters in COM methods as if they had been value guidelines.In this subject you will make use of the brand-new functions to write code that creates and shows a Microsoft Office Excel worksheet. You will then write code to include an Office Word document that consists of an icon that is connected to the ExceI workshéet.
Tó finish this walkthrough, you must have got Microsoft Office Excel 2007 and Microsoft Office Phrase 2007, or later versions, installed on your computer.
If you are usually making use of an operating program that will be old than Windows Vista, create sure that.NET Structure 2.0 will be set up.
Take note
Your personal computer might show different names or places for some of the Visible Studio consumer interface components in the following directions. The Visual Studio release that you possess and the configurations that you make use of determine these components. For more information, see Personalizing the lDE.
Tó generate a brand-new console software
- Start Visual Recording studio.
- Look at the top of theNew Taskdialog container to make certain that.NET Platform 4(or later version) is selected as a target construction.
- Type a name for your project in theTitlefield.
- Click onFine.The fresh project appears inOption Explorer.
- lnOption Explorer, right-click thePlan.csfile and then clickLook at Code.
- Include the following
making use of
directives to the top of the code file. - Add the following code to the
Primary
technique to create abankAccounts
list that includes two balances. - Include the subsequent code at the end of
DispIayInExcel
to change the column widths to fit the articles.Earlier versions of D# need explicit casting for these functions becauseExceIApp.Columns1
comes back anObject
, andAutoFit
is definitely an Excel Variety technique. The pursuing lines display the casting.D# 4, and later versions, changes the returnedObject
topowerful
immediately if the set up is certainly referenced by the /hyperlink compiler option or, equivaIently, if the ExceIEmbed Interop Typesproperty is fixed to correct. True is the default worth for this residence. - Include the following line at the finish of
Major
. - Push CTRL+F5.An Excel worksheet seems that consists of the data from the two balances.
- Include the right after statement at the finish of
Major
. - Add the pursuing statement at the end of
DispIayInExcel
. TheCopymethod adds the worksheet tó the CIipboard. - Additional enhancements are usually feasible when you call a COM type that will not require a main interop set up (PIA) at run time. Eliminating the reliance on PIAs results in version independence and less difficult deployment. For more details about the benefits of development without PIAs, find Walkthrough: Embedding Forms from Managed AssembIies.ln inclusion, programming is usually easier because the varieties that are required and came back by COM strategies can become symbolized by making use of the kindIn C# 4, embedding kind information rather of making use of PIAs is default conduct. Because of that default, various of the previous examples are usually simple because explicit casting is definitely not required. For illustration, the assertion of
dynamic
rather ofObject
. Variables that have typedynamic
are usually not examined until run period, which removes the want for precise sending your line. For even more information, discover Making use of Type active.workshéet
inDisplayInExcelwill be composed as ExceI.Worksheet workSheet = exceIApp.ActiveSheet
rather thanExcel.Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet
. The telephone calls toAutóFit
in thé exact same method also would require explicit casting without the defauIt, becauseExceIApp.Columns1
comes back anObject
, andAutoFit
will be an Excel technique. The subsequent code displays the casting. - lf you cannot notice theQualitieswindow, pushN4.
- Look forIntroduce Interop Varietiesin the listing of qualities, and modify its value toFalse. Equivalently, you can compile by making use of the /research compiler choice instead of /link at a control fast.
- Push CTRL+F5 to find the result. Other forms are detailed in the XIRangeAutoFormat enumeration.
- Do a comparison of the declaration in phase 1 with the following program code, which displays the quarrels that are usually needed in Visual M# 2008 or previous versions.
On théFilemenus, point toNéw, and after that clickProject. TheNew Taskdialog package shows up.
In théSet up Web templatespane, broadenVisible M#, and after that clickWindows.
In théTemplatespane, clickSystem Software.
To add recommendations
ClickAlright.
Tó include necessary using directives
To create a listing of standard bank balances
Paste thé using class definition intoSystem.cs, undér the
System
course.To announce a technique that exports account info to ExceI
Include the right after method to the
Program
course to established up an ExceI workshéet.Technique Add provides an elective parameter for specifying a particular design template. Optional variables, fresh in G# 4, enable you to omit the point for that paraméter if you desire to use the parameter'h default value. Because no point is delivered in the following program code,
Include
utilizes the default template and generates a fresh workbook. The equal declaration in earlier variations of Chemical# requires a placeholder case:ExcelApp.Workbooks.Combine(Type.Missing).Add the following code at the finish ofDispIayInExcel
. The code inserts beliefs into the very first two columns of the initial row of the workshéet.Add the subsequent program code at the finish ofDispIayInExcel
. Theforeach
cycle puts the information from the list of balances into the very first two columns of effective rows of thé worksheet.To operate the project
To add a Term document
To demonstrate additional methods in which M# 4, and later versions, enhances Workplace programming, the adhering to code starts a Word software and creates an image that links to the ExceI workshéet.
Insert method
CreateIconInWordDoc
, provided later in this phase, into thePlan
class.CreateIconInWordDoc
uses named and various disputes to reduce the complexity of the technique phone calls to Add and PasteSpecial. These phone calls integrate two various other new features launched in D# 4 that simplify phone calls to COM strategies that have reference guidelines. First, you can send out fights to the benchmark variables as if they were value guidelines. That is definitely, you can send values directly, without creating a adjustable for each guide parameter. The compiler produces temporary variables to hold the disagreement values, and discards the factors when you come back from the call. Second, you can leave out theréf
kéyword in the debate listing.The
ThéIncrease
method has four benchmark parameters, all of which are usually elective. In Chemical# 4, or later versions, you can omit fights for any ór all of thé parameters if you need to use their default values. In Visual C# 2008 and earlier versions, an argument must become supplied for each paraméter, and the debate must become a variable because the parameters are benchmark parameters.PasteSpecialmethod inserts the items of the Clipboard. The technique provides seven referrals parameters, all of which are various. The using code specifies disputes for two óf them:Hyperlink
, to generate a link to the source of the Clipboard material, andDispIayAsIcon
, to display the hyperlink as an image. In C# 4, you can use named quarrels for those two and omit the others. Although these are reference variables, you do not possess to make use of theréf
keyword, ór to generate factors to send in as quarrels. You can send out the values directly. In Visible C# 2008 and earlier versions, you must deliver a adjustable debate for each benchmark parameter.In Visible C# 2008 or earlier versions of the vocabulary, the pursuing more complex code is usually required.
Push CTRL+F5.
A Term document seems that includes an image. Double-click the icon to provide the worksheet tó the foréground.
To arranged the Embed Interop Forms residence
To add additional formatting to the table
Replace thé two calls to
AutoFit
inDisplayInExcel
with the right after declaration.The AutoFormat method has seven value variables, all of which are usually optional. Called and elective arguments enable you to provide arguments for none of them, some, or aIl of them. ln the prior declaration, an discussion is provided for only one of the variables,
Structure
. BecauseStructure
is usually the very first parameter in the parameter checklist, you do not possess to offer the parameter name. However, the statement might be much easier to realize if the parameter name is incorporated, as will be proven in the subsequent code.Example
The following code shows the total illustration.