class OpenXmlDemo
    {
        /*
         * excel 对象结构
         * SpreadsheetDocument
         *   》WorkbookPart
         *       》WorksheetPart
         *           》Worksheet
         *            》SheetData
         *       》WorksheetPart
         *          》Worksheet
         *                》SheetData1
         *       》Workbook
         *           》Sheets
         *                》Sheet
         */
        public static void Create(string path)
        {
            SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(path, SpreadsheetDocumentType.Workbook);

            WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
            Workbook workbook = new Workbook();
            Sheets sheets = new Sheets();

            //创建样式

            var workbookStylesPart = workbookpart.AddNewPart<WorkbookStylesPart>();
            Stylesheet stylesheet = new Stylesheet();
            workbookStylesPart.Stylesheet = stylesheet;

            #region 设置字体
            workbookStylesPart.Stylesheet.Fonts = new Fonts()
            {
                Count = (UInt32Value)1U
            };
            Font fontCalibri = new Font(new FontSize() { Val = 11D },
                             new FontName() { Val = "Calibri" },
                             new FontFamily() { Val = 2 },
                             new FontScheme() { Val = FontSchemeValues.Minor });

            stylesheet.Fonts.Append(fontCalibri);

            Font fontCalibri2 = new Font(new FontSize() { Val = 11D },
                            new FontName() { Val = "华文新魏" },
                            new FontCharSet() { Val = 134 }
                            );
            workbookStylesPart.Stylesheet.Fonts.Append(fontCalibri2);

            Font fontCalibri3 = new Font(new FontSize() { Val = 11D },
                            new FontName() { Val = "华文楷体" },
                            new FontCharSet() { Val = 134 }
                            );
            workbookStylesPart.Stylesheet.Fonts.Append(fontCalibri3);
            #endregion
            #region -- 设置边框 --
            stylesheet.Borders = new Borders()
            {
                Count = (UInt32Value)2U
            };

            //borderID=0
            Border borderDefault = new Border(new LeftBorder(), new RightBorder(), new TopBorder() { }, new BottomBorder(), new DiagonalBorder());
            stylesheet.Borders.Append(borderDefault);

            //borderID=1
            Border borderContent = new Border(
                new LeftBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
                new RightBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
                new TopBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
                new BottomBorder(new Color() { Auto = true }) { Style = BorderStyleValues.Thin },
                new DiagonalBorder()
);
            stylesheet.Borders.Append(borderContent);
            #endregion

            #region 设置填充色
             //fillId,0总是None,1总是gray125,自定义的从fillid =2开始
             stylesheet.Fills = new Fills()
             {
                 Count = (UInt32Value)3U
             };

             //fillid=0
             Fill fillDefault = new Fill(new PatternFill() { PatternType = PatternValues.None });
             stylesheet.Fills.Append(fillDefault);

             //fillid=1
             Fill fillGray = new Fill();
             PatternFill patternFillGray = new PatternFill()
             {
                 PatternType = PatternValues.Gray125
             };
             fillGray.Append(patternFillGray);
             stylesheet.Fills.Append(fillGray);

             //fillid=2
             Fill fillYellow = new Fill();
             PatternFill patternFillYellow = new PatternFill(new ForegroundColor() { Rgb = new HexBinaryValue() { Value = "FFFFFF00" } })
             {
                 PatternType = PatternValues.Solid
             };
             fillYellow.Append(patternFillYellow);
             stylesheet.Fills.Append(fillYellow);

             stylesheet.Borders = new Borders()
             {
                 Count = (UInt32Value)2U
             };
            #endregion
            #region 定义单元格样式
            stylesheet.CellFormats = new CellFormats();
            stylesheet.CellFormats.Count = 2;
            //styleIndex =0U
            CellFormat cfDefault = new CellFormat();
            cfDefault.Alignment = new Alignment();
            cfDefault.NumberFormatId = 0;
            cfDefault.FontId = 0;
            cfDefault.BorderId = 0;
            cfDefault.FillId = 0;
            cfDefault.ApplyAlignment = true;
            cfDefault.ApplyBorder = true;
            stylesheet.CellFormats.Append(cfDefault);

            //styleIndex =1U
            CellFormat cfContent = new CellFormat();
            cfContent.Alignment = new Alignment();
            cfContent.NumberFormatId = 0;
            cfContent.FontId = 0;
            cfContent.BorderId = 1;
            cfContent.FillId = 2;
            cfContent.ApplyAlignment = true;
            cfContent.ApplyBorder = true;
            stylesheet.CellFormats.Append(cfContent);
            #endregion

            #region 创建多个 sheet 页

            //创建多个sheet
            for (int s = 0; s < 2; s++)
            {
                var tname = $"sheet{s + 1}";

                WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
                Worksheet worksheet = new Worksheet();
                SheetData sheetData = new SheetData();

                //创建 sheet 页
                Sheet sheet = new Sheet()
                {
                    //页面关联的 WorksheetPart
                    Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
                    SheetId = UInt32Value.FromUInt32((uint)s + 1),
                    Name = tname
                };
                sheets.Append(sheet);

                #region 创建sheet 行
                Row row;
                uint rowIndex = 1;
                //添加表头
                row = new Row()
                {
                    RowIndex = UInt32Value.FromUInt32(rowIndex++)
                };
                sheetData.Append(row);
                for (int i = 0; i < 3; i++)
                {
                    Cell newCell = new Cell
                    {
                        CellValue = new CellValue($"row{i + 1}"),
                        DataType = new EnumValue<CellValues>(CellValues.String)
                    };
                    row.Append(newCell);
                }
                #endregion

                worksheet.Append(sheetData);
                worksheetPart.Worksheet = worksheet;
                worksheetPart.Worksheet.Save();
            }
            #endregion

            workbook.Append(sheets);
            workbookpart.Workbook = workbook;

            workbookpart.Workbook.Save();
            spreadsheetDocument.Close();
        }
    }

最近用到了OpenXml,但是这真是个很难学习的框架,一不注意,也不给你报错,等生成成功打开的时候才会说格式错误。

目前这个demo还是错误的,后面再学习处理。

版权声明:本文为tanl原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/tanl/p/13338010.html