C#读取 excel中的表名sheet名不是默认的sheet1,shee2就取不到值了 c# 如何读取自定义的sheet值?c#读取excel最佳解决方案
2012-09-03 11:07
555 查看
亲测
/// <summary>
/// 不指定sheet
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static DataTable ExcelToDataTable_sheet(string fileName,string sheet)
{
OleDbConnection conn = null;
DataTable dt = new DataTable();
string strCon = "";
//string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" + fileName;
if (fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower().Equals("xlsx"))
{
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=No;IMEX=1';Data Source=" + fileName;
}
else if (fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower().Equals("xls"))
{
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=No;IMEX=1';Data Source=" + fileName;
}
else
{
MessageBox.Show("选择的文件格式不对!", "错误");
return null;
}
try
{
string sheetname = "";
List<string> sheetnamelist = new List<string>();
conn = new OleDbConnection(strCon);
conn.Open();
System.Data.DataTable dt_excel = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
for (int i = 0; i < dt_excel.Rows.Count; i++)
{//循环取得所有表名
sheetname = dt_excel.Rows[i]["TABLE_NAME"].ToString();
sheetnamelist.Add(sheetname);
}
string CurrSheet = sheetnamelist[0].ToString();//取得第一个表名
string strCom = "Select * From [" + CurrSheet + "]";
OleDbDataAdapter adp = new OleDbDataAdapter(strCom, conn);
adp.Fill(dt);
return dt;
}
catch (Exception)
{
return null;
}
finally
{
if (conn != null && conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
}
/// <summary>
/// 不指定sheet
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static DataTable ExcelToDataTable_sheet(string fileName,string sheet)
{
OleDbConnection conn = null;
DataTable dt = new DataTable();
string strCon = "";
//string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" + fileName;
if (fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower().Equals("xlsx"))
{
strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties='Excel 12.0;HDR=No;IMEX=1';Data Source=" + fileName;
}
else if (fileName.Substring(fileName.LastIndexOf(".") + 1).ToLower().Equals("xls"))
{
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=No;IMEX=1';Data Source=" + fileName;
}
else
{
MessageBox.Show("选择的文件格式不对!", "错误");
return null;
}
try
{
string sheetname = "";
List<string> sheetnamelist = new List<string>();
conn = new OleDbConnection(strCon);
conn.Open();
System.Data.DataTable dt_excel = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
for (int i = 0; i < dt_excel.Rows.Count; i++)
{//循环取得所有表名
sheetname = dt_excel.Rows[i]["TABLE_NAME"].ToString();
sheetnamelist.Add(sheetname);
}
string CurrSheet = sheetnamelist[0].ToString();//取得第一个表名
string strCom = "Select * From [" + CurrSheet + "]";
OleDbDataAdapter adp = new OleDbDataAdapter(strCom, conn);
adp.Fill(dt);
return dt;
}
catch (Exception)
{
return null;
}
finally
{
if (conn != null && conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
}
相关文章推荐
- C#读取EXCEL中工作表不是Sheet1等默认名称
- C# conn.open() 外部表不是预期的格式( 读取EXCEL文件出错)
- C# 读取Excel中指定的Sheet名称或索引
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- Asp.Net读取excel文件的时候 出错提示:外部表不是预期的格式 解决方案
- C# 读取Excel时读取不到第一行的问题。
- [二合一]C#读取和导出EXCEL类库(可操作多个SHEET)
- C#读取Excel多个sheet
- C#读取Excel表中的数据时,为何有些行的字段内容读取不到
- C# 如何直接读取Excel表
- Asp.Net读取excel文件的时候 出错提示:外部表不是预期的格式 解决方案
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- 【Bug】c#使用oleDB读excel读取不到最后一列,把Excel表格打开随便改动一列的格式就能读取出来了。请问这是什么问题
- C# 读取excel 几种方法的集合 读取excel多sheet
- C#读取excel出错提示外部表不是预期的格式
- Office编程(二)C#读取Excel并存入数据库,通过XML自定义表名,是否重建并插入数据
- C# 读取Excel中指定的Sheet名称或索引(转)
- C# WinForm中如何自定义config文件(XML文件),并且读取和保存它
- 如何让theme是自定义dialog样式的activity横向全屏显示(默认是和普通dialog那样,横向不是布满屏幕的)
- 如何使用c#读取Excel数据?