查看列表here,您可以看到:
你可以看到大多数案件似乎已被覆盖,22个非常接近,但我真正需要的是 m / d / yyyy h:mm tt - 是否有人知道在OpenXml中设置它的方法吗?谢谢。
答案 0 :(得分:1)
没有内置格式来实现您在OpenXml中完成的内容,但您可以轻松添加自己的格式。您需要的格式字符串是m/d/yyyy\ h:mm\ AM/PM
。
要应用您需要创建NumberingFormats
对象的格式,请向其添加NumberingFormat
并将其分配到Stylesheet
上的NumberingFormats
媒体资源。以下方法将使用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
:
如果格式包含AM或PM,则小时基于12小时制,其中&#34; AM&#34;或&#34; A&#34;表示从午夜到中午的时间和&#34; PM&#34;或&#34; P&#34;表示从中午到午夜的时间。否则,小时基于24小时制。
因此,将24小时格式与AM / PM后缀混合是不可能的。