[Repost] Operation Excel (C #)

xiaoxiao2021-03-06  22

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; 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 (" C1 ", missing.Value ); 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

Everyone needs to understand the use of TLBIMP tools :) This stuff is useful, you can transplant the ordinary Win32 program to. Net below :)

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

New Post(0)