OpenXML读写EXCEL的例子

 using (var spreadSheet = SpreadsheetDocument.Open(fullPath, true))
        {
            WorkbookPart workbook = spreadSheet.WorkbookPart;
            //自动计算
            spreadSheet.WorkbookPart.Workbook.CalculationProperties.ForceFullCalculation = true;
            spreadSheet.WorkbookPart.Workbook.CalculationProperties.FullCalculationOnLoad = true;

            #region sheet页
            IEnumerable sheetsXXS = spreadSheet.WorkbookPart.Workbook.Descendants().Where(s => s.Name == "XX系统");
            if (sheetsXXS.Count() == 0)
            {
                //找出符合条件的sheet,没有则返回
                return;
            }
            WorksheetPart worksheetPartXXS = (WorksheetPart)spreadSheet.WorkbookPart.GetPartById(sheetsXXS.First().Id);
            var writerXXS = new WorksheetWriter(spreadSheet, worksheetPartXXS);
            #endregion

 

            //设置格式
            var style1 = SpreadsheetStyle.GetDefault(spreadSheet);
            //style1.SetBackgroundColor("97DDFF");  //设置背景颜色 
            DocumentFormat.OpenXml.Spreadsheet.Font fontCalibri = new DocumentFormat.OpenXml.Spreadsheet.Font(
                            new DocumentFormat.OpenXml.Spreadsheet.FontSize() { Val = 9D },
                             new FontName() { Val = "仿宋_GB2312" },
                             new DocumentFormat.OpenXml.Spreadsheet.FontFamily() { Val = 2 },
                             new FontScheme() { Val = FontSchemeValues.Minor });
            style1.AddFont(fontCalibri);
            style1.SetHorizontalAlignment(DocumentFormat.OpenXml.Spreadsheet.HorizontalAlignmentValues.Center);//设置水平居中对齐
            style1.SetVerticalAlignment(DocumentFormat.OpenXml.Spreadsheet.VerticalAlignmentValues.Center);//设置垂直居中对齐
            style1.SetBorder("000000", BorderStyleValues.Thin);//设置边框 
            style1.IsBold = true;//设置字体加粗

 

            worksheetPartXXS.Worksheet.Save();


 

你可能感兴趣的:(Excel操作类库)