"m/d/yyyy h:mm tt" [AM/PM] 是否有 OpenXml (C#) date/time 格式?
Is there an OpenXml (C#) date/time format for "m/d/yyyy h:mm tt" [AM/PM]?
查看列表here,您可以看到:
- 0 - 一般
- 1 - 0
- 2 - 0.00
- 3 - #,##0
- 4 - #,##0.00
- 9 - 0%
- 10 - 0.00%
- 11 - 0.00E+00
- 12 - # ?/?
- 13-#??/??
- 14 - d/m/yyyy
- 15 - d-mmm-yy
- 16 - d-mmm
- 17 - 年月
- 18 - h:mm tt
- 19 - h:mm:ss tt
- 20 - H:mm
- 21 - H:mm:ss
- 22 - m/d/yyyy H:mm
- 37 - #,##0 ;(#,##0)
- 38 - #,##0 ;红色
- 39 - #,##0.00;(#,##0.00)
- 40 - #,##0.00;红色
- 45 - mm:ss
- 46 - [h]:mm:ss
- 47 - mmss.0
- 48 - ##0.0E+0
- 49 - @
你可以看到大多数案例似乎都涵盖了,22个已经很接近了,但我真正需要的是m/d/yyyyh:mm tt - 有谁知道在 OpenXml 中设置它的方法吗?谢谢。
没有内置格式来实现您在 OpenXml 中所追求的,但您可以轻松添加自己的格式。您需要的格式字符串是 m/d/yyyy\ h:mm\ AM/PM
.
为了应用格式,您需要创建一个 NumberingFormats
object, add a NumberingFormat
to it and assign it to the NumberingFormats
property on a Stylesheet
。以下方法将创建 Stylesheet
格式为 m/d/yyyy\ h:mm\ AM/PM
:
private static Stylesheet CreateStyles()
{
Stylesheet styleSheet = new Stylesheet();
NumberingFormats nfs = new NumberingFormats();
NumberingFormat nf;
nf = new NumberingFormat();
nf.NumberFormatId = 165;
nf.FormatCode = "m/d/yyyy\ h:mm\ AM/PM";
nfs.Append(nf);
CellFormat cf = new CellFormat();
cf.NumberFormatId = nf.NumberFormatId;
cf.ApplyNumberFormat = true;
CellFormats cfs = new CellFormats();
cfs.Append(cf);
styleSheet.CellFormats = cfs;
styleSheet.NumberingFormats = nfs;
styleSheet.Borders = new Borders();
styleSheet.Borders.Append(new Border());
styleSheet.Fills = new Fills();
styleSheet.Fills.Append(new Fill());
styleSheet.Fonts = new Fonts();
styleSheet.Fonts.Append(new Font());
CellStyles css = new CellStyles();
CellStyle cs = new CellStyle();
cs.FormatId = 0;
cs.BuiltinId = 0;
css.Append(cs);
css.Count = UInt32Value.FromUInt32((uint)css.ChildElements.Count);
styleSheet.Append(css);
return styleSheet;
}
以下代码将使用上面添加的格式从头开始创建一个包含 2 个日期(在 A1 和 B1 中)的新文件:
public static void CreateFile(string filename)
{
using (SpreadsheetDocument spreadsheetDocument =
SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook))
{
// Add a WorkbookPart to the document.
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
workbookpart.AddNewPart<WorkbookStylesPart>();
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
// Add Sheets to the Workbook.
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
// Append a new worksheet and associate it with the workbook.
Sheet sheet = new Sheet()
{
Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
SheetId = 1,
Name = "Sheet 1"
};
sheets.Append(sheet);
Worksheet worksheet = new Worksheet();
SheetData sheetData = new SheetData();
Stylesheet styleSheet = CreateStyles();
Row row = CreateRow();
sheetData.Append(row);
worksheet.Append(sheetData);
worksheetPart.Worksheet = worksheet;
workbookpart.WorkbookStylesPart.Stylesheet = styleSheet;
// Close the document.
spreadsheetDocument.Close();
}
}
private static Row CreateRow()
{
Row row = new Row();
DateTime now = DateTime.UtcNow;
//add a date cell using the number data type
Cell cell = new Cell();
cell.StyleIndex = 0;
cell.DataType = CellValues.Number;
string columnValue = now.ToOADate().ToString();
cell.CellValue = new CellValue(columnValue);
row.Append(cell);
//add a date cell using the date data type
Cell cell2 = new Cell();
cell2.StyleIndex = 0;
cell2.DataType = CellValues.Date;
columnValue = now.ToString("o");
cell2.CellValue = new CellValue(columnValue);
row.Append(cell2);
return row;
}
原始答案(基于带有 AM/PM 说明符的 24 小时制)
很遗憾,我认为这是不可能的。
您可以定义自定义格式但是规范的部分18.8.31 numFmts
指出:
If the format contains AM or PM, the hour is based on the 12-hour clock, where "AM" or "A" indicates times from midnight until noon and "PM" or "P" indicates times from noon until midnight. Otherwise, the hour is based on the 24-hour clock.
因此,不能将 24 小时格式与 AM/PM 后缀混合使用。
查看列表here,您可以看到:
- 0 - 一般
- 1 - 0
- 2 - 0.00
- 3 - #,##0
- 4 - #,##0.00
- 9 - 0%
- 10 - 0.00%
- 11 - 0.00E+00
- 12 - # ?/?
- 13-#??/??
- 14 - d/m/yyyy
- 15 - d-mmm-yy
- 16 - d-mmm
- 17 - 年月
- 18 - h:mm tt
- 19 - h:mm:ss tt
- 20 - H:mm
- 21 - H:mm:ss
- 22 - m/d/yyyy H:mm
- 37 - #,##0 ;(#,##0)
- 38 - #,##0 ;红色
- 39 - #,##0.00;(#,##0.00)
- 40 - #,##0.00;红色
- 45 - mm:ss
- 46 - [h]:mm:ss
- 47 - mmss.0
- 48 - ##0.0E+0
- 49 - @
你可以看到大多数案例似乎都涵盖了,22个已经很接近了,但我真正需要的是m/d/yyyyh:mm tt - 有谁知道在 OpenXml 中设置它的方法吗?谢谢。
没有内置格式来实现您在 OpenXml 中所追求的,但您可以轻松添加自己的格式。您需要的格式字符串是 m/d/yyyy\ h:mm\ AM/PM
.
为了应用格式,您需要创建一个 NumberingFormats
object, add a NumberingFormat
to it and assign it to the NumberingFormats
property on a Stylesheet
。以下方法将创建 Stylesheet
格式为 m/d/yyyy\ h:mm\ AM/PM
:
private static Stylesheet CreateStyles()
{
Stylesheet styleSheet = new Stylesheet();
NumberingFormats nfs = new NumberingFormats();
NumberingFormat nf;
nf = new NumberingFormat();
nf.NumberFormatId = 165;
nf.FormatCode = "m/d/yyyy\ h:mm\ AM/PM";
nfs.Append(nf);
CellFormat cf = new CellFormat();
cf.NumberFormatId = nf.NumberFormatId;
cf.ApplyNumberFormat = true;
CellFormats cfs = new CellFormats();
cfs.Append(cf);
styleSheet.CellFormats = cfs;
styleSheet.NumberingFormats = nfs;
styleSheet.Borders = new Borders();
styleSheet.Borders.Append(new Border());
styleSheet.Fills = new Fills();
styleSheet.Fills.Append(new Fill());
styleSheet.Fonts = new Fonts();
styleSheet.Fonts.Append(new Font());
CellStyles css = new CellStyles();
CellStyle cs = new CellStyle();
cs.FormatId = 0;
cs.BuiltinId = 0;
css.Append(cs);
css.Count = UInt32Value.FromUInt32((uint)css.ChildElements.Count);
styleSheet.Append(css);
return styleSheet;
}
以下代码将使用上面添加的格式从头开始创建一个包含 2 个日期(在 A1 和 B1 中)的新文件:
public static void CreateFile(string filename)
{
using (SpreadsheetDocument spreadsheetDocument =
SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook))
{
// Add a WorkbookPart to the document.
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
workbookpart.AddNewPart<WorkbookStylesPart>();
// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
// Add Sheets to the Workbook.
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
// Append a new worksheet and associate it with the workbook.
Sheet sheet = new Sheet()
{
Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
SheetId = 1,
Name = "Sheet 1"
};
sheets.Append(sheet);
Worksheet worksheet = new Worksheet();
SheetData sheetData = new SheetData();
Stylesheet styleSheet = CreateStyles();
Row row = CreateRow();
sheetData.Append(row);
worksheet.Append(sheetData);
worksheetPart.Worksheet = worksheet;
workbookpart.WorkbookStylesPart.Stylesheet = styleSheet;
// Close the document.
spreadsheetDocument.Close();
}
}
private static Row CreateRow()
{
Row row = new Row();
DateTime now = DateTime.UtcNow;
//add a date cell using the number data type
Cell cell = new Cell();
cell.StyleIndex = 0;
cell.DataType = CellValues.Number;
string columnValue = now.ToOADate().ToString();
cell.CellValue = new CellValue(columnValue);
row.Append(cell);
//add a date cell using the date data type
Cell cell2 = new Cell();
cell2.StyleIndex = 0;
cell2.DataType = CellValues.Date;
columnValue = now.ToString("o");
cell2.CellValue = new CellValue(columnValue);
row.Append(cell2);
return row;
}
原始答案(基于带有 AM/PM 说明符的 24 小时制)
很遗憾,我认为这是不可能的。
您可以定义自定义格式但是规范的部分18.8.31 numFmts
指出:
If the format contains AM or PM, the hour is based on the 12-hour clock, where "AM" or "A" indicates times from midnight until noon and "PM" or "P" indicates times from noon until midnight. Otherwise, the hour is based on the 24-hour clock.
因此,不能将 24 小时格式与 AM/PM 后缀混合使用。