读取EXCEL中的数据
/// <summary>
/// 读取用户上传的EXCEL文档中的数据,并以DATATABLE的形式返回
/// </summary>
/// <param name="filePath">服务器上的路径</param>
/// <returns>相应的数据表</returns>
public DataTable ReadServerFile(string filePath)
{
if (string.IsNullOrEmpty(filePath))
{
return;
}
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection newOledbC = new OleDbConnection(connStr);
try
{
newOledbC.Open();
}
catch
{
HttpContext.Current.Response.Write("<script>打开数据源出错,请核对数据源路径</script>");
return;
}
DataTable DT = newOledbC.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
string TableName;
TableName = DT.Rows.Count > 0 ? (string)DT.Rows[0]["TABLE_NAME"].ToString() : string.Empty;
if (string.IsNullOrEmpty(TableName))
{
HttpContext.Current.Response.Write("<script>空数据表</script>");
// MessageBox.Show("空数据表", "出错", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
return;
}
OleDbDataAdapter newOleDbDA = new OleDbDataAdapter();
DataSet newDataSet = new DataSet();
TableName = TableName.TrimStart('\'');
TableName = TableName.TrimEnd('\'');
string sql = string.Format("select * from {0}{1}{2}", "[", TableName, "]");
newOleDbDA.SelectCommand = new OleDbCommand(sql, newOledbC);
try
{
newOleDbDA.Fill(newDataSet);
DataTable tempDT = newDataSet.Tables[0];
if (tempDT.Rows.Count > 0)
{
return tempDT;
//showAllData.Visible = true;
//showAllData.DataSource = tempDT;
//button3.Visible = true;
}
}
catch (Exception ex)
{
HttpContext.Current.Response.Write("<script>无效的数据</script>");
//MessageBox.Show(ex.Message);
//MessageBox.Show("无效的数据", "出错", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
return;
}
}
/// 读取用户上传的EXCEL文档中的数据,并以DATATABLE的形式返回
/// </summary>
/// <param name="filePath">服务器上的路径</param>
/// <returns>相应的数据表</returns>
public DataTable ReadServerFile(string filePath)
{
if (string.IsNullOrEmpty(filePath))
{
return;
}
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection newOledbC = new OleDbConnection(connStr);
try
{
newOledbC.Open();
}
catch
{
HttpContext.Current.Response.Write("<script>打开数据源出错,请核对数据源路径</script>");
return;
}
DataTable DT = newOledbC.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });
string TableName;
TableName = DT.Rows.Count > 0 ? (string)DT.Rows[0]["TABLE_NAME"].ToString() : string.Empty;
if (string.IsNullOrEmpty(TableName))
{
HttpContext.Current.Response.Write("<script>空数据表</script>");
// MessageBox.Show("空数据表", "出错", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
return;
}
OleDbDataAdapter newOleDbDA = new OleDbDataAdapter();
DataSet newDataSet = new DataSet();
TableName = TableName.TrimStart('\'');
TableName = TableName.TrimEnd('\'');
string sql = string.Format("select * from {0}{1}{2}", "[", TableName, "]");
newOleDbDA.SelectCommand = new OleDbCommand(sql, newOledbC);
try
{
newOleDbDA.Fill(newDataSet);
DataTable tempDT = newDataSet.Tables[0];
if (tempDT.Rows.Count > 0)
{
return tempDT;
//showAllData.Visible = true;
//showAllData.DataSource = tempDT;
//button3.Visible = true;
}
}
catch (Exception ex)
{
HttpContext.Current.Response.Write("<script>无效的数据</script>");
//MessageBox.Show(ex.Message);
//MessageBox.Show("无效的数据", "出错", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
return;
}
}