/******************************************************************************* * You may amend and distribute as you like, but don't remove this header! * * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets. * See http://www.codeplex.com/EPPlus for details. * * Copyright (C) 2011 Jan Källman * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html * * All code and executables are provided "as is" with no warranty either express or implied. * The author accepts no liability for any damage or loss of business that this product may cause. * * Code change notes: * * Author Change Date * ****************************************************************************** * Jan Källman Initial Release 2009-10-01 * Jan Källman License changed GPL-->LGPL 2011-12-27 *******************************************************************************/ using System; using System.Collections.Generic; using System.Text; using System.Xml; using System.Globalization; namespace OfficeOpenXml { #region "Enums" /// /// Printer orientation /// public enum eOrientation { /// /// Portrait orientation /// Portrait, /// /// Landscape orientation /// Landscape } /// /// Papersize /// public enum ePaperSize { /// /// Letter paper (8.5 in. by 11 in.) /// Letter= 1, /// /// Letter small paper (8.5 in. by 11 in.) /// LetterSmall=2, /// /// // Tabloid paper (11 in. by 17 in.) /// Tabloid=3, /// /// Ledger paper (17 in. by 11 in.) /// Ledger=4, /// /// Legal paper (8.5 in. by 14 in.) /// Legal=5, /// /// Statement paper (5.5 in. by 8.5 in.) /// Statement=6, /// /// Executive paper (7.25 in. by 10.5 in.) /// Executive=7, /// /// A3 paper (297 mm by 420 mm) /// A3=8, /// /// A4 paper (210 mm by 297 mm) /// A4=9, /// /// A4 small paper (210 mm by 297 mm) /// A4Small=10, /// /// A5 paper (148 mm by 210 mm) /// A5=11, /// /// B4 paper (250 mm by 353 mm) /// B4=12, /// /// B5 paper (176 mm by 250 mm) /// B5=13, /// /// Folio paper (8.5 in. by 13 in.) /// Folio=14, /// /// Quarto paper (215 mm by 275 mm) /// Quarto=15, /// /// Standard paper (10 in. by 14 in.) /// Standard10_14=16, /// /// Standard paper (11 in. by 17 in.) /// Standard11_17=17, /// /// Note paper (8.5 in. by 11 in.) /// Note=18, /// /// #9 envelope (3.875 in. by 8.875 in.) /// Envelope9=19, /// /// #10 envelope (4.125 in. by 9.5 in.) /// Envelope10=20, /// /// #11 envelope (4.5 in. by 10.375 in.) /// Envelope11=21, /// /// #12 envelope (4.75 in. by 11 in.) /// Envelope12=22, /// /// #14 envelope (5 in. by 11.5 in.) /// Envelope14=23, /// /// C paper (17 in. by 22 in.) /// C=24, /// /// D paper (22 in. by 34 in.) /// D=25, /// /// E paper (34 in. by 44 in.) /// E=26, /// /// DL envelope (110 mm by 220 mm) /// DLEnvelope = 27, /// /// C5 envelope (162 mm by 229 mm) /// C5Envelope = 28, /// /// C3 envelope (324 mm by 458 mm) /// C3Envelope = 29, /// /// C4 envelope (229 mm by 324 mm) /// C4Envelope = 30, /// /// C6 envelope (114 mm by 162 mm) /// C6Envelope = 31, /// /// C65 envelope (114 mm by 229 mm) /// C65Envelope = 32, /// /// B4 envelope (250 mm by 353 mm) /// B4Envelope= 33, /// /// B5 envelope (176 mm by 250 mm) /// B5Envelope= 34, /// /// B6 envelope (176 mm by 125 mm) /// B6Envelope = 35, /// /// Italy envelope (110 mm by 230 mm) /// ItalyEnvelope = 36, /// /// Monarch envelope (3.875 in. by 7.5 in.). /// MonarchEnvelope = 37, /// /// 6 3/4 envelope (3.625 in. by 6.5 in.) /// Six3_4Envelope = 38, /// /// US standard fanfold (14.875 in. by 11 in.) /// USStandard=39, /// /// German standard fanfold (8.5 in. by 12 in.) /// GermanStandard=40, /// /// German legal fanfold (8.5 in. by 13 in.) /// GermanLegal=41, /// /// ISO B4 (250 mm by 353 mm) /// ISOB4=42, /// /// Japanese double postcard (200 mm by 148 mm) /// JapaneseDoublePostcard=43, /// /// Standard paper (9 in. by 11 in.) /// Standard9=44, /// /// Standard paper (10 in. by 11 in.) /// Standard10=45, /// /// Standard paper (15 in. by 11 in.) /// Standard15=46, /// /// Invite envelope (220 mm by 220 mm) /// InviteEnvelope = 47, /// /// Letter extra paper (9.275 in. by 12 in.) /// LetterExtra=50, /// /// Legal extra paper (9.275 in. by 15 in.) /// LegalExtra=51, /// /// Tabloid extra paper (11.69 in. by 18 in.) /// TabloidExtra=52, /// /// A4 extra paper (236 mm by 322 mm) /// A4Extra=53, /// /// Letter transverse paper (8.275 in. by 11 in.) /// LetterTransverse=54, /// /// A4 transverse paper (210 mm by 297 mm) /// A4Transverse=55, /// /// Letter extra transverse paper (9.275 in. by 12 in.) /// LetterExtraTransverse=56, /// /// SuperA/SuperA/A4 paper (227 mm by 356 mm) /// SuperA=57, /// /// SuperB/SuperB/A3 paper (305 mm by 487 mm) /// SuperB=58, /// /// Letter plus paper (8.5 in. by 12.69 in.) /// LetterPlus=59, /// /// A4 plus paper (210 mm by 330 mm) /// A4Plus=60, /// /// A5 transverse paper (148 mm by 210 mm) /// A5Transverse=61, /// /// JIS B5 transverse paper (182 mm by 257 mm) /// JISB5Transverse=62, /// /// A3 extra paper (322 mm by 445 mm) /// A3Extra=63, /// /// A5 extra paper (174 mm by 235 mm) /// A5Extra=64, /// /// ISO B5 extra paper (201 mm by 276 mm) /// ISOB5=65, /// /// A2 paper (420 mm by 594 mm) /// A2=66, /// /// A3 transverse paper (297 mm by 420 mm) /// A3Transverse=67, /// /// A3 extra transverse paper (322 mm by 445 mm*/ /// A3ExtraTransverse = 68 } /// /// Specifies printed page order /// public enum ePageOrder { /// /// Order pages vertically first, then move horizontally. /// DownThenOver, /// /// Order pages horizontally first, then move vertically /// OverThenDown } #endregion /// /// Printer settings /// public sealed class ExcelPrinterSettings : XmlHelper { ExcelWorksheet _ws; bool _marginsCreated = false; internal ExcelPrinterSettings(XmlNamespaceManager ns, XmlNode topNode,ExcelWorksheet ws) : base(ns, topNode) { _ws = ws; SchemaNodeOrder = ws.SchemaNodeOrder; } const string _leftMarginPath = "d:pageMargins/@left"; /// /// Left margin in inches /// public decimal LeftMargin { get { return GetXmlNodeDecimal(_leftMarginPath); } set { CreateMargins(); SetXmlNodeString(_leftMarginPath, value.ToString(CultureInfo.InvariantCulture)); } } const string _rightMarginPath = "d:pageMargins/@right"; /// /// Right margin in inches /// public decimal RightMargin { get { return GetXmlNodeDecimal(_rightMarginPath); } set { CreateMargins(); SetXmlNodeString(_rightMarginPath, value.ToString(CultureInfo.InvariantCulture)); } } const string _topMarginPath = "d:pageMargins/@top"; /// /// Top margin in inches /// public decimal TopMargin { get { return GetXmlNodeDecimal(_topMarginPath); } set { CreateMargins(); SetXmlNodeString(_topMarginPath, value.ToString(CultureInfo.InvariantCulture)); } } const string _bottomMarginPath = "d:pageMargins/@bottom"; /// /// Bottom margin in inches /// public decimal BottomMargin { get { return GetXmlNodeDecimal(_bottomMarginPath); } set { CreateMargins(); SetXmlNodeString(_bottomMarginPath, value.ToString(CultureInfo.InvariantCulture)); } } const string _headerMarginPath = "d:pageMargins/@header"; /// /// Header margin in inches /// public decimal HeaderMargin { get { return GetXmlNodeDecimal(_headerMarginPath); } set { CreateMargins(); SetXmlNodeString(_headerMarginPath, value.ToString(CultureInfo.InvariantCulture)); } } const string _footerMarginPath = "d:pageMargins/@footer"; /// /// Footer margin in inches /// public decimal FooterMargin { get { return GetXmlNodeDecimal(_footerMarginPath); } set { CreateMargins(); SetXmlNodeString(_footerMarginPath, value.ToString(CultureInfo.InvariantCulture)); } } const string _orientationPath = "d:pageSetup/@orientation"; /// /// Orientation /// Portrait or Landscape /// public eOrientation Orientation { get { return (eOrientation)Enum.Parse(typeof(eOrientation), GetXmlNodeString(_orientationPath), true); } set { SetXmlNodeString(_orientationPath, value.ToString().ToLower()); } } const string _fitToWidthPath = "d:pageSetup/@fitToWidth"; /// /// Fit to Width in pages. /// Set FitToPage to true when using this one. /// 0 is automatic /// public int FitToWidth { get { return GetXmlNodeInt(_fitToWidthPath); } set { SetXmlNodeString(_fitToWidthPath, value.ToString()); } } const string _fitToHeightPath = "d:pageSetup/@fitToHeight"; /// /// Fit to height in pages. /// Set FitToPage to true when using this one. /// 0 is automatic /// public int FitToHeight { get { return GetXmlNodeInt(_fitToHeightPath); } set { SetXmlNodeString(_fitToHeightPath, value.ToString()); } } const string _scalePath = "d:pageSetup/@scale"; /// /// Print scale /// public int Scale { get { return GetXmlNodeInt(_scalePath); } set { SetXmlNodeString(_scalePath, value.ToString()); } } const string _fitToPagePath = "d:sheetPr/d:pageSetUpPr/@fitToPage"; /// /// Fit To Page. /// public bool FitToPage { get { return GetXmlNodeBool(_fitToPagePath); } set { SetXmlNodeString(_fitToPagePath, value ? "1" : "0"); } } const string _headersPath = "d:printOptions/@headings"; /// /// Print headings (column letter and row numbers) /// public bool ShowHeaders { get { return GetXmlNodeBool(_headersPath, false); } set { SetXmlNodeBool(_headersPath, value, false); } } /// /// Print titles /// Rows to be repeated after each pagebreak. /// The address must be a full row address (ex. 1:1) /// public ExcelAddress RepeatRows { get { if (_ws.Names.ContainsKey("_xlnm.Print_Titles")) { ExcelRangeBase r = _ws.Names["_xlnm.Print_Titles"] as ExcelRangeBase; if (r.Start.Column == 1 && r.End.Column == ExcelPackage.MaxColumns) { return r; } else if (r.Address != null && r.Addresses[0].Start.Column == 1 && r.Addresses[0].End.Column == ExcelPackage.MaxColumns) { return r.Addresses[0]; } else { return null; } } else { return null; } } set { //Must span entire columns if (!(value.Start.Column == 1 && value.End.Column == ExcelPackage.MaxColumns)) { throw new InvalidOperationException("Address must spann full columns only (for ex. Address=\"A:A\" for the first column)."); } var vertAddr = RepeatColumns; string addr; if (vertAddr == null) { addr = value.Address; } else { addr = vertAddr.Address + "," + value.Address; } if (_ws.Names.ContainsKey("_xlnm.Print_Titles")) { _ws.Names["_xlnm.Print_Titles"].Address = addr; } else { _ws.Names.Add("_xlnm.Print_Titles", new ExcelRangeBase(_ws, addr)); } } } /// /// Print titles /// Columns to be repeated after each pagebreak. /// The address must be a full column address (ex. A:A) /// public ExcelAddress RepeatColumns { get { if (_ws.Names.ContainsKey("_xlnm.Print_Titles")) { ExcelRangeBase r = _ws.Names["_xlnm.Print_Titles"] as ExcelRangeBase; if (r.Start.Row == 1 && r.End.Row == ExcelPackage.MaxRows) { return r; } else { return null; } } else { return null; } } set { //Must span entire rows if (!(value.Start.Row == 1 && value.End.Row== ExcelPackage.MaxRows)) { throw new InvalidOperationException("Address must spann rows only (for ex. Address=\"1:1\" for the first row)."); } var horAddr = RepeatRows; string addr; if (horAddr == null) { addr = value.Address; } else { addr = value.Address + "," + horAddr.Address; } if (_ws.Names.ContainsKey("_xlnm.Print_Titles")) { _ws.Names["_xlnm.Print_Titles"].Address = addr; } else { _ws.Names.Add("_xlnm.Print_Titles", new ExcelRangeBase(_ws, addr)); } } } /// /// The printarea. /// Null if no print area is set. /// public ExcelRangeBase PrintArea { get { if (_ws.Names.ContainsKey("_xlnm.Print_Area")) { return _ws.Names["_xlnm.Print_Area"]; } else { return null; } } set { if (value == null) { _ws.Names.Remove("_xlnm.Print_Area"); } else if (_ws.Names.ContainsKey("_xlnm.Print_Area")) { _ws.Names["_xlnm.Print_Area"].Address = value.Address; } else { _ws.Names.Add("_xlnm.Print_Area", value); } } } const string _gridLinesPath = "d:printOptions/@gridLines"; /// /// Print gridlines /// public bool ShowGridLines { get { return GetXmlNodeBool(_gridLinesPath, false); } set { SetXmlNodeBool(_gridLinesPath, value, false); } } const string _horizontalCenteredPath = "d:printOptions/@horizontalCentered"; /// /// Horizontal centered when printing /// w public bool HorizontalCentered { get { return GetXmlNodeBool(_horizontalCenteredPath, false); } set { SetXmlNodeBool(_horizontalCenteredPath, value, false); } } const string _verticalCenteredPath = "d:printOptions/@verticalCentered"; /// /// Vertical centered when printing /// public bool VerticalCentered { get { return GetXmlNodeBool(_verticalCenteredPath, false); } set { SetXmlNodeBool(_verticalCenteredPath, value, false); } } const string _pageOrderPath = "d:pageSetup/@pageOrder"; /// /// Specifies printed page order /// public ePageOrder PageOrder { get { if (GetXmlNodeString(_pageOrderPath) == "overThenDown") { return ePageOrder.OverThenDown; } else { return ePageOrder.DownThenOver; } } set { if (value == ePageOrder.OverThenDown) { SetXmlNodeString(_pageOrderPath, "overThenDown"); } else { DeleteNode(_pageOrderPath); } } } const string _blackAndWhitePath = "d:pageSetup/@blackAndWhite"; /// /// Print black and white /// public bool BlackAndWhite { get { return GetXmlNodeBool(_blackAndWhitePath, false); } set { SetXmlNodeBool(_blackAndWhitePath, value, false); } } const string _draftPath = "d:pageSetup/@draft"; /// /// Print a draft /// public bool Draft { get { return GetXmlNodeBool(_draftPath, false); } set { SetXmlNodeBool(_draftPath, value, false); } } const string _paperSizePath = "d:pageSetup/@paperSize"; /// /// Paper size /// public ePaperSize PaperSize { get { string s = GetXmlNodeString(_paperSizePath); if (s != "") { return (ePaperSize)int.Parse(s); } else { return ePaperSize.Letter; } } set { SetXmlNodeString(_paperSizePath, ((int)value).ToString()); } } /// /// All or none of the margin attributes must exist. Create all att ones. /// private void CreateMargins() { if (_marginsCreated==false && TopNode.SelectSingleNode(_leftMarginPath, NameSpaceManager) == null) { _marginsCreated=true; LeftMargin = 0.7087M; RightMargin = 0.7087M; TopMargin = 0.7480M; BottomMargin = 0.7480M; HeaderMargin = 0.315M; FooterMargin = 0.315M; } } } }