编辑-更多信息
以下是来自xl文件夹内style.xml文件中194号数字格式的示例。
Excel表格来自世界上不同的地区,因此我认为数字格式不同,但它们是否重叠?在不同的文化设置中,numFmtId 194 是否会变成除了日期之外的其他东西?
以下是我如何将类似于“40574”这样的 c.CellValues 转换为日期,但问题是如何知道“40574”是一个日期而不是一个数字?
DateTime.FromOADate(Convert.ToDouble(c.CellValue.Text));
目前我是通过检查是否存在 DataType,然后检查 CellFormat 来完成这个操作,但是当某些 NumberFormatId 不在我的检查范围内时就会出现问题。
private Object FormatCellValue(Cell c, SharedStringTable ssTable, CellFormats cellFormats)
{
if (c.CellValue != null)
{
// If there is no data type, this must be a string that has been formatted as a number
if (c.DataType == null)
{
CellFormat cf;
if (c.StyleIndex == null)
{
cf = cellFormats.Descendants<CellFormat>().ElementAt<CellFormat>(0);
}
else
{
cf = cellFormats.Descendants<CellFormat>().ElementAt<CellFormat>(Convert.ToInt32(c.StyleIndex.Value));
}
if ((cf.NumberFormatId >= 14 && cf.NumberFormatId <= 22) ||
(cf.NumberFormatId >= 165 && cf.NumberFormatId <= 180) ||
cf.NumberFormatId == 278 || cf.NumberFormatId == 185 || cf.NumberFormatId == 196 ||
cf.NumberFormatId == 217 || cf.NumberFormatId == 326) // Dates
{
try
{
DateTime dt;
dt = DateTime.FromOADate(Convert.ToDouble(c.CellValue.Text));
...CODE CONTINUES
编辑
在我更新的帖子中,我忘记发布我在style.xml文件中找到的值:
<numFmt numFmtId="323" formatCode="mmm/yy;@"/>
所以我的问题是如何获取formatCode并解析它以确定它是否为日期?以下是数字格式化323的即时调试窗口输出:{DocumentFormat.OpenXml.Spreadsheet.CellFormat}
base {DocumentFormat.OpenXml.OpenXmlCompositeElement}: {DocumentFormat.OpenXml.Spreadsheet.CellFormat}
Alignment: {DocumentFormat.OpenXml.Spreadsheet.Alignment}
ApplyAlignment: "1"
ApplyBorder: "1"
ApplyFill: "1"
ApplyFont: "1"
ApplyNumberFormat: "1"
ApplyProtection: "1"
BorderId: "64"
ExtensionList: null
FillId: "0"
FontId: "83"
FormatId: "37992"
LocalName: "xf"
NumberFormatId: "323"
PivotButton: null
Protection: {DocumentFormat.OpenXml.Spreadsheet.Protection}
QuotePrefix: "1"