如何获取EXCEL工作表名称?如果工作表不再是sheet1、sheet2这样的默认名称时,如何获取呢?
5 个解决方案
#1
sheet對象中 [0]代表第一個工作表,[1]代表第二個工作表,以此類推,然后取name屬性(也有可能是title,試試)
#2
public static DataTable GetExcelTableName(string p_ExcelFile)
{
try
{
if (System.IO.File.Exists(p_ExcelFile))
{
OleDbConnection _ExcelConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + p_ExcelFile);
_ExcelConn.Open();
DataTable _Table = _ExcelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
_ExcelConn.Close();
return _Table;
}
return null;
}
catch
{
return null;
}
}
{
try
{
if (System.IO.File.Exists(p_ExcelFile))
{
OleDbConnection _ExcelConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + p_ExcelFile);
_ExcelConn.Open();
DataTable _Table = _ExcelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
_ExcelConn.Close();
return _Table;
}
return null;
}
catch
{
return null;
}
}
#3
// 读取Excel数据,填充DataSet
// 连接字符串
string xlsPath = Server.MapPath("~/app_data/somefile.xls");
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\";" + // 指定扩展属性为 Microsoft Excel 8.0 (97) 9.0 (2000) 10.0 (2002),并且第一行作为数据返回,且以文本方式读取
"data source=" + xlsPath;
string sql_F = "SELECT * FROM [{0}]";
OleDbConnection conn = null;
OleDbDataAdapter da = null;
DataTable tblSchema = null;
IList<string> tblNames = null;
// 初始化连接,并打开
conn = new OleDbConnection(connStr);
conn.Open();
// 获取数据源的表定义元数据
//tblSchema = conn.GetSchema("Tables");
tblSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
GridView1.DataSource = tblSchema;
GridView1.DataBind();
// 关闭连接
conn.Close();
#4
string date = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString();
path += UserCompany + UserName + YearList.SelectedValue.ToString() + "年" + MonthList.SelectedValue.ToString() + "月" + date + ".xls";
FileUpload1.PostedFile.SaveAs(path);//将文件复制到服务器指定目录中
//调用获取excel为dataset的函数
DataSet data = new DataSet();
for (int i = 0; i < tablenum; i++)
{
tablename[i] = tables[i].TableName.ToString(); //获取表格名 称,excel表格中sheet的名称
}
data = GetExcelData.GetDataSetFromExcel(path);
int tablenum;
tablenum = data.Tables.Count;
//只充许一个excel中有一个sheet页,通过选择页面中表格类型确定,如需要一个excel中有多个sheet页面,则可去掉下面的判断
if (tablenum>1)
{
Response.Write("<Script Language=JavaScript>alert('excel表格中只充许有一个sheet页面!');</Script>");
return;
}
DataTable[] tables = new DataTable[tablenum];
#5
for (int i = 0; i < tablenum; i++)
{
tablename[i] = tables[i].TableName.ToString(); //获取表格名称,excel表格中sheet的名称
}
#1
sheet對象中 [0]代表第一個工作表,[1]代表第二個工作表,以此類推,然后取name屬性(也有可能是title,試試)
#2
public static DataTable GetExcelTableName(string p_ExcelFile)
{
try
{
if (System.IO.File.Exists(p_ExcelFile))
{
OleDbConnection _ExcelConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + p_ExcelFile);
_ExcelConn.Open();
DataTable _Table = _ExcelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
_ExcelConn.Close();
return _Table;
}
return null;
}
catch
{
return null;
}
}
{
try
{
if (System.IO.File.Exists(p_ExcelFile))
{
OleDbConnection _ExcelConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + p_ExcelFile);
_ExcelConn.Open();
DataTable _Table = _ExcelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
_ExcelConn.Close();
return _Table;
}
return null;
}
catch
{
return null;
}
}
#3
// 读取Excel数据,填充DataSet
// 连接字符串
string xlsPath = Server.MapPath("~/app_data/somefile.xls");
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Extended Properties=\"Excel 8.0;HDR=No;IMEX=1\";" + // 指定扩展属性为 Microsoft Excel 8.0 (97) 9.0 (2000) 10.0 (2002),并且第一行作为数据返回,且以文本方式读取
"data source=" + xlsPath;
string sql_F = "SELECT * FROM [{0}]";
OleDbConnection conn = null;
OleDbDataAdapter da = null;
DataTable tblSchema = null;
IList<string> tblNames = null;
// 初始化连接,并打开
conn = new OleDbConnection(connStr);
conn.Open();
// 获取数据源的表定义元数据
//tblSchema = conn.GetSchema("Tables");
tblSchema = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
GridView1.DataSource = tblSchema;
GridView1.DataBind();
// 关闭连接
conn.Close();
#4
string date = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString();
path += UserCompany + UserName + YearList.SelectedValue.ToString() + "年" + MonthList.SelectedValue.ToString() + "月" + date + ".xls";
FileUpload1.PostedFile.SaveAs(path);//将文件复制到服务器指定目录中
//调用获取excel为dataset的函数
DataSet data = new DataSet();
for (int i = 0; i < tablenum; i++)
{
tablename[i] = tables[i].TableName.ToString(); //获取表格名 称,excel表格中sheet的名称
}
data = GetExcelData.GetDataSetFromExcel(path);
int tablenum;
tablenum = data.Tables.Count;
//只充许一个excel中有一个sheet页,通过选择页面中表格类型确定,如需要一个excel中有多个sheet页面,则可去掉下面的判断
if (tablenum>1)
{
Response.Write("<Script Language=JavaScript>alert('excel表格中只充许有一个sheet页面!');</Script>");
return;
}
DataTable[] tables = new DataTable[tablenum];
#5
for (int i = 0; i < tablenum; i++)
{
tablename[i] = tables[i].TableName.ToString(); //获取表格名称,excel表格中sheet的名称
}