Exemplo n.º 1
0
 public void SetFreezePanes(int row, int col)
 {
     if (_headWriter.CurrentSection > StreamsheetParser.StreamSheetSection.SheetViews)
     {
         throw new FatalException();
     }
     if (_stagedSheetView == null)
     {
         _stagedSheetView = new CT_SheetView();
     }
     _stagedSheetView.Pane                  = new CT_Pane();
     _stagedSheetView.Pane                  = new CT_Pane();
     _stagedSheetView.Pane.State_Attr       = ST_PaneState.frozen;
     _stagedSheetView.Pane.XSplit_Attr      = col;
     _stagedSheetView.Pane.YSplit_Attr      = row;
     _stagedSheetView.Pane.TopLeftCell_Attr = CellPair.Name(row, col);
 }
Exemplo n.º 2
0
        private void WriteSheet(string sheetPath, Sheet sheet)
        {
            var sharedFormulas = new Dictionary <SharedFormula, int>();
            var mergedCells    = new Mapper <Range, CT_MergeCell>(ConvertRangeToMergedCell);

            bool[] autoFit     = new bool[256];
            int[]  width       = new int[256];
            bool   calcAutoFit = (Options & XlsxFileWriterOptions.AutoFit) != 0;


            var sheetFormat = new Lazy <CT_SheetFormatPr>();

            if (sheet.DefaultRowHeight.HasValue)
            {
                sheetFormat.Value.defaultRowHeight = sheet.DefaultRowHeight.Value;
                sheetFormat.Value.customHeight     = true;
            }

            if (sheet.DefaultColumnWidth.HasValue)
            {
                sheetFormat.Value.defaultColWidth          = sheet.DefaultColumnWidth.Value;
                sheetFormat.Value.defaultColWidthSpecified = true;
            }

            var sheetView = new CT_SheetView();

            if (sheet.ActiveCell != null)
            {
                sheetView.selection = new[] { new CT_Selection {
                                                  activeCell = sheet.ActiveCell.ToString(), sqref = new[] { sheet.ActiveCell.ToString() }
                                              } }
            }
            ;

            sheetView.showGridLines = sheet.ShowGridLines;


            var rows = new List <CT_Row>();

            foreach (var row in sheet.Data)
            {
                var cells = new List <CT_Cell>();

                foreach (var cd in row.Value)
                {
                    bool skipData = false;
                    var  c        = new Cell {
                        Col = cd.Key, Row = row.Key
                    };
                    if (cd.Value.IsMerged)
                    {
                        if (cd.Value.MergedRange.Cell1 == c)
                        {
                            mergedCells.GetIndex(cd.Value.MergedRange);
                        }
                        else
                        {
                            skipData = true;
                        }
                    }

                    ST_CellType ct;
                    var         data = cd.Value;

                    var cell = new CT_Cell
                    {
                        r = c.ToString()
                    };

                    if (!skipData)
                    {
                        if (data.Formula != null)
                        {
                            if (data.Formula.StartsWith("{"))
                            {
                                cell.f = new CT_CellFormula
                                {
                                    t     = ST_CellFormulaType.array,
                                    Value = data.Formula.Substring(1, data.Formula.Length - 2)
                                };
                            }
                            else
                            {
                                cell.f = new CT_CellFormula {
                                    Value = data.Formula
                                }
                            };
                        }
                        else if (data.SharedFormula != null)
                        {
                            int si;
                            if (!sharedFormulas.TryGetValue(data.SharedFormula, out si))
                            {
                                si = sharedFormulas.Count; //zero based
                                sharedFormulas.Add(data.SharedFormula, si);
                                cell.f = new CT_CellFormula
                                {
                                    t           = ST_CellFormulaType.shared,
                                    Value       = data.SharedFormula.Formula,
                                    @ref        = data.SharedFormula.Range.ToString(),
                                    si          = (uint)si,
                                    siSpecified = true,
                                };
                            }
                            else
                            {
                                cell.f = new CT_CellFormula
                                {
                                    si          = (uint)si,
                                    siSpecified = true,
                                    t           = ST_CellFormulaType.shared
                                }
                            };
                        }
                        else
                        {
                            String sv, format;
                            var    v = WriteCellValue(data, out ct, out sv, out format);
                            if (format != null && (data.style == null || data.style.format == null))
                            {
                                data.Style.Format = format;
                            }
                            cell.v = v;
                            cell.t = ct;
                            if (calcAutoFit && sv != null && !cd.Value.IsMerged && cd.Key < 256)
                            {
                                autoFit[cd.Key] = true;
                                if (sv.Length > width[cd.Key])
                                {
                                    width[cd.Key] = sv.Length;
                                }
                            }
                        }
                    }

                    if (data.style != null)
                    {
                        cell.s = RegisterStyle(data.style);
                    }

                    cells.Add(cell);
                }

                var r = new CT_Row()
                {
                    c          = cells.ToArray(),
                    r          = (uint)row.Key + 1,
                    rSpecified = true
                };

                if (row.Value.Height.HasValue)
                {
                    r.ht           = row.Value.Height.Value;
                    r.htSpecified  = true;
                    r.customHeight = true;
                }

                r.hidden    = row.Value.Hidden;
                r.collapsed = row.Value.Collapsed;
                r.ph        = row.Value.Phonetic;

                if (row.Value.style != null)
                {
                    r.s            = RegisterStyle(row.Value.style);
                    r.customFormat = r.s > 0;
                }

                rows.Add(r);
            }

            var cols = new List <CT_Col>();

            if (calcAutoFit)
            {
                for (int i = 0; i < width.Length; i++)
                {
                    if (width[i] > 0)
                    {
                        if (!sheet.Columns[i].Width.HasValue)
                        {
                            sheet.Columns[i].Width   = width[i];
                            sheet.Columns[i].BestFit = true;
                        }
                    }
                }
            }

            if (sheet.Columns.data.Count > 0)
            {
                foreach (var node in sheet.Columns.data)
                {
                    var col = new CT_Col
                    {
                        min            = (uint)(node.FirstColumn + 1),
                        max            = (uint)(node.LastColumn + 1),
                        bestFit        = node.Data.BestFit,
                        hidden         = node.Data.Hidden,
                        outlineLevel   = node.Data.OutlineLevel,
                        phonetic       = node.Data.Phonetic,
                        width          = node.Data.Width ?? 10,
                        widthSpecified = true,
                        customWidth    = node.Data.Width.HasValue
                    };

                    if (node.Data.style != null)
                    {
                        col.style = RegisterStyle(node.Data.style);
                    }

                    cols.Add(col);
                }
            }

            var ws = new CT_Worksheet
            {
                sheetData     = rows.ToArray(),
                cols          = cols.Count > 0 ? cols.ToArray() : null,
                sheetFormatPr = sheetFormat.Data,
                sheetViews    = new CT_SheetViews {
                    sheetView = new[] { sheetView }
                },
                pageSetup = new CT_PageSetup()
                {
                    scale = (uint)sheet.Page.Scale
                }
            };

            switch (sheet.Page.Orientation)
            {
            case PageOrientation.Landscape:
                ws.pageSetup.orientation = ST_Orientation.landscape;
                break;

            case PageOrientation.Portrait:
                ws.pageSetup.orientation = ST_Orientation.portrait;
                break;
            }

            if (sheet.Page.Margins != null)
            {
                ws.pageMargins = new CT_PageMargins
                {
                    bottom = sheet.Page.Margins.Bottom,
                    footer = sheet.Page.Margins.Footer,
                    header = sheet.Page.Margins.Header,
                    left   = sheet.Page.Margins.Left,
                    right  = sheet.Page.Margins.Right,
                    top    = sheet.Page.Margins.Top
                };
            }

            if (sheet.ColumnBreaks != null && sheet.ColumnBreaks.Count > 0)
            {
                ws.colBreaks = new CT_PageBreak
                {
                    count            = (uint)sheet.ColumnBreaks.Count,
                    manualBreakCount = (uint)sheet.ColumnBreaks.Count,
                    brk = sheet.ColumnBreaks.Select(id => new CT_Break
                    {
                        id  = (uint)id + 1,
                        man = true,
                        //max = (uint)sheet.Data.LastRow + 1
                    }).ToArray()
                };
            }

            if (sheet.RowBreaks != null && sheet.RowBreaks.Count > 0)
            {
                ws.rowBreaks = new CT_PageBreak
                {
                    count            = (uint)sheet.RowBreaks.Count,
                    manualBreakCount = (uint)sheet.RowBreaks.Count,
                    brk = sheet.RowBreaks.Select(id => new CT_Break
                    {
                        id  = (uint)id + 1,
                        man = true,
                        //max = 1048575U
                    }).ToArray()
                };
            }

            if (!mergedCells.Empty)
            {
                ws.mergeCells = new CT_MergeCells {
                    mergeCell = mergedCells.ToArray()
                }
            }
            ;

            //conditional formatting
            CT_ConditionalFormatting[] cf;
            WriteConditionalFormatting(sheet, out cf);
            ws.conditionalFormatting = cf;

            WriteFile(sheetPath, ws, SpreadsheetNs(false));
        }
