Exemple #1
0
        private void writeSheetData(OpenXmlWriter writer, SpreadsheetStylesManager stylesManager, SpreadsheetHyperlinkManager hyperlinkManager, DrawingsManager drawingsManager)
        {
            writer.WriteStartElement(new SheetData());

            foreach (var row in _rows)
            {
                row.Value.WriteRow(writer, row.Key.ColumnIndex, row.Key.RowIndex, stylesManager, hyperlinkManager, drawingsManager);
            }

            foreach (var table in _tables)
            {
                if (!table.Value.IsInStreamingMode)
                {
                    continue;
                }

                using (var enumerator = table.Value.GetStreamingEnumerator())
                {
                    var tableRowPosition = table.Value.StreamedRowsSoFar;
                    while (enumerator.MoveNext())
                    {
                        var row = enumerator.Current;
                        row.WriteRow(writer, table.Key.ColumnIndex, table.Key.RowIndex + tableRowPosition + 1,
                                     stylesManager, hyperlinkManager, drawingsManager);
                        tableRowPosition++;
                    }
                }
            }

            writer.WriteEndElement();
        }
        private void save(SpreadsheetDocument document)
        {
            var sheets       = new Sheets();
            var workbookPart = document.AddWorkbookPart();
            var workbook     = workbookPart.Workbook = new Workbook();

            workbook.Sheets = sheets;
            var stylesManager = new SpreadsheetStylesManager(workbookPart);

            writeWorkSheets(workbookPart, sheets, stylesManager);
        }
Exemple #3
0
        public void WriteRow(OpenXmlWriter writer, int columnIndex, int rowIndex, SpreadsheetStylesManager stylesManager, SpreadsheetHyperlinkManager hyperlinkManager, DrawingsManager drawingsManager)
        {
            var span          = string.Format("{0}:{1}", columnIndex, RowCells.Count + columnIndex);
            var attributeList = new List <OpenXmlAttribute>();

            var rowIndexAtt = new OpenXmlAttribute("r", null, rowIndex.ToString());
            var spanAtt     = new OpenXmlAttribute("spans", null, span);

            attributeList.Add(rowIndexAtt);
            attributeList.Add(spanAtt);
            if (this.RowHeight != null)
            {
                attributeList.Add(new OpenXmlAttribute("customHeight", null, "1"));
                attributeList.Add(new OpenXmlAttribute("ht", null, this.RowHeight.Value.ToString(CultureInfo.InvariantCulture)));
            }
            if (!IsVisible)
            {
                var hiddenAttribute = new OpenXmlAttribute("hidden", null, 1.ToString());
                attributeList.Add(hiddenAttribute);
            }
            if (!IsExpanded)
            {
                var hiddenAttribute = new OpenXmlAttribute("collapsed", null, 1.ToString());
                attributeList.Add(hiddenAttribute);
            }
            if (!IsMaster)
            {
                var hiddenAttribute = new OpenXmlAttribute("outlineLevel", null, 1.ToString());
                attributeList.Add(hiddenAttribute);
            }

            writer.WriteStartElement(new Row(), attributeList);
            foreach (var cell in RowCells)
            {
                if (cell.ImageIndex != -1)
                {
                    drawingsManager.SetImageForCell(new ImageDetails()
                    {
                        Column           = columnIndex,
                        ImageIndex       = cell.ImageIndex,
                        ImageScaleFactor = cell.ImageScaleFactor,
                        Indent           = cell.Indent,
                        Row = rowIndex
                    });
                }

                cell.WriteCell(writer, columnIndex, rowIndex, stylesManager, hyperlinkManager);
                columnIndex++;
            }
            writer.WriteEndElement();
        }
        protected override OpenXmlAttribute?getStyleAttribute(SpreadsheetStylesManager stylesManager)
        {
            var spreadsheetStyle = new SpreadsheetStyle();

            if (Indent != 0 || Alignment != null || VerticalAlignment != null)
            {
                spreadsheetStyle = new SpreadsheetStyle()
                {
                    Alignment         = Alignment.HasValue ? SpreadsheetHelper.GetHorizontalAlignmentValue(Alignment.Value) : (HorizontalAlignmentValues?)null,
                    VerticalAlignment = VerticalAlignment.HasValue ? SpreadsheetHelper.GetVerticalAlignmentValues(VerticalAlignment.Value) : (VerticalAlignmentValues?)null,
                    BackgroundColor   = BackgroundColor,
                    Font            = Font,
                    ForegroundColor = ForegroundColor,
                    Indent          = Indent
                };
            }
            return(new OpenXmlAttribute("s", null, ((UInt32)stylesManager.GetHyperlinkStyleIndex(spreadsheetStyle)).ToString()));
        }
        private void writeWorkSheets(WorkbookPart workbookPart, Sheets sheets, SpreadsheetStylesManager stylesManager)
        {
            var sheetCounter = 1;

            foreach (var worksheets in _worksheets)
            {
                var sheetId = "Sheet" + sheetCounter;
                var sheet   = new Sheet()
                {
                    Name = new string(worksheets.Name.Take(30).ToArray()), SheetId = (UInt32Value)(UInt32)sheetCounter, Id = sheetId
                };
                sheetCounter++;
                sheets.Append(sheet);
                var worksheetPart = workbookPart.AddNewPart <WorksheetPart>(sheetId);
                using (var writer = OpenXmlWriter.Create(worksheetPart))
                {
                    worksheets.WriteWorksheet(writer, worksheetPart, stylesManager, ref _tableCount);
                }
            }
        }
Exemple #6
0
        public void WriteWorksheet(OpenXmlWriter writer, WorksheetPart part, SpreadsheetStylesManager stylesManager, ref int tableCount)
        {
            var hyperLinksManager = new SpreadsheetHyperlinkManager();

            writer.WriteStartElement(new Worksheet(), new List <OpenXmlAttribute>(), new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships")
            });

            writeSheetProperties(writer);
            writeFrozenFirstColumn(writer);
            writeColumns(writer);
            writeSheetData(writer, stylesManager, hyperLinksManager, DrawingsManager);
            writeMergedCells(writer);
            writeHyperlinks(writer, part, hyperLinksManager);
            writeDrawings(part, writer);
            writeTables(writer, part, ref tableCount);
            writeExtensionsList(writer);
            writer.WriteEndElement();
        }
 public override void WriteCell(OpenXmlWriter writer, int columnIndex, int rowIndex, SpreadsheetStylesManager stylesManager, SpreadsheetHyperlinkManager hyperlinkManager)
 {
     base.WriteCell(writer, columnIndex, rowIndex, stylesManager, hyperlinkManager);
     hyperlinkManager.AddHyperlink(new SpreadsheetLocation(rowIndex, columnIndex), _hyperLink);
 }