Operation Excel (C #)

xiaoxiao2021-03-06  41

Compilation: http://www.aspcn.com orthodontics: arung Source: http://www.csharphelp.com/archives/archive241.html

I didn't translate it in front. Foreigners operate in Excel 2000. All Excel's program operations are derived from Excel's object library Excel9.olb. This example is just a simple operation of this stuff. There is a specific understanding of your friends :) It is also a brother who is annoying for Excel on our site, refers to a "bright road" :) First step is to use the TLBIMP tool to use the object library file of Excel9.0 Excel8.olb conversion becomes a DLL, so you can use the .NET platform assembly to use :) The operation is as follows:

TLBIMP Excel9.olb Excel.dll

As long as there is this Excel.dll, now we can use Excel's various operational functions. Let's take a look at how C # is how to use these stuff.

1. Create a new Excel Application:

Application EXC = New Application (); if (exc == null) {Console.Writeline ("Error: Excel Couldn't Be Started); Return 0;}

2. Let this project visible:

Exc.set_visible (0, TRUE);

3. Get the Workbooks collection:

Workbooks Workbooks = Exc.Workbooks;

4. Add new Workbook:

_Workbook workbook = workbooks.add (xlwbateMplate.xlwbatworksheet, 0);

5. Get the Worksheets collection:

_Worksheet Worksheet = (_Worksheet) Sheets.get_Item (1); if (Worksheet == Null) {Console.WriteLine ("Error In Worksheet == Null);} 6. Set variables to cells:

Range Range1 = Worksheet.get_Range; if (Range1 == Null) {Console.writeline ("Error: Range == Null);} const Int ncells = 1; Object [] args1 = New Object [1]; args1 [0] = ncells; Range1.gettype (). InvokeMember ("Value", BindingFlags.SetProperty, Null, Range1, Args1);

Routine:

Using system.reflection; using system.runtime.interopservices; using Excel;

Class Excel {public static int main () {Application Exc = new application (); if (exc == null) {Console.Writeline ("Error: Excel Couldn't Be Started!"); Return 0;}

exc.set_Visible (0, true); Workbooks workbooks = exc.Workbooks; _Workbook workbook = workbooks.Add (XlWBATemplate.xlWBATWorksheet, 0); Sheets sheets = workbook.Worksheets; _Worksheet worksheet = (_Worksheet) sheets.get_Item (1); IF (Worksheet == Null) {Console.WriteLine ("Error: Worksheet == Null);}

Range Range1 = Worksheet.get_Range; if (Range1 == Null) {Console.writeline ("Error: Range == Null);} const Int ncells = 1; Object [] args1 = New Object [1]; args1 [0] = ncells; Range1.gettype (). InvokeMember ("Value", BindingFlags.SetProperty, Null, Range1, Args1); Return 100;}}

Now let's take a look at how to use arrays, he is some similar to setting cells. Just a change in the change is just args2 [0] = array2; const Int ncell = 5; Range Range2 = Worksheet.GET_RANGE ("A1", "E1"); int [] array2 = new int [ncell]; for (int i = 0; I

Output: You need to know the use of TLBIMP tools :) This stuff is useful, you can transplant the ordinary Win32 program below. Net below :)

转载请注明原文地址:https://www.9cbs.com/read-54421.html

New Post(0)