C # methods and code for generating Excel files

Source: Internet
Author: User
Tags visual studio

An example:

Class Apptest

{

Private Excel.applicationclass _x;

public static void Main0 ()

{

Apptest a = new Apptest ();

a._x = new Excel.applicationclass ();

A._x.usercontrol = false;

for (int i = 0; i < 4; i++)

{

A.savetoxls ("d://test//" + i + ". xls"); This example is the test folder created under D disk

}

A._x.quit ();

System.Runtime.InteropServices.Marshal.ReleaseComObject ((object) a._x);

System.GC.Collect ();

}

private void Savetoxls (string filename)

{

Excel.workbookclass WB = (excel.workbookclass) this._x.workbooks.add (System.Reflection.Missing.Value);

for (int i = 1;i <= 4; i++)

{

This._x.cells[i,1]=i.tostring ();

this._x.cells[i,2]= "' Bbb2";

this._x.cells[i,3]= "' ccc3";

this._x.cells[i,4]= "' Aaa4";

}

Wb. Saved = true;

This._x.activeworkbook.savecopyas (filename);

}

}

"Note: Running in vs.net is to add Excel.dll components, the Excel component Vs.net itself is not, and the following is the method for generating Excel.dll." 】

1. To ensure that the machine itself to install Office.

2. Copy the Excel9.olb file under [C:/Program files/microsoft office/office: Default installation path] to the [c:/visual studio.net/sdk/v1.1/bin:vs.net installation path] path.

3. Open Visual Studio. Net2003 command prompt, run tlbimp excel9.olb Excel.dll, the C:/visual component is generated under [Studio.net/sdk/v1.1/bin Excel.dll].

4. Adding a Excel.dll reference to the project is OK.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

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.