. Net key code for creating an Excel file and some instructions

Source: Internet
Author: User
To add com to the project to reference Microsoft Excel 11.0 object libary

Excel. applicationclass AC = new excel. applicationclass (); // The application is equivalent to executing in Excel. Program , Excel.exe
AC. defaultfilepath = "";
AC. sheetsinnewworkbook = 2; // when creating an Excel xls file, two sheets are created by default.
AC. displayalerts = false; // when the saveas method is executed, no dialog box is displayed. For example, if a file with the same name is overwritten, setting it to false will overwrite the file directly.


Excel. Workbook WB = ac. workbooks. Add (type. Missing); // Workbook is equivalent to an Excel xls file
Excel. worksheet Ws = (Excel. worksheet) AC. worksheets [2]; // because there are two sheets set above, 2nd sheets are obtained here. worksheet is equivalent to a sheet in an Excel file, note that the Excel indexes start from 1.


WS. cells [1, 1] = "hello121"; // assign "hello" to the grid of the 2nd rows and columns in the 1st sheets ";
AC. cells [1, 2] = "I love u"; // This is a value to the first sheet, after a workbook is added to applicationclass, its cells can only access cells in the 1st sheet.

// Save the file. Excel. xlsaveasaccessmode sets the file access mode. Other parameters can be omitted.
WB. saveas (@ "E: \ A \ test.xls", type. missing, type. missing, type. missing, type. missing, type. missin, Excel. xlsaveasaccessmode. xlnochange, type. missing, type. missing, type. missing, type. missing, type. missing );


// Shut down the excel.exe Process
AC. workbooks. Close (); // close all files
AC. Quit (); // your excel.exe. This line must exist. If there is no previous line, you can close the Excel application, and the files are also closed.

// The following lines are important in ASP. if these rows are not found in. net, the Excel process cannot be cleared. In this case, the memory is terminated by the Excel process started every time the export is started. Only the following lines are added, to shut down the process.
Int Generation = GC. getgeneration (AC );
System. runtime. interopservices. Marshal. releasecomobject (AC );
AC = NULL;
GC. Collect (generation );

Excelapplication. Workbook. saveas (filename, fileformat, password, writerespassword, readonlyrecommended, createbackup, accessmode, conflictresolution, addtomru, textcodepage, textvisuallayout, local)

1. filename: variant type. Optional. This string indicates the name of the file to be saved. It can contain the complete path. If no path is specified, Microsoft Excel saves the file to the current folder.

2. fileformat: variant type. Optional. The format used to save the file. For a list of valid options, see the fileformat attribute. For existing files, the default format is the last specified file format. For new files, the default format is the current Excel version format. The optional constants are as follows:

Xladdin 18
Xlcsv 6
Xlcsvmac 22
Xlcsvmsdos 24
Xlcsvwindows 23
Xlcurrentplatformtext-4158
Xldbf2 7
Xldbf3 8
Xldbf4 11
Xldif 9
Xlexcel2 16
Xlexcel2fareast 27
Xlexcel3 29
Xlexcel4 33
Xlexcel4workbook 35
Xlexcel5 39
Xlexcel739
Xlexcel9795 43
Xlhtml 44
Xlintladdin 26
Xlintlmacro 25
Xlsylk 2
Xltemplate 17
Xltextmac 19
Xltextmsdos 21
Xltextprinter 36
Xltextwindows 20
Xlunicodetext 42
Xlwebarchive 45
Xlwj2wd1 14
Xlwj3 40
Xlwj3fj3 41
Xlwk1 5
Xlwk1all 31
Xlwk1fmt 30
Xlwk3 15
Xlwk3fm3 32
Xlwk4 38
Xlwks 4
Xlworkbooknormal to 4143
Xlworks2fareast 28
Xlwq1 34
Xlxmldata 47
Xlxmlspreadsheet 46

3. Password: variant type. Optional. It is a case-sensitive string (up to 15 characters), used to specify the File Password.

4. writerespassword: variant type. Optional. This string indicates the WRITE password of the file. If the file is saved with a password, but the password is not entered when the file is opened, the file is read-only.

5. readonlyrecommended: variant type. Optional. If the value is true, a message is displayed when the file is opened, prompting the file to be opened in read-only mode.

6. createbackup: variant type. Optional. If this value is true, a backup file is created.

7. accessmode: xlsaveasaccessmode type. Optional. If this parameter is omitted, the access mode is not changed. If the shared list is saved without changing the file name, ignore the parameter. To change the access method, use the exclusiveaccess method. The following are optional constants: xlexclusive 3 does not include the method xlnochange 1 and does not change the access method. The default value is xlshared 2 shared list.

8. conflictresolution: xlsaveconflictresolution type. If this parameter is ignored, the *** processing dialog box is displayed. The optional constants are as follows:

Xllocalsessionchanges 2 automatically accepts modifications from Local Users
Xlothersessionchanges 3 accepts changes from users other than local users
Xluserresolution 1 shows the *** solution dialog box

9. addtomru: variant type. Optional. If the value is true, the workbook is added to the list of recently used files. The default value is false.

10. textcodepage: variant type. Optional. It is not used in Microsoft Excel of the U.S. English version.

11. textvisuallayout: variant type. Optional. It is not used in Microsoft Excel of the U.S. English version.

12. Local: variant type. Optional. If the value is true, the file is saved in Microsoft Excel (including control panel settings. If the value is false (default value), save the file in the language of Visual Basic for Applications (VBA), where Visual Basic for Applications (VBA) is a typical American English version installed, unless workbooks. open from the old international xl5/95 VBA Project

The approximate Excel mode should be the composition mode.
Applicationclass (EXCEL program)-> (included) Workbook (Excel document)-> (included) Worksheet (sheet in the document)-> (included) range (cell)

About office development
Http://msdn2.microsoft.com/en-us/library/bb241279.aspx description office storage format

Xlexcel856It is stored in the format of 97-2003 in 2007. If there is a workbook saveas method, you need to add 2nd file format parameters to save it in the format of 97-2003. Setting defaultsaveformat is useless.

If no 2nd format parameters are added, the XLSX format is 2007.

In addition, to close the Excel process, you can also use the method of killing the process.

GC. Collect (); // force destroy </P>
<P> # region forcibly kills the recently opened Excel Process
System. Diagnostics. Process [] excelproc = system. Diagnostics. process. getprocessesbyname ("Excel ");
System. datetime starttime = new datetime ();
Int M, killid = 0;
For (m = 0; m <excelproc. length; m ++)
{
If (starttime <excelproc [M]. starttime)
{
Starttime = excelproc [M]. starttime;
Killid = m;
}
}
If (excelproc [killid]. hasexited = false)
{
Excelproc [killid]. Kill ();
}

However, I think the process killing method is unavailable in the concurrent environment.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.