/// <summary>
        /// Get the page settings of sheet.
        /// </summary>
        /// <param name="SheetName">The name of the sheet.</param>
        /// <returns>An SLPageSettings object with the page settings of the specified sheet.</returns>
        public SLPageSettings GetPageSettings(string SheetName)
        {
            if (SheetName.Equals(gsSelectedWorksheetName, StringComparison.OrdinalIgnoreCase))
            {
                return slws.PageSettings.Clone();
            }

            SLPageSettings ps = new SLPageSettings(SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);

            bool bSheetFound = false;
            SLSheet sheet = new SLSheet(string.Empty, 0, string.Empty, SLSheetType.Unknown);
            foreach (SLSheet s in slwb.Sheets)
            {
                if (s.Name.Equals(SheetName, StringComparison.OrdinalIgnoreCase))
                {
                    bSheetFound = true;
                    sheet = s.Clone();
                    break;
                }
            }

            if (bSheetFound)
            {
                if (sheet.SheetType == SLSheetType.Worksheet)
                {
                    WorksheetPart wsp = (WorksheetPart)wbp.GetPartById(sheet.Id);
                    using (OpenXmlReader oxr = OpenXmlReader.Create(wsp))
                    {
                        bool bFound = false;
                        SLSheetView slsv;
                        while (oxr.Read())
                        {
                            if (oxr.ElementType == typeof(SheetProperties))
                            {
                                ps.SheetProperties.FromSheetProperties((SheetProperties)oxr.LoadCurrentElement());
                            }
                            else if (oxr.ElementType == typeof(SheetView))
                            {
                                // if we find a sheet view with the default workbook view id,
                                // we'll just take that.
                                if (!bFound)
                                {
                                    slsv = new SLSheetView();
                                    slsv.FromSheetView((SheetView)oxr.LoadCurrentElement());
                                    if (slsv.ShowFormulas) ps.bShowFormulas = slsv.ShowFormulas;
                                    if (!slsv.ShowGridLines) ps.bShowGridLines = slsv.ShowGridLines;
                                    if (!slsv.ShowRowColHeaders) ps.bShowRowColumnHeaders = slsv.ShowRowColHeaders;
                                    if (!slsv.ShowRuler) ps.bShowRuler = slsv.ShowRuler;
                                    if (slsv.View != SheetViewValues.Normal) ps.vView = slsv.View;
                                    if (slsv.ZoomScale != 100) ps.ZoomScale = slsv.ZoomScale;
                                    if (slsv.ZoomScaleNormal != 0) ps.ZoomScaleNormal = slsv.ZoomScaleNormal;
                                    if (slsv.ZoomScalePageLayoutView != 0) ps.ZoomScalePageLayoutView = slsv.ZoomScalePageLayoutView;

                                    if (slsv.WorkbookViewId == 0) bFound = true;
                                }
                            }
                            else if (oxr.ElementType == typeof(PrintOptions))
                            {
                                ps.ImportPrintOptions((PrintOptions)oxr.LoadCurrentElement());
                            }
                            else if (oxr.ElementType == typeof(PageMargins))
                            {
                                ps.ImportPageMargins((PageMargins)oxr.LoadCurrentElement());
                            }
                            else if (oxr.ElementType == typeof(PageSetup))
                            {
                                ps.ImportPageSetup((PageSetup)oxr.LoadCurrentElement());
                            }
                            else if (oxr.ElementType == typeof(HeaderFooter))
                            {
                                ps.ImportHeaderFooter((HeaderFooter)oxr.LoadCurrentElement());
                            }
                        }
                    }
                }
                else if (sheet.SheetType == SLSheetType.Chartsheet)
                {
                    ChartsheetPart csp = (ChartsheetPart)wbp.GetPartById(sheet.Id);
                    if (csp.Chartsheet.ChartSheetProperties != null)
                    {
                        ps.SheetProperties.FromChartSheetProperties(csp.Chartsheet.ChartSheetProperties);
                    }
                    if (csp.Chartsheet.PageMargins != null)
                    {
                        ps.ImportPageMargins(csp.Chartsheet.PageMargins);
                    }
                    if (csp.Chartsheet.ChartSheetPageSetup != null)
                    {
                        ps.ImportChartSheetPageSetup(csp.Chartsheet.ChartSheetPageSetup);
                    }
                    if (csp.Chartsheet.HeaderFooter != null)
                    {
                        ps.ImportHeaderFooter(csp.Chartsheet.HeaderFooter);
                    }
                }
                else if (sheet.SheetType == SLSheetType.DialogSheet)
                {
                    DialogsheetPart dsp = (DialogsheetPart)wbp.GetPartById(sheet.Id);
                    if (dsp.DialogSheet.SheetProperties != null)
                    {
                        ps.SheetProperties.FromSheetProperties(dsp.DialogSheet.SheetProperties);
                    }
                    if (dsp.DialogSheet.PrintOptions != null)
                    {
                        ps.ImportPrintOptions(dsp.DialogSheet.PrintOptions);
                    }
                    if (dsp.DialogSheet.PageMargins != null)
                    {
                        ps.ImportPageMargins(dsp.DialogSheet.PageMargins);
                    }
                    if (dsp.DialogSheet.PageSetup != null)
                    {
                        ps.ImportPageSetup(dsp.DialogSheet.PageSetup);
                    }
                    if (dsp.DialogSheet.HeaderFooter != null)
                    {
                        ps.ImportHeaderFooter(dsp.DialogSheet.HeaderFooter);
                    }
                }
                else if (sheet.SheetType == SLSheetType.Macrosheet)
                {
                    // not doing anything for macrosheets. What *are* macrosheets?
                }
            }

            return ps;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get the page settings of sheet.
        /// </summary>
        /// <param name="SheetName">The name of the sheet.</param>
        /// <returns></returns>
        public SLPageSettings GetPageSettings(string SheetName)
        {
            if (SheetName.Equals(gsSelectedWorksheetName, StringComparison.InvariantCultureIgnoreCase))
            {
                return slws.PageSettings.Clone();
            }

            SLPageSettings ps = new SLPageSettings(SimpleTheme.listThemeColors, SimpleTheme.listIndexedColors);

            bool bSheetFound = false;
            SLSheet sheet = new SLSheet(string.Empty, 0, string.Empty, SLSheetType.Unknown);
            foreach (SLSheet s in slwb.Sheets)
            {
                if (s.Name.Equals(SheetName, StringComparison.InvariantCultureIgnoreCase))
                {
                    bSheetFound = true;
                    sheet = s.Clone();
                    break;
                }
            }

            if (bSheetFound)
            {
                if (sheet.SheetType == SLSheetType.Worksheet)
                {
                    WorksheetPart wsp = (WorksheetPart)wbp.GetPartById(sheet.Id);
                    using (OpenXmlReader oxr = OpenXmlReader.Create(wsp))
                    {
                        while (oxr.Read())
                        {
                            if (oxr.ElementType == typeof(SheetProperties))
                            {
                                ps.SheetProperties.FromSheetProperties((SheetProperties)oxr.LoadCurrentElement());
                            }
                            else if (oxr.ElementType == typeof(PrintOptions))
                            {
                                ps.ImportPrintOptions((PrintOptions)oxr.LoadCurrentElement());
                            }
                            else if (oxr.ElementType == typeof(PageMargins))
                            {
                                ps.ImportPageMargins((PageMargins)oxr.LoadCurrentElement());
                            }
                            else if (oxr.ElementType == typeof(PageSetup))
                            {
                                ps.ImportPageSetup((PageSetup)oxr.LoadCurrentElement());
                            }
                            else if (oxr.ElementType == typeof(HeaderFooter))
                            {
                                ps.ImportHeaderFooter((HeaderFooter)oxr.LoadCurrentElement());
                            }
                        }
                    }
                }
                else if (sheet.SheetType == SLSheetType.Chartsheet)
                {
                    ChartsheetPart csp = (ChartsheetPart)wbp.GetPartById(sheet.Id);
                    if (csp.Chartsheet.ChartSheetProperties != null)
                    {
                        ps.SheetProperties.FromChartSheetProperties(csp.Chartsheet.ChartSheetProperties);
                    }
                    if (csp.Chartsheet.PageMargins != null)
                    {
                        ps.ImportPageMargins(csp.Chartsheet.PageMargins);
                    }
                    if (csp.Chartsheet.ChartSheetPageSetup != null)
                    {
                        ps.ImportChartSheetPageSetup(csp.Chartsheet.ChartSheetPageSetup);
                    }
                    if (csp.Chartsheet.HeaderFooter != null)
                    {
                        ps.ImportHeaderFooter(csp.Chartsheet.HeaderFooter);
                    }
                }
                else if (sheet.SheetType == SLSheetType.DialogSheet)
                {
                    DialogsheetPart dsp = (DialogsheetPart)wbp.GetPartById(sheet.Id);
                    if (dsp.DialogSheet.SheetProperties != null)
                    {
                        ps.SheetProperties.FromSheetProperties(dsp.DialogSheet.SheetProperties);
                    }
                    if (dsp.DialogSheet.PrintOptions != null)
                    {
                        ps.ImportPrintOptions(dsp.DialogSheet.PrintOptions);
                    }
                    if (dsp.DialogSheet.PageMargins != null)
                    {
                        ps.ImportPageMargins(dsp.DialogSheet.PageMargins);
                    }
                    if (dsp.DialogSheet.PageSetup != null)
                    {
                        ps.ImportPageSetup(dsp.DialogSheet.PageSetup);
                    }
                    if (dsp.DialogSheet.HeaderFooter != null)
                    {
                        ps.ImportHeaderFooter(dsp.DialogSheet.HeaderFooter);
                    }
                }
                else if (sheet.SheetType == SLSheetType.Macrosheet)
                {
                    // not doing anything for macrosheets. What *are* macrosheets?
                }
            }

            return ps;
        }