public void Write(CellFormat cellFormat) { if (cellFormat == null) { throw new ArgumentNullException(nameof(cellFormat)); } var cf = new OpenXml.CellFormat(); if (cellFormat.FontId.HasValue) { cf.FontId = cellFormat.FontId.Value; cf.ApplyFont = cellFormat.FontId != 0; } if (cellFormat.FillId.HasValue) { cf.FillId = cellFormat.FillId.Value; cf.ApplyFill = cellFormat.FillId != 0; } if (cellFormat.BorderId.HasValue) { cf.BorderId = cellFormat.BorderId.Value; cf.ApplyBorder = cellFormat.BorderId != 0; } if (cellFormat.NumberFormatId.HasValue) { cf.NumberFormatId = cellFormat.NumberFormatId.Value; cf.ApplyNumberFormat = cellFormat.NumberFormatId != 0; } if (cellFormat.Alignment != null) { IWriter <Alignment> alignmentWriter = new AlignmentWriter(cf); alignmentWriter.Write(cellFormat.Alignment); } stylesheet.CellFormats.Append(cf); }
public AlignmentWriter(OpenXml.CellFormat parent) { this.parent = parent ?? throw new ArgumentNullException(nameof(parent)); }
public override byte[] Render(global::Campus.Report.Base.Report report) { byte[] buffer; using (var outputMemoryStream = new MemoryStream()) { using (var document = SpreadsheetDocument.Create(outputMemoryStream, SpreadsheetDocumentType.Workbook, true)) { // Add a WorkbookPart to the document. var workbookpart = document.AddWorkbookPart(); workbookpart.Workbook = new Workbook(); // Add a WorksheetPart to the WorkbookPart. var worksheetPart = workbookpart.AddNewPart <WorksheetPart>(); worksheetPart.Worksheet = new Worksheet(new SheetData()); // Add Sheets to the Workbook. var sheets = document.WorkbookPart.Workbook.AppendChild(new Sheets()); // Append a new worksheet and associate it with the workbook. var sheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet() { Id = document.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet1" }; sheets.Append(sheet); // Add a WorkbookStylesPart to the WorkbookPart var stylesPart = document.WorkbookPart.AddNewPart <WorkbookStylesPart>(); var shData = worksheetPart.Worksheet.GetFirstChild <SheetData>(); stylesPart.Stylesheet = InitializeDefaultStyles(stylesPart.Stylesheet); uint styleId = 2; //обязательно 2 потому что уже додано два стиля по умолчанию с индексами 0 и 1; var styles = report.Select(o => o.Style).ToList(); //нельзя перенести в отдельный метод :( foreach (var s in styles) { if (s is TableStyle) { var style = s as TableStyle; var border = new DocumentFormat.OpenXml.Spreadsheet.Border(); var leftBorder = new DocumentFormat.OpenXml.Spreadsheet.LeftBorder(); var rightBorder = new DocumentFormat.OpenXml.Spreadsheet.RightBorder(); var topBorder = new DocumentFormat.OpenXml.Spreadsheet.TopBorder(); var bottomBorder = new DocumentFormat.OpenXml.Spreadsheet.BottomBorder(); leftBorder.Style = SetLineType(style.BorderLine); rightBorder.Style = SetLineType(style.BorderLine); topBorder.Style = SetLineType(style.BorderLine); bottomBorder.Style = SetLineType(style.BorderLine); leftBorder.Color = new DocumentFormat.OpenXml.Spreadsheet.Color { Rgb = ToHexBinaryValue(style.BorderColor) }; rightBorder.Color = new DocumentFormat.OpenXml.Spreadsheet.Color { Rgb = ToHexBinaryValue(style.BorderColor) }; topBorder.Color = new DocumentFormat.OpenXml.Spreadsheet.Color { Rgb = ToHexBinaryValue(style.BorderColor) }; bottomBorder.Color = new DocumentFormat.OpenXml.Spreadsheet.Color { Rgb = ToHexBinaryValue(style.BorderColor) }; border.LeftBorder = leftBorder; border.RightBorder = rightBorder; border.TopBorder = topBorder; border.BottomBorder = bottomBorder; stylesPart.Stylesheet.Borders.AppendChild <DocumentFormat.OpenXml.Spreadsheet.Border>(border); var patternFill = new DocumentFormat.OpenXml.Spreadsheet.PatternFill { BackgroundColor = new DocumentFormat.OpenXml.Spreadsheet.BackgroundColor { Rgb = ToHexBinaryValue(style.Background) }, ForegroundColor = new DocumentFormat.OpenXml.Spreadsheet.ForegroundColor { Rgb = ToHexBinaryValue(style.Foreground) }, PatternType = SetPatternType(style.PatternType) }; var fill = new DocumentFormat.OpenXml.Spreadsheet.Fill { PatternFill = patternFill }; stylesPart.Stylesheet.Fills.AppendChild <DocumentFormat.OpenXml.Spreadsheet.Fill>(fill); DocumentFormat.OpenXml.Spreadsheet.Font font = new DocumentFormat.OpenXml.Spreadsheet.Font() { FontName = new DocumentFormat.OpenXml.Spreadsheet.FontName { Val = new StringValue { Value = s.FontName } }, FontSize = new DocumentFormat.OpenXml.Spreadsheet.FontSize { Val = s.FontSize }, Color = new DocumentFormat.OpenXml.Spreadsheet.Color { Rgb = ToHexBinaryValue(s.FontColor) } }; stylesPart.Stylesheet.Fonts.AppendChild <DocumentFormat.OpenXml.Spreadsheet.Font>(font); } else { stylesPart.Stylesheet.Fills.AppendChild <DocumentFormat.OpenXml.Spreadsheet.Fill>(new DocumentFormat.OpenXml.Spreadsheet.Fill()); stylesPart.Stylesheet.Borders.AppendChild <DocumentFormat.OpenXml.Spreadsheet.Border>(new DocumentFormat.OpenXml.Spreadsheet.Border()); DocumentFormat.OpenXml.Spreadsheet.Font font; if (s != null) { font = new DocumentFormat.OpenXml.Spreadsheet.Font { FontName = new DocumentFormat.OpenXml.Spreadsheet.FontName { Val = new StringValue { Value = s.FontName } }, FontSize = new DocumentFormat.OpenXml.Spreadsheet.FontSize { Val = s.FontSize }, Color = new DocumentFormat.OpenXml.Spreadsheet.Color { Rgb = ToHexBinaryValue(s.FontColor) } }; foreach (var item in s.TextStyle) { switch (item) { case TextStyleType.Bold: font.Bold = new DocumentFormat.OpenXml.Spreadsheet.Bold { Val = true }; break; case TextStyleType.Italic: font.Italic = new DocumentFormat.OpenXml.Spreadsheet.Italic { Val = true }; break; case TextStyleType.Underline: font.Underline = new DocumentFormat.OpenXml.Spreadsheet.Underline { Val = DocumentFormat.OpenXml.Spreadsheet.UnderlineValues.Single }; break; case TextStyleType.Normal: break; } } } else { font = new DocumentFormat.OpenXml.Spreadsheet.Font(); } stylesPart.Stylesheet.Fonts.AppendChild <DocumentFormat.OpenXml.Spreadsheet.Font>(font); } if (s != null) { var cellFormat = new DocumentFormat.OpenXml.Spreadsheet.CellFormat { BorderId = UInt32Value.ToUInt32(styleId), FillId = UInt32Value.ToUInt32(styleId), FontId = UInt32Value.ToUInt32(styleId) }; if (s.Aligment != null) { var align = new Alignment() { TextRotation = new UInt32Value(s.Aligment.Rotation), Horizontal = AlignmentMapper.MapHorizontalAligment(s.Aligment.HorizontalAligment), Vertical = AlignmentMapper.MapVerticalAligment(s.Aligment.VerticalAligment) }; cellFormat.Append(align); } stylesPart.Stylesheet.CellFormats.AppendChild <DocumentFormat.OpenXml.Spreadsheet.CellFormat>(cellFormat); } styleId = styleId + 1; } foreach (var x in report) { if (x is ComplexHeaderCell) { var element = x as ComplexHeaderCell; if (Render(element, styles, shData, worksheetPart, workbookpart)) { continue; } } if (x is TextElement) { var element = x as TextElement; if (Render(element, styles, shData, workbookpart)) { continue; } } if (x is TableElement) { var element = x as TableElement; if (Render(element, styles, shData, workbookpart)) { continue; } } if (x is ImageElement) { var element = x as ImageElement; if (Render(element, styles, shData, workbookpart)) { continue; } } } //var dateLine = shData.AppendChild(new Spreadsheet.Row()); //dateLine.AppendChild(new Spreadsheet.Cell() //{ // CellValue = new Spreadsheet.CellValue(_report.TimeStamp.Date.ToString()), // DataType = Spreadsheet.CellValues.String, // StyleIndex = 0 //}); workbookpart.Workbook.Save(); } buffer = new byte[outputMemoryStream.Position]; outputMemoryStream.Position = 0; outputMemoryStream.Read(buffer, 0, buffer.Length); } return(buffer); }
public void SetStyle(WorkbookStylesPart stylesPart, UInt32Value index) { DocumentFormat.OpenXml.Spreadsheet.CellFormat cellFormat; NumberingFormat numberingFormat; Alignment aligment; cellFormat = new DocumentFormat.OpenXml.Spreadsheet.CellFormat(); #region FormatsSwitch switch (Format) { case CellFormat.Header: break; case CellFormat.String: break; case CellFormat.StringWrap: break; case CellFormat.NumberInt: cellFormat.NumberFormatId = 3; // #,##0 cellFormat.ApplyNumberFormat = BooleanValue.FromBoolean(true); break; case CellFormat.NumberFract: cellFormat.NumberFormatId = 4; // #,##0.00 cellFormat.ApplyNumberFormat = BooleanValue.FromBoolean(true); break; case CellFormat.NumberFractLong: numberingFormat = new NumberingFormat(); numberingFormat.NumberFormatId = 164; numberingFormat.FormatCode = StringValue.FromString("#0.000"); stylesPart.Stylesheet.NumberingFormats.AppendChild(numberingFormat); cellFormat.NumberFormatId = numberingFormat.NumberFormatId; // 22; // m/d/yy h:mm cellFormat.ApplyNumberFormat = BooleanValue.FromBoolean(true); //cellFormat.NumberFormatId = 4; // #,##0.00 4 //cellFormat.ApplyNumberFormat = DocumentFormat.OpenXml.BooleanValue.FromBoolean(true); break; case CellFormat.PercentInt: cellFormat.NumberFormatId = 9; // 0% cellFormat.ApplyNumberFormat = BooleanValue.FromBoolean(true); break; case CellFormat.PercentFract: cellFormat.NumberFormatId = 10; // 0.00% cellFormat.ApplyNumberFormat = BooleanValue.FromBoolean(true); break; case CellFormat.Date: numberingFormat = new NumberingFormat(); numberingFormat.NumberFormatId = 165; numberingFormat.FormatCode = StringValue.FromString("dd.mm.yyyy"); stylesPart.Stylesheet.NumberingFormats.AppendChild(numberingFormat); cellFormat.NumberFormatId = numberingFormat.NumberFormatId; // 22; // m/d/yy h:mm cellFormat.ApplyNumberFormat = BooleanValue.FromBoolean(true); break; case CellFormat.DateTime: numberingFormat = new NumberingFormat(); numberingFormat.NumberFormatId = 166; numberingFormat.FormatCode = StringValue.FromString("dd.mm.yyyy hh:mm"); stylesPart.Stylesheet.NumberingFormats.AppendChild(numberingFormat); cellFormat.NumberFormatId = numberingFormat.NumberFormatId; // 22; // m/d/yy h:mm cellFormat.ApplyNumberFormat = BooleanValue.FromBoolean(true); break; case CellFormat.Bool: break; default: break; } #endregion //if (!IsLock) //{ // cellFormat.Protection = new Protection() { Locked = false }; // cellFormat.ApplyProtection = true; // // = new DocumentFormat.OpenXml.Spreadsheet.CellFormat() { ApplyProtection = true, Protection = new Protection() { Locked = false } }; //} if (IsBorder) { cellFormat.BorderId = 1; cellFormat.ApplyBorder = true; } if (Font != null) { cellFormat.FontId = Font.FontIndex; } else { cellFormat.FontId = 0; } cellFormat.ApplyFont = true; if (Fill != null) { cellFormat.FillId = Fill.FillIndex; } else { cellFormat.FillId = 0; } cellFormat.ApplyFill = true; aligment = new Alignment(); aligment.Vertical = Vertical; aligment.Horizontal = Horizontal; aligment.WrapText = IsWordWrap; cellFormat.AppendChild(aligment); stylesPart.Stylesheet.CellFormats.AppendChild(cellFormat); StyleIndex = index; }
private static void GerarExcelOpXml(System.Data.DataTable dt, string pathExcelFile, string sheetName, string ColunaDefault, int rowDefault, int larguraColuna) { var styleIndexDefault = 0; var styleIndexDefaultLine = 2; DocumentFormat.OpenXml.UInt32Value styleIndex; using (OpenXmlPackaging.SpreadsheetDocument myDoc = OpenXmlPackaging.SpreadsheetDocument.Open(pathExcelFile, true)) { OpenXmlPackaging.WorksheetPart worksheetPart = GetWorksheetPartByName(myDoc, sheetName); OpenXmlSpread.Cell cellDefault = GetCell(worksheetPart.Worksheet, ColunaDefault, Convert.ToUInt32(rowDefault)); styleIndex = cellDefault.StyleIndex; styleIndexDefault = Convert.ToInt32(styleIndex.Value.ToString()); OpenXmlSpread.Cell cellDefaultline = GetCell(worksheetPart.Worksheet, ColunaDefault, Convert.ToUInt32(rowDefault + 1)); styleIndex = cellDefaultline.StyleIndex; styleIndexDefaultLine = Convert.ToInt32(styleIndex.Value.ToString()); } using (OpenXmlPackaging.SpreadsheetDocument myDoc = OpenXmlPackaging.SpreadsheetDocument.Open(pathExcelFile, true)) { OpenXmlPackaging.WorksheetPart worksheetPart = GetWorksheetPartByName(myDoc, sheetName); OpenXmlSpread.Stylesheet stylesheet = myDoc.WorkbookPart.WorkbookStylesPart.Stylesheet; OpenXmlSpread.CellFormat deafultFormat = new OpenXmlSpread.CellFormat() { Alignment = new OpenXmlSpread.Alignment() { Horizontal = OpenXmlSpread.HorizontalAlignmentValues.Left, Vertical = OpenXmlSpread.VerticalAlignmentValues.Center }, ApplyAlignment = true }; stylesheet.CellFormats.AppendChild(deafultFormat); DocumentFormat.OpenXml.OpenXmlWriter writer = DocumentFormat.OpenXml.OpenXmlWriter.Create(worksheetPart); var indexColumn = ColumnIndex(ColunaDefault); var startColunaIndex = indexColumn + 1; writer.WriteStartElement(new OpenXmlSpread.Worksheet()); writer.WriteStartElement(new OpenXmlSpread.Columns()); AjustaLarguraColunas(writer, startColunaIndex, larguraColuna, dt.Columns.Count); writer.WriteStartElement(new OpenXmlSpread.SheetData()); writer.WriteStartElement(new OpenXmlSpread.Row { RowIndex = (UInt32)rowDefault }); CriarColunas(writer, dt, indexColumn, rowDefault, styleIndexDefault); CriarLinha(writer, dt, indexColumn, rowDefault, styleIndexDefaultLine); writer.Dispose(); } }
private static MSOpenXML.Stylesheet CreateStylesheet(int pQuantidadeDecimais = 2) { MSOpenXML.Stylesheet ss = new MSOpenXML.Stylesheet(); #region Fontes MSOpenXML.Fonts fts = new MSOpenXML.Fonts(); MSOpenXML.Font ft = new MSOpenXML.Font(); MSOpenXML.FontName ftn = new MSOpenXML.FontName(); ftn.Val = "Calibri"; MSOpenXML.FontSize ftsz = new MSOpenXML.FontSize(); ftsz.Val = 11; ft.FontName = ftn; ft.FontSize = ftsz; fts.Append(ft); ft = new MSOpenXML.Font(); ft.Bold = new MSOpenXML.Bold(); ftn = new MSOpenXML.FontName(); ftn.Val = "Calibri"; ftsz = new MSOpenXML.FontSize(); ftsz.Val = 11; ft.FontName = ftn; ft.FontSize = ftsz; fts.Append(ft); fts.Count = (uint)fts.ChildElements.Count; #endregion #region Preenchimento MSOpenXML.Fills fills = new MSOpenXML.Fills(); MSOpenXML.Fill fill; MSOpenXML.PatternFill patternFill; fill = new MSOpenXML.Fill(); patternFill = new MSOpenXML.PatternFill(); patternFill.PatternType = MSOpenXML.PatternValues.None; fill.PatternFill = patternFill; fills.Append(fill); /*fill = new dos.Fill(); * patternFill = new dos.PatternFill(); * patternFill.PatternType = dos.PatternValues.Gray125; * fill.PatternFill = patternFill; * fills.Append(fill); * * fill = new dos.Fill(); * patternFill = new dos.PatternFill(); * patternFill.PatternType = dos.PatternValues.Solid; * patternFill.ForegroundColor = new dos.ForegroundColor(); * patternFill.ForegroundColor.Rgb = HexBinaryValue.FromString("00ff9728"); * patternFill.BackgroundColor = new dos.BackgroundColor(); * patternFill.BackgroundColor.Rgb = patternFill.ForegroundColor.Rgb; * fill.PatternFill = patternFill; * fills.Append(fill); */ fills.Count = (uint)fills.ChildElements.Count; #endregion #region Bordas MSOpenXML.Borders borders = new MSOpenXML.Borders(); MSOpenXML.Border border = new MSOpenXML.Border(); border.LeftBorder = new MSOpenXML.LeftBorder(); border.RightBorder = new MSOpenXML.RightBorder(); border.TopBorder = new MSOpenXML.TopBorder(); border.BottomBorder = new MSOpenXML.BottomBorder(); border.DiagonalBorder = new MSOpenXML.DiagonalBorder(); borders.Append(border); border = new MSOpenXML.Border(); border.LeftBorder = new MSOpenXML.LeftBorder(); border.RightBorder = new MSOpenXML.RightBorder(); border.TopBorder = new MSOpenXML.TopBorder(); border.TopBorder.Style = MSOpenXML.BorderStyleValues.Thin; border.BottomBorder = new MSOpenXML.BottomBorder(); border.DiagonalBorder = new MSOpenXML.DiagonalBorder(); borders.Append(border); borders.Count = (uint)borders.ChildElements.Count; #endregion MSOpenXML.CellStyleFormats csfs = new MSOpenXML.CellStyleFormats(); MSOpenXML.CellFormat cf = new MSOpenXML.CellFormat(); cf.NumberFormatId = 0; cf.FontId = 0; cf.BorderId = 0; cf.Alignment = new MSOpenXML.Alignment() { WrapText = false }; csfs.Append(cf); csfs.Count = (uint)csfs.ChildElements.Count; uint iExcelIndex = 164; MSOpenXML.NumberingFormats nfs = new MSOpenXML.NumberingFormats(); MSOpenXML.CellFormats cfs = new MSOpenXML.CellFormats(); cf = new MSOpenXML.CellFormat(); cf.NumberFormatId = 0; cf.FontId = 0; cf.BorderId = 0; cf.FormatId = 0; cf.Alignment = new MSOpenXML.Alignment() { WrapText = false }; cfs.Append(cf); MSOpenXML.NumberingFormat nfDateTime = new MSOpenXML.NumberingFormat(); nfDateTime.NumberFormatId = iExcelIndex++; nfDateTime.FormatCode = "dd/mm/yyyy"; nfs.Append(nfDateTime); MSOpenXML.NumberingFormat nf4decimal = new MSOpenXML.NumberingFormat(); nf4decimal.NumberFormatId = iExcelIndex++; nf4decimal.FormatCode = "#,##0"; nfs.Append(nf4decimal); // #,##0.00 is also Excel style index 4 MSOpenXML.NumberingFormat nf2decimal = new MSOpenXML.NumberingFormat(); nf2decimal.NumberFormatId = iExcelIndex++; nf2decimal.FormatCode = FormatoDecimal(pQuantidadeDecimais); //"#,##0.00" nfs.Append(nf2decimal); // @ is also Excel style index 49 MSOpenXML.NumberingFormat nfForcedText = new MSOpenXML.NumberingFormat(); nfForcedText.NumberFormatId = iExcelIndex++; nfForcedText.FormatCode = "@"; nfs.Append(nfForcedText); // index 1 cf = new MSOpenXML.CellFormat(); cf.NumberFormatId = nfDateTime.NumberFormatId; cf.FontId = 0; cf.BorderId = 0; cf.FormatId = 0; cf.ApplyNumberFormat = true; cf.Alignment = new MSOpenXML.Alignment() { WrapText = false }; cfs.Append(cf); // index 2 cf = new MSOpenXML.CellFormat(); cf.NumberFormatId = nf4decimal.NumberFormatId; cf.FontId = 0; cf.BorderId = 0; cf.FormatId = 0; cf.ApplyNumberFormat = true; cf.Alignment = new MSOpenXML.Alignment() { WrapText = false }; cfs.Append(cf); // index 3 cf = new MSOpenXML.CellFormat(); cf.NumberFormatId = nf2decimal.NumberFormatId; cf.FontId = 0; cf.BorderId = 0; cf.FormatId = 0; cf.ApplyNumberFormat = true; cf.Alignment = new MSOpenXML.Alignment() { WrapText = false }; cfs.Append(cf); // index 4 cf = new MSOpenXML.CellFormat(); cf.NumberFormatId = nfForcedText.NumberFormatId; cf.FontId = 0; cf.BorderId = 0; cf.FormatId = 0; cf.ApplyNumberFormat = true; cf.Alignment = new MSOpenXML.Alignment() { WrapText = false }; cfs.Append(cf); // index 5 // Header text cf = new MSOpenXML.CellFormat(); cf.NumberFormatId = nfForcedText.NumberFormatId; cf.FontId = 1; cf.BorderId = 0; cf.FormatId = 0; cf.Alignment = new MSOpenXML.Alignment() { WrapText = false }; cf.Alignment.Horizontal = MSOpenXML.HorizontalAlignmentValues.Center; cf.ApplyNumberFormat = true; cfs.Append(cf); // index 6 // group text cf = new MSOpenXML.CellFormat(); cf.NumberFormatId = nf2decimal.NumberFormatId; cf.FontId = 1; cf.BorderId = 1; cf.FormatId = 0; cf.ApplyNumberFormat = true; cf.Alignment = new MSOpenXML.Alignment() { WrapText = false }; cfs.Append(cf); // index 7 // Total text, ColumnHeader Text cf = new MSOpenXML.CellFormat(); cf.NumberFormatId = nf2decimal.NumberFormatId; cf.FontId = 1; cf.BorderId = 0; cf.FormatId = 0; cf.ApplyNumberFormat = true; cf.Alignment = new MSOpenXML.Alignment() { WrapText = false }; cfs.Append(cf); nfs.Count = (uint)nfs.ChildElements.Count; cfs.Count = (uint)cfs.ChildElements.Count; ss.Append(nfs); ss.Append(fts); ss.Append(fills); ss.Append(borders); ss.Append(csfs); ss.Append(cfs); MSOpenXML.CellStyles css = new MSOpenXML.CellStyles(); MSOpenXML.CellStyle cs = new MSOpenXML.CellStyle(); cs.Name = "Normal"; cs.FormatId = 0; cs.BuiltinId = 0; css.Append(cs); css.Count = (uint)css.ChildElements.Count; ss.Append(css); MSOpenXML.DifferentialFormats dfs = new MSOpenXML.DifferentialFormats(); dfs.Count = 0; ss.Append(dfs); MSOpenXML.TableStyles tss = new MSOpenXML.TableStyles(); tss.Count = 0; //tss.DefaultTableStyle = StringValue.FromString("TableStyleMedium9"); //tss.DefaultPivotStyle = StringValue.FromString("PivotStyleLight16"); ss.Append(tss); return(ss); }