原始查詢: 目前我想從 32 位構建切換到 64 位,但我無法找到任何庫以從 xls 中提取資料。我可以在 ClosedXML 上與 (xlsx, xlsm) 等其他人一起作業,不幸的是不支持 xls。
所以現在這種聯系是站不住腳的。
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" path ";Extended Properties='Excel 12.0;HDR=NO,IMEX=1'";
string excelQuery = "SELECT * FROM [monthly$A:G]";
為 OLE 安裝 64 位驅動程式是不可能的。所以也許有人偶然發現了它,或者有任何想法。
我想將資料構建到資料表中,其標題類似于HDR=YES上面的示例,并且可以將其關閉。
另外,由于 excel 格式,dapper 是不可能的:(
我試圖找到一些問題,但我能找到的只是安裝 64 位的 OLEDB 驅動程式。
編輯:
我不明白為什么有人會選擇 Panagiotis 的答案,因為它已得到糾正,這就是我接受它的原因。他的使用建議ExcelDataReader被證明是最有效的,因為我在應用程式中使用了更多格式的 excel .xlsx,如.xlsm&.xlsb在應用程式中,另外還有一些.csv檔案。這讓我能夠構建一個模塊,這就足夠了。
他還正確地指出這.xls是古老的,盡管不幸的是它并沒有過時,因為我公司的許多流程仍然依賴它。但它應該已經過時了,不幸的是,由于流程計劃不周,我們被困住了,而不是因為.xls它有用。尤其是那些公司應該注意某些格式不受支持的事實,或者可能會失去這種支持,因為.xls正在慢慢獲得這種待遇。
我實施了兩個建議ExcelDataReader,Aspose.Cells因為xlsb第一個有問題。雖然我的問題是針對.xls所以他的問題是最有幫助的,而且ExcelDataReader如果涉及到提取速度,它的效率要高得多。
感謝您的幫助,非常感謝。
初始代碼
/// <summary>
/// gets data of passed file into DataTable
/// </summary>
/// <param name="_filePath">full path to needed file</param>
public void GetData(string _filePath, string _sheetName = null)
{
try
{
string extension = Path.GetExtension(_filePath);
switch (extension)
{
case ".csv":
ReaderCSV(_filePath);
break;
case ".xlsb":
ReaderXLSB(_filePath, _sheetName);
break;
default:
ReaderDefault(_filePath, "Full list");
break;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
/// <summary>
/// Auto-detect format, supports:
// Binary Excel files (2.0-2003 format; *.xls)
// OpenXml Excel files (2007 format; *.xlsx, *.xlsm)
/// </summary>
/// <param name="_stream">path of needed file</param>
private void ReaderDefault(string _filePath, string _sheetName = null)
{
using (FileStream stream = File.Open(_filePath, FileMode.Open, FileAccess.Read))
{
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
var result = reader.AsDataSet();
/*action to format dataset*/
}
}
}
/// <summary>
/// CSV files (.csv)
/// </summary>
/// <param name="_stream">path of needed file</param>
private void ReaderCSV(string _filePath)
{
using (FileStream stream = File.Open(_filePath, FileMode.Open, FileAccess.Read))
{
using (var reader = ExcelReaderFactory.CreateCsvReader(stream))
{
var result = reader.AsDataSet();
/*action to format dataset*/
}
}
}
/// <summary>
/// xlsb files (.xlsb)
/// </summary>
/// <param name="_filePath">path of needed file</param>
/// <param name="_neededSheet">name of needed sheet</param>
private void ReaderXLSB(string _filePath, string _neededSheet = null)
{
Workbook workbook = new Workbook(_filePath);
Worksheet worksheet = null;
if (_neededSheet != null)
{
//check for needed worksheet
foreach (Worksheet ws in workbook.Worksheets)
{
if (ws.Name == _neededSheet) { worksheet = ws; break; }
}
}
else
{
//get first worksheet
worksheet = workbook.Worksheets[0];
}
//check if worksheet was selected
if (worksheet == null) throw new Exception("Needed sheet was not located!");
int totalRows = worksheet.Cells.MaxDataRow;
int totalColumns = worksheet.Cells.MaxDataColumn;
ExportTableOptions tableOptions = new ExportTableOptions();
tableOptions.ExportAsString = true;
tableOptions.FormatStrategy = CellValueFormatStrategy.None;
tableOptions.ExportColumnName = true;
//(int = first row, int = first columns, int = total rows, int = total columns, bool = if 1st row is header)
DataTable = worksheet.Cells.ExportDataTable(0,0, totalRows, totalColumns, tableOptions);
/*action to format datatable*/
}
uj5u.com熱心網友回復:
首先,不要一開始就使用 XLS。這種格式在 2007 年被取代xlsx——那是15年前。所有Excel 版本都使用xlsx,所有應用程式也是如此。沒有兼容性問題 - 事實上,xls導致兼容性問題的是古代。xls例如,除非您付費,否則Google 表格不支持檔案。唯一xls存在的原因是慣性。
如果您堅持使用xls(為什么?),您可以使用ExcelDataReader。該庫可以讀取xlsandxlsx檔案并回傳 anDbDataReader或DataSet. NuGet 包的下載量為 1700 萬次,使其成為繼 EPPlus 之后第二受歡迎的 Excel 庫。
存盤庫頁面中的示例顯示了如何在最簡單的情況下使用它 -ExcelReaderFactory.CreateReader(stream)回傳一個DbDataReader:
using (var stream = File.Open(filePath, FileMode.Open, FileAccess.Read))
{
// Auto-detect format, supports:
// - Binary Excel files (2.0-2003 format; *.xls)
// - OpenXml Excel files (2007 format; *.xlsx, *.xlsb)
using (var reader = ExcelReaderFactory.CreateReader(stream))
{
// Choose one of either 1 or 2:
// 1. Use the reader methods
do
{
while (reader.Read())
{
// reader.GetDouble(0);
}
} while (reader.NextResult());
// 2. Use the AsDataSet extension method
var result = reader.AsDataSet();
// The result of each spreadsheet is in result.Tables
}
}
uj5u.com熱心網友回復:
您可以查看我的圖書館Sylvan.Data.Excel。它沒有外部依賴,是跨平臺的、開源的、MIT 許可的,也是.NET 上最快的 Excel 資料閱讀器。它同時支持 .xls 和 .xlsx。但是,它不支持 .xlsb。它不像其他一些庫那樣功能齊全,它只支持讀取(不支持寫入),但它應該是 ACE 的直接替代品,因為它實作了DbDataReader. 如果您遇到任何問題/疑問,請隨時在 GitHub 存盤庫中打開它們。
使用它非常簡單:
var edr = ExcelDataReader.Create("data.xls");
while(edr.Read()) {
for(int i = 0; i < edr.FieldCount; i ) {
var value = edr.GetString(i);
}
}
默認情況下,它將期望每張作業表的第一行包含標題。然而,這可以被禁用。禁用時,只能按序號訪問列。
var opts = new ExcelDataReaderOptions { Schema = ExcelSchema.NoHeaders };
var edr = ExcelDataReader.Create("data.xls", opts);
由于它實作DbDataReader了,將其加載到資料表中是單行的:
var dt = new DataTable();
dt.Load(edr);
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/411949.html
標籤:
