資源描述:
《c#讀取excel數(shù)據(jù)的兩個方法》由會員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在應(yīng)用文檔-天天文庫。
1、C#讀取excel數(shù)據(jù)的兩個方法oledb方法?程序代碼usingSystem.Data.OleDb;????????????stringstrCon="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=c:\test.xls;ExtendedProperties=Excel8.0";????????????OleDbConnectionmyConn=newOleDbConnection(strCon);????????????stringstrCom="Sel
2、ect*FROM[1$A1:C100]";????????????myConn.Open();????????????OleDbDataAdaptermyCommand=newOleDbDataAdapter(strCom,myConn);????????????DataSetmyDataSet=newDataSet();????????????myCommand.Fill(myDataSet,"1$");????????????myConn.Close();????????????dataGridV
3、iew1.DataSource=myDataSet.Tables[0];????????????//MessageBox.Show(dataGridView1.ColumnCount.ToString());????????????dataGridView1.Columns[0].SortMode=DataGridViewColumnSortMode.NotSortable;????????????dataGridView1.Columns[1].SortMode=DataGridViewColumn
4、SortMode.NotSortable;????????????dataGridView1.Columns[2].SortMode=DataGridViewColumnSortMode.NotSortable;????????????dataGridView1.Columns[0].AutoSizeMode=DataGridViewAutoSizeColumnMode.AllCells;????????????dataGridView1.Columns[1].AutoSizeMode=DataGri
5、dViewAutoSizeColumnMode.AllCells;????????????dataGridView1.Columns[2].AutoSizeMode=DataGridViewAutoSizeColumnMode.AllCells;????????????dataGridView1.ReadOnly=true;Excel.Application方法?程序代碼usingExcel=Microsoft.Office.Interop.Excel;usingSystem.Reflection;?
6、????????????Excel.ApplicationoXL=newMicrosoft.Office.Interop.Excel.Application();????????????oXL.Visible=true;????????????Excel._WorkbookoWB2=(Excel._Workbook)oXL.Workbooks.Open(@"f:test.xls",?????????????????Missing.Value,?????????????????Missing.Valu
7、e,?????????????????Missing.Value,?????????????????Missing.Value,?????????????????Missing.Value,?????????????????Missing.Value,?????????????????Missing.Value,?????????????????Missing.Value,?????????????????Missing.Value,?????????????????Missing.Value,???
8、??????????????Missing.Value,?????????????????Missing.Value,?????????????????Missing.Value,?????????????????Missing.Value????????????????);????????????Excel._WorksheetoSheet=(Excel._Worksheet)oWB.ActiveSheet;????????????Excel.Shap