Open Xml 写入日期类型

 效果图:

   Open Xml 写入日期类型_第1张图片

 

关键代码:

类型判断

                        if (rIndex == 1)
                        {
                            cell.CellValue = new CellValue(item.ToString());
                        }
                        else if (rIndex == 2)
                        {
                            cell.CellValue = new CellValue(((DateTime)item).ToOADate().ToString());
                            cell.StyleIndex = 1;
                        }
                        else
                        {
                            int index = InsertSharedStringItem(item.ToString(), m_SharedStringTablePart);
                            cell.CellValue = new CellValue(index.ToString());
                            cell.DataType = new EnumValue(CellValues.SharedString);
                        }


Style:

        static Stylesheet CreateStylesheet()
        {
            Stylesheet stylesheet1 = new Stylesheet();

            Fonts fonts1 = new Fonts() { Count = (UInt32Value)1U, KnownFonts = true };
            Font font1 = new Font();
            fonts1.Append(font1);
            Fills fills1 = new Fills() { Count = (UInt32Value)1U };
            Fill fill1 = new Fill();
            fills1.Append(fill1);
            Borders borders1 = new Borders() { Count = (UInt32Value)1U };
            Border border1 = new Border();
            borders1.Append(border1);

            CellFormats cellFormats1 = new CellFormats() { Count = (UInt32Value)2U };
            CellFormat cellFormat1 = new CellFormat() ;
            CellFormat cellFormat2 = new CellFormat()
            { 
                NumberFormatId = (UInt32Value)22U, 
                ApplyNumberFormat = true 
            };
            cellFormats1.Append(cellFormat1);
            cellFormats1.Append(cellFormat2);

            stylesheet1.Append(fonts1);
            stylesheet1.Append(fills1);
            stylesheet1.Append(borders1);
            stylesheet1.Append(cellFormats1);

            return stylesheet1;
        } 

其中,fonts,fills和borders需要写入,这个很关键,并且cellFormat1也需要写入。

 

你可能感兴趣的:(Open Xml 写入日期类型)