0

我需要使用 Open XML 使用日期类型在单元格中写入日期。你能告诉我我该怎么做吗?

4

2 回答 2

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 中日期的默认样式索引。所以不需要添加所有外部样式表

享受 :)

于 2012-07-26T12:26:14.270 回答
1

所以不需要添加所有的外部样式表

如果没有样式表,我无法让它工作。

我用这个博客文章让它工作。除了使用日期所需的 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
于 2016-06-22T19:45:39.230 回答