Usando o OpenXmlReader
Eu odeio recorrer ao StackOverflow para algo tão (aparentemente) básico, mas eu tenho lutado com a Microsoft pelas últimas horas e parece estar chegando a um beco sem saída. Estou tentando ler planilhas do Excel 2007+ e o Google gentilmente me informou que o uso do OpenXml SDK é uma escolha bastante popular. Então, dei uma chance à coisa, li alguns tutoriais, verifiquei as páginas da própria biblioteca da Microsoft e fiquei muito pequena com todas elas.
Eu estou usando uma pequena planilha de teste com apenas uma coluna de números e uma das seqüências de caracteres - testes em larga escala virão mais tarde. Eu tentei várias implementações semelhantes ao que estou prestes a postar, e nenhuma delas lê dados. O código abaixo foi retirado de outro thread do StackOverflow, onde parecia ter funcionado - não para mim. Eu imaginei que vocês veriam / depurariam / ajudariam com esta versão, porque ela provavelmente será menos quebrada do que qualquer coisa que eu tenha escrito hoje.
<code>static void ReadExcelFileSAX(string fileName) { using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, true)) { WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart; WorksheetPart worksheetPart = workbookPart.WorksheetParts.First(); OpenXmlPartReader reader = new OpenXmlPartReader(worksheetPart); string text; string rowNum; while (reader.Read()) { if (reader.ElementType == typeof(Row)) { do { if (reader.HasAttributes) { rowNum = reader.Attributes.First(a => a.LocalName == "r").Value; Console.Write("rowNum: " + rowNum); //we never even get here, I tested it with a breakpoint } } while (reader.ReadNextSibling()); // Skip to the next row Console.ReadKey(); break; // We just looped through all the rows so no need to continue reading the worksheet } if (reader.ElementType == typeof(Cell)) { } if (reader.ElementType != typeof(Worksheet)) // Dont' want to skip the contents of the worksheet reader.Skip(); // Skip contents of any node before finding the first row. } reader.Close(); Console.WriteLine(); Console.ReadKey(); } } </code>
E, em uma nota lateral, existem algumas boas alternativas para usar o SDK do OpenXml que perdi de alguma forma?