Excel. Application APP = new excel. Application ();
App. sheetsinnewworkbook = 2;
App. workbooks. Add ();
Worksheet sheet1 = (worksheet) app. activeworkbook. worksheets [1];
Sheet1.name = "Hello word ";
Sheet1.cells [1, 1] = "Welcome to excel ";
Worksheet sheet2 = (worksheet) app. activeworkbook. worksheets [2];
Sheet2.name = "Hello Jiahua ";
Sheet2.cells [1, 1] = "Welcome to Jiahua ";
App. activeworkbook. saveas ("E: \ test.xls ");
App. activeworkbook. Close ();
App. Quit ();
Export to excel
Microsoft. Office. InterOP. Excel. Application APP = new Microsoft. Office. InterOP. Excel. Application ();
App. sheetsinnewworkbook = 1;
App. workbooks. Add ();
// Generate the column header
For (INT I = 0; I <datagridview1.columns. Count; I ++)
{
App. cells [1, I + 1] = maid [I]. headertext;
}
// Generate content
For (INT I = 0; I <datagridview1.rows. Count; I ++)
{
For (Int J = 0; j <datagridview1.columns. Count; j ++)
{
App. cells [I + 2, J + 1] = maid [I]. cells [J]. value;
}
}
// Set the style of the column header
Range = app. Range [App. cells [1, 1], app. cells [1, datagridview1.columns. Count];
// Range = app. get_range (App. cells [1, 1], app. cells [1, datagridview1.columns. Count]);
Range. Font. Bold = true;
Range. Font. Color = color. Red;
Range. Interior. colorindex = 15;
Range. Borders. linestyle = xllinestyle. xlcontinuous;
If (savefiledialog1.showdialog () = system. Windows. Forms. dialogresult. OK)
{
String filename = savefiledialog1.filename;
App. activeworkbook. saveas (filename );
App. activeworkbook. Close ();
App. Quit ();
}
// Import to the datagridview
If (openfiledialog1.showdialog () = system. Windows. Forms. dialogresult. OK)
{
String filename = openfiledialog1.filename;
Microsoft. Office. InterOP. Excel. Application APP = new Microsoft. Office. InterOP. Excel. Application ();
App. workbooks. Open (filename );
If (App. activeworkbook. worksheets [1]! = NULL)
{
Worksheet sheet = app. activeworkbook. worksheets [1];
Int ROW = 2;
// Generate a able
System. Data. datatable dt = new system. Data. datatable ();
DT. Columns. Add ("studentno", typeof (INT ));
DT. Columns. Add ("studentname", typeof (string ));
DT. Columns. Add ("gradename", typeof (string ));
DT. Columns. Add ("sex", typeof (string ));
DT. Columns. Add ("phone", typeof (string ));
DT. Columns. Add ("Address", typeof (string ));
While (true)
{
Datarow newrow = DT. newrow ();
Range range1 = sheet. cells [row, 1];
If (range1.text = "")
{
Break;
}
For (INT I = 0; I <6; I ++)
{
Range range2 = sheet. cells [row, I + 1];
If (range2.text = "")
{
Break;
}
Else
{
Newrow [I] = range2.text;
}
}
DT. Rows. Add (newrow );
Row ++;
}
App. activeworkbook. Close ();
App. Quit ();
Datagridview1.datasource = DT;
}
}
// Only data tables with rules can be operated (by querying)
If (openfiledialog1.showdialog () = system. Windows. Forms. dialogresult. OK)
{
String filename = openfiledialog1.filename;
String constring = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + filename + "; extended properties = 'excel 8.0; HDR = yes; IMEX = 1 '";
Oledbconnection con = new oledbconnection (constring );
String SQL = "select * from [sheet1 $]";
Oledbdataadapter da = new oledbdataadapter (SQL, con );
System. Data. datatable dt = new system. Data. datatable ();
Da. Fill (DT );
Datagridview1.datasource = DT;
}
This article from the "Zhang zhipeng" blog, please be sure to keep this source http://zhangzhipeng.blog.51cto.com/9115459/1571061
Import and export of EXCEL tables