Man*_*uel 18 c# excel openxml openxml-sdk
我正在使用Microsoft Open XML SDK 2,我很难在单元格中插入日期.我可以通过设置插入数字而没有问题Cell.DataType = CellValues.Number,但是当我对日期(Cell.DataType = CellValues.Date)执行相同操作时Excel 2010崩溃(2007年也是如此).
我尝试将Cell.Text值设置为多种日期格式以及Excel的日期/数字格式无济于事.我也尝试使用样式,删除type属性,以及我扔在墙上的许多其他比萨...
有人能指出我在工作表中插入日期的例子吗?
Luk*_*uke 27
我使用了Andrew J提供的代码,但是DataType CellValues.Date为我生成了一个损坏的xlsx文件.
该DataType CellValues.Number工作对我罚款(不要忘记设置NumberFormatId):
cell.DataType = new EnumValue<CellValues>(CellValues.Number);
Run Code Online (Sandbox Code Playgroud)
我的整个代码:
DateTime valueDate = DateTime.Now;
string valueString = valueDate.ToOADate().ToString();
CellValue cellValue = new CellValue(valueString);
Cell cell = new Cell();
cell.DataType = new EnumValue<CellValues>(CellValues.Number);
cell.StyleIndex = yourStyle; //StyleIndex of CellFormat cfBaseDate -> See below
cell.Append(cellValue);
Run Code Online (Sandbox Code Playgroud)
我CellFormat在样式表的单元格的样子:
CellFormat cfBaseDate = new CellFormat() {
ApplyNumberFormat = true,
NumberFormatId = 14, //14 is a localized short Date (d/m/yyyy) -> See list below
//Some further styling parameters
};
Run Code Online (Sandbox Code Playgroud)
如果您想格式化你的日期另一种方式,这里是所有默认的Excel列表NumberFormatId的
ID FORMAT CODE 0 General 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 mmm-yy 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 ;[Red](#,##0) 39 #,##0.00;(#,##0.00) 40 #,##0.00;[Red](#,##0.00) 45 mm:ss 46 [h]:mm:ss 47 mmss.0 48 ##0.0E+0 49 @
清单来源:http://closedxml.codeplex.com/wikipage?title = NumberFormatId%20Lookup%20Table
我知道这个列表来自ClosedXML,但它在OpenXML中是相同的.
小智 14
你必须转换DateTime为double使用函数,ToOADate即:
DateTime dtValue = DateTime.Now;
string strValue = dtValue.ToOADate().ToString(CultureInfo.InvariantCulture);
Run Code Online (Sandbox Code Playgroud)
然后将其设置为 CellValue
Cell cell;
cell.DataType = new EnumValue<CellValues>(CellValues.Date);
cell.CellValue = new CellValue(strValue);
Run Code Online (Sandbox Code Playgroud)
请记住使用DateTime格式设置格式化单元格,否则您将看到double值,而不是日期.
Nen*_*nad 14
SpreadsheetDocument从头开始创建新文件时Date,要使格式化工作,Stylesheet必须创建最小化.
关键是那几行:
new CellFormat
{
NumberFormatId = 14,
ApplyNumberFormat = true
})
Run Code Online (Sandbox Code Playgroud)
全Stylesheet等级:
using (var spreadSheet = SpreadsheetDocument.Create(ms, SpreadsheetDocumentType.Workbook))
{
// Workbook
var workbookPart = spreadSheet.AddWorkbookPart();
workbookPart.Workbook =
new Workbook(new Sheets(new Sheet { Name = "Sheet1", SheetId = (UInt32Value) 1U, Id = "rId1" }));
// Add minimal Stylesheet
var stylesPart = spreadSheet.WorkbookPart.AddNewPart<WorkbookStylesPart>();
stylesPart.Stylesheet = new Stylesheet
{
Fonts = new Fonts(new Font()),
Fills = new Fills(new Fill()),
Borders = new Borders(new Border()),
CellStyleFormats = new CellStyleFormats(new CellFormat()),
CellFormats =
new CellFormats(
new CellFormat(),
new CellFormat
{
NumberFormatId = 14,
ApplyNumberFormat = true
})
};
// Continue creating `WorksheetPart`...
Run Code Online (Sandbox Code Playgroud)
之后Stylesheet加入,DateTime可以格式化:
if (valueType == typeof(DateTime))
{
DateTime date = (DateTime)value;
cell.CellValue = new CellValue(date.ToOADate().ToString(CultureInfo.InvariantCulture));
// "StyleIndex" is "1", because "NumberFormatId=14"
// is in the 2nd item of `CellFormats` array.
cell.StyleIndex = 1;
}
Run Code Online (Sandbox Code Playgroud)
请注意,StyleIndex值取决于数组或对象中CellFormat项的顺序.在数组中第二项上的此示例项中.CellFormatsStylesheetNumberFormatId = 14
在 OpenXml 中存储日期有 2 种方法;通过写入数字(使用ToOADate)并将 设为DataType或Number写入ISO 8601格式的日期并将DataType设为Date。请注意,默认情况DataType下Number,如果您选择第一个选项,则不必设置DataType.
无论选择哪种方法,都需要设置样式,因为 Excel 会以相同的方式显示这两种方法。以下代码显示了使用 格式Number(无论是否显式设置DataType)并使用 ISO 8601 格式写入日期的示例。
using (SpreadsheetDocument document = SpreadsheetDocument.Create(filename, SpreadsheetDocumentType.Workbook))
{
//fluff to generate the workbook etc
WorkbookPart workbookPart = document.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
var worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet();
Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet" };
sheets.Append(sheet);
workbookPart.Workbook.Save();
var sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());
//add the style
Stylesheet styleSheet = new Stylesheet();
CellFormat cf = new CellFormat();
cf.NumberFormatId = 14;
cf.ApplyNumberFormat = true;
CellFormats cfs = new CellFormats();
cfs.Append(cf);
styleSheet.CellFormats = cfs;
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());
workbookPart.AddNewPart<WorkbookStylesPart>();
workbookPart.WorkbookStylesPart.Stylesheet = styleSheet;
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);
Row row = new Row();
DateTime date = new DateTime(2017, 6, 24);
/*** Date code here ***/
//write an OADate with type of Number
Cell cell1 = new Cell();
cell1.CellReference = "A1";
cell1.CellValue = new CellValue(date.ToOADate().ToString());
cell1.DataType = new EnumValue<CellValues>(CellValues.Number);
cell1.StyleIndex = 0;
row.Append(cell1);
//write an OADate with no type (defaults to Number)
Cell cell2 = new Cell();
cell2.CellReference = "B1";
cell2.CellValue = new CellValue(date.ToOADate().ToString());
cell1.StyleIndex = 0;
row.Append(cell2);
//write an ISO 8601 date with type of Date
Cell cell3 = new Cell();
cell3.CellReference = "C1";
cell3.CellValue = new CellValue(date.ToString("yyyy-MM-dd"));
cell3.DataType = new EnumValue<CellValues>(CellValues.Date);
cell1.StyleIndex = 0;
row.Append(cell3);
sheetData.AppendChild(row);
worksheetPart.Worksheet.Save();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
23684 次 |
| 最近记录: |