繁体   English   中英

使用Open xml在单元格中写入日期值

[英]Writing Date Value in a cell using Open xml

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

我得到了答案并与大家分享...

只需添加单元格即可。

 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中日期的默认样式索引。 因此,无需添加所有外部样式表

请享用 :)

因此,无需添加所有外部样式表

没有样式表,我无法使它工作。

我使用了此博客文章来使其正常工作。 除了使用日期所需的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM