62,046
社区成员
发帖
与我相关
我的任务
分享
private DataTable getTable(string fileName)
{
try
{
//判断文件是否存在
if (!File.Exists(fileName)) { Response.Write("<script>alert('文件不存在,请重新上传。');</script>"); return null; }
DataSet ds = new DataSet();
OleDbConnection objConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";" + "Extended Properties='Excel 8.0;IMEX=1'"); //Excel 8.0;HDR=NO;IMEX=1中的IMEX=0;
objConn.Open();
DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
string tableName = schemaTable.Rows[0][2].ToString().Trim();//获取 Excel 的表名,默认值是sheet1
//string queryField = "型号,品牌,封装,年份,包装,数量,单价,订货周期,备注";
//string strSql = "select " + queryField + " from [" + tableName + "]";
string strSql = "select * from [" + tableName + "]";
//string strSql = "select * from [" + tableName + "]";
OleDbCommand objCmd = new OleDbCommand(strSql, objConn);
OleDbDataAdapter myData = new OleDbDataAdapter(strSql, objConn);
myData.Fill(ds, tableName);//填充数据
return ds.Tables[0];
}
catch (Exception ex)
{
//获取文件名
string fName = fileName.Substring(fileName.LastIndexOf("\\") + 1);
//MessageBox.Show(ex.Message);
return null;
}
}
这是我读取excel的文件。本地IDE测试没问题。但在本地IIS就不行。这代码有问题吗。