Exemplo n.º 3
0
 public void WriteSheetView(CT_SheetView view)
 {
     this.WriteTagInCollection <CT_SheetViews>(view, StreamSheetSection.SheetViews, CT_SheetViews.SheetViewElementName, CT_Worksheet.SheetViewsElementName);
 }
Exemplo n.º 4
0
        private void AdvanceStreamsheetTo(StreamsheetParser.StreamSheetSection newSection, bool head)
        {
            StreamsheetParser    streamsheetParser   = head ? _headWriter : _tailWriter;
            IOoxmlCtWrapperModel ooxmlCtWrapperModel = head ? _stagedHeadTag : _stagedTailTag;

            CheckAdvancingStatus(newSection, streamsheetParser);
            if (ooxmlCtWrapperModel != null)
            {
                ooxmlCtWrapperModel.Cleanup();
                streamsheetParser.WriteTag(ooxmlCtWrapperModel.OoxmlTag);
                if (head)
                {
                    _stagedHeadTag = null;
                }
                else
                {
                    _stagedTailTag = null;
                }
            }
            if (_stagedPageSetupModel != null && IsGoingPast(streamsheetParser.CurrentSection, newSection, StreamsheetParser.StreamSheetSection.SheetPr))
            {
                _stagedPageSetupModel.Cleanup();
                streamsheetParser.WriteSheetProperties(_stagedPageSetupModel.BackingSheet.SheetPr);
            }
            if (_stagedSheetView != null && IsGoingPast(streamsheetParser.CurrentSection, newSection, StreamsheetParser.StreamSheetSection.SheetViews))
            {
                streamsheetParser.WriteSheetView(_stagedSheetView);
                _stagedSheetView = null;
            }
            if (_stagedPageSetupModel != null && IsGoingPast(streamsheetParser.CurrentSection, newSection, StreamsheetParser.StreamSheetSection.PageMargins))
            {
                _stagedPageSetupModel.Cleanup();
                streamsheetParser.WritePageMargins(_stagedPageSetupModel.BackingSheet.PageMargins);
            }
            if (_stagedPageSetupModel != null && IsGoingPast(streamsheetParser.CurrentSection, newSection, StreamsheetParser.StreamSheetSection.PageSetup))
            {
                _stagedPageSetupModel.Cleanup();
                streamsheetParser.WritePageSetup(_stagedPageSetupModel.BackingSheet.PageSetup);
            }
            if (_stagedPageSetupModel != null && IsGoingPast(streamsheetParser.CurrentSection, newSection, StreamsheetParser.StreamSheetSection.HeaderFooter))
            {
                _stagedPageSetupModel.Cleanup();
                if (_stagedPageSetupModel.BackingSheet.HeaderFooter == null)
                {
                    _stagedPageSetupModel.BackingSheet.HeaderFooter = new CT_HeaderFooter();
                }
                _stagedPageSetupModel.BackingSheet.HeaderFooter.AlignWithMargins_Attr = false;
                streamsheetParser.WriteHeaderFooter(_stagedPageSetupModel.BackingSheet.HeaderFooter);
            }
            if (_stagedDrawingTag != null && IsGoingPast(streamsheetParser.CurrentSection, newSection, StreamsheetParser.StreamSheetSection.Drawing))
            {
                streamsheetParser.WriteTag(_stagedDrawingTag);
                _stagedDrawingTag = null;
            }
            if (_stagedSheetBackgroundTag != null && IsGoingPast(streamsheetParser.CurrentSection, newSection, StreamsheetParser.StreamSheetSection.Picture))
            {
                streamsheetParser.WriteTag(_stagedSheetBackgroundTag);
                _stagedSheetBackgroundTag = null;
            }
            CheckAdvancingStatus(newSection, streamsheetParser);
        }