我通过OleDbConnection连接到Excel电子表格。
如何按ColumnNumber订购?我想做点什么: SELECT * FROM [Sheet1 $] ORDERBY ColumnNumber ColumnNumber是一个像1还是2的数字? 有任何想法吗 ? 注意:我正在尝试打开的文件没有标题。
private String BuildConnectionStringXLS()
{
String fileName = GetFileName();
Dictionary<string, string> props = new Dictionary<string, string>();
props["Provider"] = "Microsoft.Jet.OLEDB.4.0";
props["Data Source"] = fileName;
props["Extended Properties"] = "\"Excel 8.0;HDR=No;IMEX=1\"";
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<string, string> prop in props)
{
sb.Append(prop.Key);
sb.Append('=');
sb.Append(prop.Value);
sb.Append(';');
}
return sb.ToString();
}
public DataTable GetFullTable(int columnToOrderBy)
{
String fileName = GetFileName();
DataTable resultDataTable = new DataTable();
String connectionString = BuildConnectionString();
OleDbConnection conn = new OleDbConnection(connectionString);
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM [Sheet1$] ORDERBY ColumnNumber", conn);
da.Fill(resultDataTable);
conn.Close();
return resultDataTable;
}
答案 0 :(得分:0)
我发现你直接调用[sheet1 $]而不引用任何链接服务器是令人惊讶的。 以下是两种可能的方法:
exec sp_addLinkedServer @server='ExcelLnkdServr',
@srvproduct='ACE 12.0',
@provider='Microsoft.ACE.OLEDB.12.0',
@datasrc='\\sysdev\loadExcel.xlsx',
@provstr='Excel 12.0;HDR=Yes';
/* If the above creation is successful, you can see a listing when use SP_LINKEDSERVERS in SSMS. Now you can query the sheet using order by as below: */
select * from ExcelLnkdServr...[Sheet1$]
order by 1 asc, 2 desc
select * from openquery(ExcelLnkdServr, 'SELECT * FROM [Sheet1$] order by 1 asc, 3 desc')
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0;Database=\\sysdev\loadExcel.xlsx;HDR=Yes','Select * from [Sheet1$] order by 1 asc')