1:引入using System.Data.OleDb;
using System.Data;
2:
private void bind()
{
if (FileUpload1.PostedFile != null)
{
string str = FileUpload1.PostedFile.FileName;
int lu = str.LastIndexOf("\\");
String filename = str.Substring(lu + 1);
FileUpload1.PostedFile.SaveAs(@Server.MapPath("xls") + filename);
// string conn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + Server.MapPath("xls") + "\\" + filename + ";Extended Properties=Excel 8.0";
string conn = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =C:/she.xls;Extended Properties=Excel 8.0";
OleDbConnection thisconnection = new OleDbConnection(conn);
thisconnection.Open();
DataTable excelShema = thisconnection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
string tableName = excelShema.Rows[0][2].ToString().Trim();
string strSql = "select * from [" + tableName + "]";
OleDbCommand objCmd = new OleDbCommand(strSql, thisconnection);
DataSet ds = new DataSet();
OleDbDataAdapter myData = new OleDbDataAdapter(strSql, thisconnection);
myData.Fill(ds, tableName);
thisconnection.Close();
int iColums = ds.Tables[tableName].Columns.Count;//列数
int iRows = ds.Tables[tableName].Rows.Count;//行数
int count = ds.Tables[tableName].Rows.Count;
C_CreditMonitorInfo infot = new C_CreditMonitorInfo();
string[,] storedata = new string[iRows, iColums];
// C_CreditMonitorInfo infot1 = new C_CreditMonitorInfo();
for (int i = 0; i < iRows; i++)
{
//for (int j = 0; j < iColums; j++)
//{
//将Excel表中的数据存储到数组
int j = 0;
string st = ds.Tables[tableName].Rows[i][j].ToString();
string sr = ds.Tables[tableName].Rows[i][j + 1].ToString();
}
}
}