如何使用Excel Data Reader(以前承載here)codeplex上的開源項目?它的工作非常適合我從Excel表導出數據。
指定的鏈接中給出的樣本代碼:
FileStream stream = File.Open(filePath, FileMode.Open, FileAccess.Read);
//1. Reading from a binary Excel file ('97-2003 format; *.xls)
IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
//...
//2. Reading from a OpenXml Excel file (2007 format; *.xlsx)
IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
//...
//3. DataSet - The result of each spreadsheet will be created in the result.Tables
DataSet result = excelReader.AsDataSet();
//...
//4. DataSet - Create column names from first row
excelReader.IsFirstRowAsColumnNames = true;
DataSet result = excelReader.AsDataSet();
//5. Data Reader methods
while (excelReader.Read())
{
//excelReader.GetInt32(0);
}
//6. Free resources (IExcelDataReader is IDisposable)
excelReader.Close();
UPDATE
周圍的一些搜索後,我遇到了這篇文章:Faster MS Excel Reading using Office Interop Assemblies。該文章僅使用Office Interop Assemblies
從給定的Excel工作表讀取數據。源代碼是該項目也在那裏。我想這篇文章可以成爲你嘗試實現的起點。看看是否有幫助
更新2
下面的代碼需要一個excel workbook
並讀取找到的所有值,每個excel worksheet
的excel workbook
內。
private static void TestExcel()
{
ApplicationClass app = new ApplicationClass();
Workbook book = null;
Range range = null;
try
{
app.Visible = false;
app.ScreenUpdating = false;
app.DisplayAlerts = false;
string execPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
book = app.Workbooks.Open(@"C:\data.xls", Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value);
foreach (Worksheet sheet in book.Worksheets)
{
Console.WriteLine(@"Values for Sheet "+sheet.Index);
// get a range to work with
range = sheet.get_Range("A1", Missing.Value);
// get the end of values to the right (will stop at the first empty cell)
range = range.get_End(XlDirection.xlToRight);
// get the end of values toward the bottom, looking in the last column (will stop at first empty cell)
range = range.get_End(XlDirection.xlDown);
// get the address of the bottom, right cell
string downAddress = range.get_Address(
false, false, XlReferenceStyle.xlA1,
Type.Missing, Type.Missing);
// Get the range, then values from a1
range = sheet.get_Range("A1", downAddress);
object[,] values = (object[,]) range.Value2;
// View the values
Console.Write("\t");
Console.WriteLine();
for (int i = 1; i <= values.GetLength(0); i++)
{
for (int j = 1; j <= values.GetLength(1); j++)
{
Console.Write("{0}\t", values[i, j]);
}
Console.WriteLine();
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
range = null;
if (book != null)
book.Close(false, Missing.Value, Missing.Value);
book = null;
if (app != null)
app.Quit();
app = null;
}
}
在上面的代碼,values[i, j]
是,你需要添加到dataset
值。 i
表示該行,而j
表示該列。
添加賞金,因爲我真的很想知道,如果Interop.Excel有這個能力。如果任何人都可以在不事先了解Excel文件內容的情況下以自動方式獲取數據,我將獎勵賞金。 –
這是可能的_if_你可以事先保證數據。我擔心的是,你希望有一些工作可以用於任何舊的工作簿,並將表格數據提取出來。該表格數據或者需要按照命名範圍來劃分,或者它必須遵循某種約定。如果遵循約定,工作簿中的每張工作表都與第1行中的標題行完全相同,那麼這將成爲可能。否則,你會運氣不好...... – adamleerich