내 프로그램이 Excel 문서를 스캔하여 특정 셀의 값을 가져 와서 목록에 넣어야합니다.값이 숫자인지 또는 공유 문자열 테이블인지 OpenXML
그러나 스프레드 시트의 데이터는 모두 일반 형식으로되어 있으며 항상 SST에서 값을 조회합니다. 어쨌든 생각하고 목록의 스프레드 시트에 숫자 값을 넣지 않습니다.
내 프로그램에 데이터가 SST에 대한 참조가 아니라 번호임을 알리는 방법은 무엇입니까?
foreach (Cell cell in row.Elements<Cell>())
{
try
{
cellvalue1 = cell.CellValue.InnerText;
if (cell.DataType == CellValues.SharedString && cellvalue2.Any(char.IsDigit))
{
cellvalue2 = ssT.ElementAt(Int32.Parse(cellvalue1)).InnerText;
}
else
{
cellvalue2 = cell.CellValue.ToString();
}
}
catch (Exception)
{
cellvalue2 = " ";
}
switch (cellvalue2)
{
case ("WELL NAME and NUMBER"):
WellnameCol = GetColumnName(cell.CellReference);
break;
case ("FLOWING PRESSURE"):
FlowpCol = GetColumnName(cell.CellReference);
break;
case ("SHUT-IN PRESSURE"):
ShutpCol = GetColumnName(cell.CellReference);
break;
default:
if (GetColumnName(cell.CellReference) == WellnameCol)
{
if (cellvalue2.Contains("#"))
{
Wellname.Add(cellvalue2);
inRow = true;
}
else
{
inRow = false;
}
}
else if (GetColumnName(cell.CellReference) == FlowpCol)
{
if (!cellvalue2.Contains("#") && inRow)
Flowp.Add(cellvalue2);
}
else if (GetColumnName(cell.CellReference) == ShutpCol)
{
if (inRow)
{
ShutP.Add(cellvalue2);
}
}
break;
}
}
은 try 캐치 문은 셀이 비어인지 여부를 확인하는 것입니다 후 빈 문자열 그렇다면 같은 셀을 반환합니다.
모두에게 감사드립니다.
을하지만 그것은 단지 작은 조각이다 내 코드는 각 셀을 통과하는 내부 루프입니다. –
간단한 실수가있는 것처럼 보입니까? 변경 "cellvalue2 = cell.CellValue.ToString();" ~ "cellvalue2 = cell.CellValue.InnerText;" 또는 단순히 "cellvalue2 = cellvalue1"? CellValue는 System.Object에서 상속 된 ToString()을 재정의하지 않습니다. https://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.cellvalue_methods(v=office.15).aspx. –
@JackMiller 'cell.DataType == CellValues.SharedString'은'cell.DataType.Value == CellValues.SharedString'이어야합니다. – sly