나는 (겉으로보기에) 기본적으로 StackOverflow에 의존하는 것을 싫어하지만 지난 몇 시간 동안 Microsoft와 싸워 왔으며 막 다른 골목을 치고있는 것처럼 보입니다. 나는 (큰) Excel 2007+ 스프레드 시트를 읽으려고 노력하고 있으며, Google은 OpenXml SDK를 사용하는 것이 꽤 대중적인 선택이라고 친절하게 알려주었습니다. 그래서 나는 그 일을 포기하고, 튜토리얼을 읽고, 마이크로 소프트 자신의 라이브러리 페이지를 확인하고, 그 중 거의 아무것도 얻지 못했습니다.OpenXmlReader 사용하기
하나의 숫자 열과 하나의 문자열 만 가진 작은 테스트 스프레드 시트를 사용하고 있습니다. 대규모 테스트는 나중에 나옵니다. 내가 게시하려고하는 것과 유사한 몇 가지 구현을 시도했지만 그 중 어느 것도 데이터를 읽지 않습니다. 아래의 코드는 대부분 다른 StackOverflow 스레드에서 가져온 것입니다. 나는 내가 여러분에게 오늘이 글을 쓰는 것보다 덜 부러울 것이기 때문에 내가 여러분에게이 버전을 가지고/debug/help를 할 것이라고 생각했다.
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();
}
}
그리고 부수적으로 OpenXml SDK 사용에 대한 대안이 있습니까?
오픈 XML의 SDK를 여기에 사용할 수 2.0 생산성 도구를 사용해보십시오. * xlsx *를 열고 구조를 보거나 파일을 다시 만들 수있는 C# 코드를 볼 수 있습니다. 이렇게하면 작업중인 파일에서 도달 할 값의 위치를 볼 수 있습니다. –