E a proposito di usare Excel Data Reader (precedentemente ospitato here) un progetto open source su codeplex? Funziona molto bene per me esportare i dati dai fogli Excel.
Il codice di esempio dato sul link specificato:
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
Dopo un po 'di ricerca in giro, mi sono imbattuto in questo articolo: Faster MS Excel Reading using Office Interop Assemblies. L'articolo utilizza solo Office Interop Assemblies
per leggere i dati da un determinato foglio di Excel. C'è anche il codice sorgente del progetto. Immagino che questo articolo possa essere un punto di partenza su ciò che stai cercando di ottenere. Vedere se questo aiuta
UPDATE 2
Il codice qui sotto prende un excel workbook
e legge tutti i valori trovati, per ogni excel worksheet
all'interno della 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;
}
}
Nel codice precedente, values[i, j]
è il valore che è necessario aggiungere al dataset
. i
indica la riga, mentre j
indica la colonna.
Aggiunta di taglie perché mi piacerebbe davvero sapere se Interop.Excel dispone di questa funzionalità. Assegnerò la ricompensa se chiunque può ottenere i dati in questo modo, nel modo più automatico possibile, senza una conoscenza preliminare dei contenuti del file Excel. –
Questo è possibile _if_ è possibile garantire qualcosa sui dati in anticipo. Ciò di cui ho paura è che vuoi che qualcosa funzioni per qualsiasi vecchia cartella di lavoro e inserisci i dati tabulari. Quei dati tabulari avrebbero bisogno di essere demarcati da intervalli denominati o avrebbero dovuto seguire una sorta di convenzione. Se seguiva la convenzione che ogni foglio nella cartella di lavoro sembrava esattamente come un set di record con una riga di intestazione nella riga 1, allora sarebbe possibile. Altrimenti, saresti sfortunato ... – adamleerich