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:
Copy codeThe 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 ();