1. Job Environment
Development Environment:Vs2005/vs2008
Database:Sql2005
Excel: 2003 First InVSJoinComComponent (of course, you can also add the Excel component under. Net ): After that, the following three DLL files will be added to the subdirectory referenced by: The procedure is as follows:
CopyCode The Code is as follows: Using Excel;
// From Bill example
Public void writeexceladvance (string outputfile)
{
String [,] mydata =
{
{"License plate number", "type", "product brand", "model number", "color", "Additional license number", "vehicle identification number "},
{"Zhe ka3676", "dangerous goods", "truck", "Tie Feng szg9220yy", "White", "1110708900", "022836 "},
{"Zhe ka4109", "dangerous goods", "Freight Car", "Liberation ca4110p1k2", "White", "223132", "010898 "},
{"Zhe ka0001a", "dangerous goods", "Freight Car", "nanming lsy9donews", "White", "1110205458", "0474636 "},
{"Zhe ka0493", "shangpu goods", "Freight Car", "Liberation lsy9donews", "White", "1110255971", "0094327 "},
{"Zhe ka1045", "general cargo", "Freight Car", "Liberation lsy9171wcd", "blue", "1110391226", "0516003 "},
{"Zhe ka1313", "general cargo", "Freight Car", "Liberation 9425wcd", "blue", "1110315027", "0538701 "},
{"Zhe ka1322", "general cargo", "Freight Car", "Liberation lsy9donews", "blue", "24323332", "0538716 "},
{"Zhe ka1575", "general cargo", "Freight Car", "Liberation lsy9181wcd", "blue", "1110314149", "0113018 "},
{"Zhe ka1925", "general cargo", "Freight Car", "Liberation lsy9220wcd", "blue", "1110390626", "00268729 "},
{"Zhe ka2258", "general cargo", "Freight Car", "Liberation lsy9220wsp", "blue", "111048152", "00320 "}
};
// Reference the Excel application category
Application myexcel = NULL;
// Reference the binder category
Workbook mybook = NULL;
// Reference the worksheet category
Worksheet mysheet = NULL;
// Reference the range category
Range myrange = NULL;
// Start a new application
Myexcel = new Microsoft. Office. InterOP. Excel. Application ();
// Open an existing Excel 2007 and with different open parameters. For details, go online and check:
// Excelsql. workbooks. Open (@ "C: \ 08.xls" (an existing Excel path), type. Missing,
// Type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing, type. missing );
// Add a new album
Myexcel. workbooks. Add (true );
// Disable warning message
Myexcel. displayalerts = false;
// Make the Excel file visible
Myexcel. Visible = true;
// Reference the first album
Mybook = myexcel. workbooks [1];
// Set the album focus
Mybook. Activate ();
// Reference the first Worksheet
Mysheet = (worksheet) mybook. worksheets [1];
// Name the worksheet as "array"
Mysheet. Name = "cells ";
// Set the worksheet focus
Mysheet. Activate ();
Int A = 0;
Int upbound1 = mydata. getupperbound (0 );
// Maximum number of two-dimensional arrays
Int upbound2 = mydata. getupperbound (1 );
// Maximum number of two-dimensional arrays
// Write report name
Myexcel. cells [1, 4] = "normal Report ";
// The following select method can be omitted to accelerate Excel running, but some functions of VBA must use the select method.
// The following select method can be omitted to accelerate Excel running, but some functions of VBA must use the select method.
// Write data row by row
For (INT I = 0; I <upbound1; I ++)
{
For (Int J = 0; j <upbound2; j ++)
{
// Start with a single quotation mark, indicating that the cell is pure text
A ++;
// Use offset to write data to the array
Myrange = mysheet. get_range ("A2", type. Missing );
Myrange. get_offset (I, j). Select ();
Myrange. value2 = "'" + mydata [I, j];
// Write the array data with cells
Myrange. get_range (myexcel. cells [2 + I, 1 + J], myexcel. cells [2 + I, 1 + J]). Select ();
Myexcel. cells [2 + I, 1 + J] = "'" + mydata [I, j];
}
}
// Add a new worksheet after 1st
Mybook. Sheets. Add (type. Missing, mybook. worksheets [1], 1, type. Missing );
// Reference 2nd worksheets
Mysheet = (worksheet) mybook. worksheets [2];
// Name the worksheet as "array"
Mysheet. Name = "array ";
// Console. writeline (mysheet. Name );
// Write report name
Myexcel. cells [1, 4] = "normal Report ";
// Set the range
Myrange = (range) mysheet. get_range (myexcel. cells [2, 1], myexcel. cells [upbound1 + 1, upbound2 + 1]);
Myrange. Select ();
// Write data with an array at a time
Myrange. value2 = "'" + mydata;
// Set the storage path
// String pathfile = directory. getcurrentdirectory () + @ "\ button4.xls ";
// Save another album
Mybook. saveas (outputfile, type. Missing, type. Missing
, Xlsaveasaccessmode. xlnochange, type. Missing );
// Close the binder
Mybook. Close (false, type. Missing, type. Missing );
// Close Excel
Myexcel. Quit ();
// Release Excel Resources
System. runtime. interopservices. Marshal. releasecomobject (myexcel );
Mybook = NULL;
Mysheet = NULL;
Myrange = NULL;
Myexcel = NULL;
GC. Collect ();