我需要使用Open XML使用日期类型在单元格中写入日期。 你能告诉我我该怎么做?在使用Open XML的单元格中写入日期值
0
A
回答
2
我得到了答案,并与你分享一切...
只需添加单元格,因为我们做..
Cell cell =new Cell(){ cellReference=A1 }; //Or other necessary details
cell.cellValue = new CellValue(DateTime.Now.ToOADate().ToString());
cell.StyleIndex=5;
在这里,我已经使用
cell.StyleIndex=5;
这是默认Excel中的日期样式索引。因此,没有必要添加所有的外部styylesheets
享受:)
1
所以没有必要添加所有的外部样式表
我无法得到它没有样式表的工作。
我用This Blog post得到它的工作。除了需要使用日期的CellStyleFormat和CellFormat部分外,样式表还需要Font,Border,Fill,DifferentialFormat和TableStyle部分。
private static Stylesheet CreateStylesheet()
{
Stylesheet ss = new Stylesheet();
Fonts fonts = new Fonts(new OpenXmlElement[]
{
new Font
{
FontName = new FontName { Val = "Calibri" },
FontSize = new FontSize { Val = 11 }
}
});
fonts.Count = (uint)fonts.ChildElements.Count;
Fills fills = new Fills(new OpenXmlElement[]
{
new Fill
{
PatternFill = new PatternFill { PatternType = PatternValues.None }
}
});
fills.Count = (uint)fills.ChildElements.Count;
Borders borders = new Borders(new OpenXmlElement[]
{
new Border
{
LeftBorder = new LeftBorder(),
RightBorder = new RightBorder(),
TopBorder = new TopBorder(),
BottomBorder = new BottomBorder(),
DiagonalBorder = new DiagonalBorder(),
}
});
borders.Count = (uint)borders.ChildElements.Count;
CellStyleFormats csfs = new CellStyleFormats(new OpenXmlElement[]
{
new CellFormat
{
NumberFormatId = 0,
FontId = 0,
FillId = 0,
BorderId = 0,
}
});
csfs.Count = (uint)csfs.ChildElements.Count;
CellFormats cfs = new CellFormats(new OpenXmlElement[]
{
new CellFormat
{
NumberFormatId = 0,
FontId = 0,
FillId = 0,
BorderId = 0,
FormatId = 0,
},
new CellFormat
{
NumberFormatId = 14,
FontId = 0,
FillId = 0,
BorderId = 0,
FormatId = 0,
ApplyNumberFormat = true
}
});
cfs.Count = (uint)cfs.ChildElements.Count;
ss.Append(fonts);
ss.Append(fills);
ss.Append(borders);
ss.Append(csfs);
ss.Append(cfs);
DifferentialFormats dfs = new DifferentialFormats();
dfs.Count = 0;
ss.Append(dfs);
TableStyles tss = new TableStyles();
tss.Count = 0;
tss.DefaultTableStyle = "TableStyleMedium9";
tss.DefaultPivotStyle = "PivotStyleLight16";
ss.Append(tss);
return ss;
}
一旦样式表就位,您可以设置StyleIndex。
cell.StyleIndex=14
相关问题
- 1. 什么表示Office Open XML单元格包含日期/时间值?
- 2. 从C++在Excel中写入单元格 - 没有写入值,单元格为空
- 3. 在Excel中设置日期单元格格式xml
- 4. 如何在Excel中使用日期格式读出日期单元格
- 5. 在表格单元格中插入日期
- 6. 如何使用vba将日期添加到日期的现有单元格值
- 7. 使用Apache POI从Excel获取单元格的日期值Java
- 8. 如何使用JSON写入将日期写入ISO 8601格式?
- 9. 在单元格中比较日期
- 10. 将元素值写入Python中的XML
- 11. 单元格中日期和日期的单独对齐样式
- 12. 在单个单元格中写入多个值csv
- 13. 将日期戳插入单元名下的单元格
- 14. 当另一个单元格中输入值时,Excel将日期时间输入到单元格中
- 15. 在Python中将写入日期转换为日期格式
- 16. 在MySQL查询获取单元格的值,其中日期
- 17. 错误1004 - 将值写入单元格
- 18. 使用vba写入单元格的字符串值不正确
- 19. Open XML SDK:格式化Excel单元格的一部分
- 20. 使用日期的Excel Open XML的最小样式表?
- 21. 比较单元格中的日期
- 22. 删除单元格中的日期
- 23. 如何使用PhpSpreadsheet从表格单元格中检索日期?
- 24. VBA比较单元格中日期列中的日期
- 25. 写入Google Spreadsheets中的空单元格
- 26. Excel用户表单,定位单元格和插入日期
- 27. 如何使用VBscript在Excel中的单元格中输入值
- 28. 基于CSV单元格写入XML文件名Python
- 29. Excel单元格格式 - 日期notaton
- 30. Excel Interop单元格日期格式
甚至没有一个函数DateTime.Now.ToDouble – user236215 2012-11-17 06:47:04
我编辑了答案..现在尝试 – 2012-11-19 05:24:36