簡體   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