gpt4 book ai didi

c#-4.0 - 如何在 OpenXML 电子表格中区分内联数字和 OLE 自动化日期数字?

转载 作者:行者123 更新时间:2023-12-01 09:00:02 25 4
gpt4 key购买 nike

我得消耗一些 xlsx文件。我已阅读 Reading a date from xlsx using open xml sdkhttp://www.dotnetperls.com/fromoadate .我的大部分专栏都是文本(共享字符串),但也有一些数字(整数),我也有一些日期和日期时间。
我正在使用 OpenXML SDK 2.5。

我的问题是我不知道如何区分实际数字和日期。他们俩都有DataTypenull ,文本数字表示在 Text细胞的属性。

一些代码:

  using (var xlsxStream = assembly.GetManifestResourceStream("Checklist.xlsx"))
using (var spreadsheetDocument = SpreadsheetDocument.Open(xlsxStream, false))
{
var workbookPart = spreadsheetDocument.WorkbookPart;
var sharedStringTable = workbookPart.SharedStringTablePart.SharedStringTable;
var worksheetPart = workbookPart.WorksheetParts.First();
var sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
string text;
foreach (Row r in sheetData.Elements<Row>())
{
foreach (Cell c in r.Elements<Cell>())
{
if (c.CellValue != null)
{
text = c.CellValue.Text;
if (c.DataType != null)
{
if (c.DataType.Value == CellValues.SharedString)
{
int tableIndex = int.Parse(text);
text = sharedStringTable.ChildElements[tableIndex].InnerText;
}
// note: the date cells do not have c.DataType.Value == CellValues.Date
// Their c.DataType is null, if they are OLE Automation date numbers
}
// So here I am, and I'd need to know if the number supposed to be an
// OLE Automation date or a number, so I can transform it if needed.
//if (it's a date) // <- ?????
//{
// double dateDouble = double.Parse(text);
// DateTime dateTime = DateTime.FromOADate(dateDouble);
// text = dateTime.ToShortDateString();
//}
Console.Write(text + " ");
}
else
{
Console.Write("NULL" + " ");
}
}
Console.WriteLine();
}
Console.WriteLine();
Console.ReadKey();

最佳答案

我刚刚遇到了类似的问题,检查单元格是否包含日期/时间值并不容易,请参阅 Using cell format to determine a cell contains date/time value ,但问题并没有以内置数字格式结束,我也需要处理自定义格式。 OpenXML SDK 2.5 中没有实用程序可以提供帮助,因此我必须自己编写(不支持泰语日期/时间格式)。

public class ExcelHelper
{
static uint[] builtInDateTimeNumberFormatIDs = new uint[] { 14, 15, 16, 17, 18, 19, 20, 21, 22, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 45, 46, 47, 50, 51, 52, 53, 54, 55, 56, 57, 58 };
static Dictionary<uint, NumberingFormat> builtInDateTimeNumberFormats = builtInDateTimeNumberFormatIDs.ToDictionary(id => id, id => new NumberingFormat { NumberFormatId = id });
static Regex dateTimeFormatRegex = new Regex(@"((?=([^[]*\[[^[\]]*\])*([^[]*[ymdhs]+[^\]]*))|.*\[(h|mm|ss)\].*)", RegexOptions.Compiled);

public static Dictionary<uint, NumberingFormat> GetDateTimeCellFormats(WorkbookPart workbookPart)
{
var dateNumberFormats = workbookPart.WorkbookStylesPart.Stylesheet.NumberingFormats
.Descendants<NumberingFormat>()
.Where(nf => dateTimeFormatRegex.Match(nf.FormatCode.Value).Success)
.ToDictionary(nf => nf.NumberFormatId.Value);

var cellFormats = workbookPart.WorkbookStylesPart.Stylesheet.CellFormats
.Descendants<CellFormat>();

var dateCellFormats = new Dictionary<uint, NumberingFormat>();
uint styleIndex = 0;
foreach (var cellFormat in cellFormats)
{
if (cellFormat.ApplyNumberFormat != null && cellFormat.ApplyNumberFormat.Value)
{
if (dateNumberFormats.ContainsKey(cellFormat.NumberFormatId.Value))
{
dateCellFormats.Add(styleIndex, dateNumberFormats[cellFormat.NumberFormatId.Value]);
}
else if (builtInDateTimeNumberFormats.ContainsKey(cellFormat.NumberFormatId.Value))
{
dateCellFormats.Add(styleIndex, builtInDateTimeNumberFormats[cellFormat.NumberFormatId.Value]);
}
}

styleIndex++;
}

return dateCellFormats;
}

// Usage Example
public static bool IsDateTimeCell(WorkbookPart workbookPart, Cell cell)
{
if (cell.StyleIndex == null)
return false;

var dateTimeCellFormats = ExcelHelper.GetDateTimeCellFormats(workbookPart);

return dateTimeCellFormats.ContainsKey(cell.StyleIndex);
}
}

关于c#-4.0 - 如何在 OpenXML 电子表格中区分内联数字和 OLE 自动化日期数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19034805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com