1
使用Oledb,是否可以在Excel中获取特殊工作表的所有NamedRanges?C# - 使用Oledb获取Excel中特定工作表的名称范围
我写了下面的代码,它给了我NamedRanges,但我无法弄清楚NamedRange引用哪张表。
private String[] GetExcelSheetNames(string excelFilePath)
{
OleDbConnection objConn = null;
System.Data.DataTable dt = null;
try
{
//String connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelFile + ";Extended Properties=Excel 12.0;";
string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0", excelFilePath);
objConn = new OleDbConnection(connectionString);
objConn.Open();
// Get the data table containg the schema guid.
dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables_Info, null);
if (dt == null)
return null;
String[] excelSheets = new String[dt.Rows.Count];
int i = 0;
// Add the sheet name to the string array.
foreach (DataRow row in dt.Rows)
excelSheets[i++] = row["TABLE_NAME"].ToString();
return excelSheets;
}
catch (Exception ex)
{
return null;
}
finally
{
// Clean up.
if (objConn != null)
{
objConn.Close();
objConn.Dispose();
}
if (dt != null)
{
dt.Dispose();
}
}
}
我猜你需要[打开XML SDK(http://www.microsoft.com/en-us/download/details.aspx?id=5124)的这种努力。这样您可以动态访问名称管理器。否则,只有在您事先知道您的命名范围的情况下,您才能从表格中获取值。 – 2014-10-18 11:29:22
@ andrei.ciprian:谢谢你的回复。我正在离开想法,通过表名读取命名范围。您是否认为可以使用Oledb或LinqToExcel包来读取命名范围的描述?谢谢! – Tejas 2014-10-18 17:41:50