Public static void createexcel ()
{
For (INT I = 0; I <1; I ++)
{
String stafile = "d :\\ test \ x ";
System. reflection. Missing Miss = system. reflection. Missing. value;
Excel. applicationclass m_objexcel = new excel. applicationclass ();
M_objexcel.visible = false;
Excel. workbooks m_objbooks = (Excel. workbooks) m_objexcel.workbooks;
Excel. Workbook m_objbook = (Excel. workbook) (m_objbooks.add (MISs ));
Excel. worksheet m_objsheet = (Excel. worksheet) m_objbook.activesheet;
// Write data to an Excel file
Excel. Range ER = m_objsheet.get_range (object) "A1", system. reflection. Missing. value );
Er. value2 = "dfadfa ";
M_objbook.saveas (stafile + I. tostring () + ". xls ", Miss, miss, Excel. xlsaveasaccessmode. xlnochange, miss, miss );
M_objbook.close (false, miss, miss );
M_objbooks.close ();
M_objexcel.quit ();
System. runtime. interopservices. Marshal. releasecomobject (ER );
System. runtime. interopservices. Marshal. releasecomobject (m_objsheet );
System. runtime. interopservices. Marshal. releasecomobject (m_objbook );
System. runtime. interopservices. Marshal. releasecomobject (m_objbooks );
System. runtime. interopservices. Marshal. releasecomobject (m_objexcel );
GC. Collect ();
}
}
[Note: You need to add the Excel. dll component to run vs. net. The Excel component vs. Net itself does not exist. The following describes how to generate EXCEL. dll .]
1. Make sure that the machine itself needs to install office.
2. copy the excel9.olb file under [c: \ Program Files \ Microsoft Office \ Office: default installation path] to [c: \ Visual Studio. net \ SDK \ V1.1 \ bin:.. net installation path.
3. open Visual Studio. run tlbimp excel9.olb excel. DLL in [c: \ Visual Studio. net \ SDK \ V1.1 \ bin] to generate an Excel file. DLL component.
4. Add an Excel. dll reference to the project.
Find tlbimp and excel9.olb on your computer, copy them to a folder, and execute tlbimp excel9.olb in the DOS window. The following three files will be generated: excel. DLL, office. DLL and vbide. DLL. "
========================================================== ====
// Connect to the Excel Data Source
String filename = This. openfiledialog2.filename;
String filepath = This. openfiledialog1.filename;
Oledbconnection conn = new oledbconnection ("provider = Microsoft. Jet. oledb.4.0; Data Source =" + filepath + "; extended properties = \" Excel 8.0 ;\"");
Conn. open ();
// Create an Excel application (before writing an Excel sheet)
Microsoft. Office. InterOP. Excel. applicationclass oexcel;
Oexcel = new Microsoft. Office. InterOP. Excel. applicationclass ();
Oexcel. usercontrol = false;
Microsoft. Office. InterOP. Excel. workbookclass WB = (Microsoft. Office. InterOP. Excel. workbookclass) oexcel. workbooks. Add (system. reflection. Missing. value );
// Create a new sheet, which is in the focus state by default after being created
WB. worksheets. Add (missing. Value, missing. Value, 1, missing. value );
// Cell write example
Oexcel. cells [1, 1] = "first column ";
Oexcel. cells [1, 2] = "second column ";
Oexcel. cells [1, 3] = "third column ";
// Save the workbook
WB. Saved = true;
// Generate a file and release resources
Oexcel. activeworkbook. savecopyas (filename );
Oexcel. Quit ();
System. gc. Collect ();