[12074] | 1 | /*******************************************************************************
|
---|
| 2 | * You may amend and distribute as you like, but don't remove this header!
|
---|
| 3 | *
|
---|
| 4 | * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
|
---|
| 5 | * See http://www.codeplex.com/EPPlus for details.
|
---|
| 6 | *
|
---|
| 7 | * Copyright (C) 2011 Jan Källman
|
---|
| 8 | *
|
---|
| 9 | * This library is free software; you can redistribute it and/or
|
---|
| 10 | * modify it under the terms of the GNU Lesser General Public
|
---|
| 11 | * License as published by the Free Software Foundation; either
|
---|
| 12 | * version 2.1 of the License, or (at your option) any later version.
|
---|
| 13 |
|
---|
| 14 | * This library is distributed in the hope that it will be useful,
|
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
| 17 | * See the GNU Lesser General Public License for more details.
|
---|
| 18 | *
|
---|
| 19 | * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
|
---|
| 20 | * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
|
---|
| 21 | *
|
---|
| 22 | * All code and executables are provided "as is" with no warranty either express or implied.
|
---|
| 23 | * The author accepts no liability for any damage or loss of business that this product may cause.
|
---|
| 24 | *
|
---|
| 25 | * Code change notes:
|
---|
| 26 | *
|
---|
| 27 | * Author Change Date
|
---|
| 28 | * ******************************************************************************
|
---|
| 29 | * Jan Källman Initial Release 2009-10-01
|
---|
| 30 | * Jan Källman License changed GPL-->LGPL 2011-12-27
|
---|
| 31 | *******************************************************************************/
|
---|
| 32 | using System;
|
---|
| 33 | using System.Xml;
|
---|
| 34 |
|
---|
| 35 | namespace OfficeOpenXml
|
---|
| 36 | {
|
---|
| 37 | /// <summary>
|
---|
| 38 | /// Represents the different view states of the worksheet
|
---|
| 39 | /// </summary>
|
---|
| 40 | public class ExcelWorksheetView : XmlHelper
|
---|
| 41 | {
|
---|
| 42 | /// <summary>
|
---|
| 43 | /// The worksheet panes after a freeze or split.
|
---|
| 44 | /// </summary>
|
---|
| 45 | public class ExcelWorksheetPanes : XmlHelper
|
---|
| 46 | {
|
---|
| 47 | XmlElement _selectionNode = null;
|
---|
| 48 | internal ExcelWorksheetPanes(XmlNamespaceManager ns, XmlNode topNode) :
|
---|
| 49 | base(ns, topNode)
|
---|
| 50 | {
|
---|
| 51 | if(topNode.Name=="selection")
|
---|
| 52 | {
|
---|
| 53 | _selectionNode=topNode as XmlElement;
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | const string _activeCellPath = "@activeCell";
|
---|
| 58 | /// <summary>
|
---|
| 59 | /// Set the active cell. Must be set within the SelectedRange.
|
---|
| 60 | /// </summary>
|
---|
| 61 | public string ActiveCell
|
---|
| 62 | {
|
---|
| 63 | get
|
---|
| 64 | {
|
---|
| 65 | string address = GetXmlNodeString(_activeCellPath);
|
---|
| 66 | if (address == "")
|
---|
| 67 | {
|
---|
| 68 | return "A1";
|
---|
| 69 | }
|
---|
| 70 | return address;
|
---|
| 71 | }
|
---|
| 72 | set
|
---|
| 73 | {
|
---|
| 74 | int fromCol, fromRow, toCol, toRow;
|
---|
| 75 | if(_selectionNode==null) CreateSelectionElement();
|
---|
| 76 | ExcelCellBase.GetRowColFromAddress(value, out fromRow, out fromCol, out toRow, out toCol);
|
---|
| 77 | SetXmlNodeString(_activeCellPath, value);
|
---|
| 78 | if (((XmlElement)TopNode).GetAttribute("sqref") == "")
|
---|
| 79 | {
|
---|
| 80 |
|
---|
| 81 | SelectedRange = ExcelCellBase.GetAddress(fromRow, fromCol);
|
---|
| 82 | }
|
---|
| 83 | else
|
---|
| 84 | {
|
---|
| 85 | //TODO:Add fix for out of range here
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | private void CreateSelectionElement()
|
---|
| 91 | {
|
---|
| 92 | _selectionNode=TopNode.OwnerDocument.CreateElement("selection", ExcelPackage.schemaMain);
|
---|
| 93 | TopNode.AppendChild(_selectionNode);
|
---|
| 94 | TopNode=_selectionNode;
|
---|
| 95 | }
|
---|
| 96 | const string _selectionRangePath = "@sqref";
|
---|
| 97 | /// <summary>
|
---|
| 98 | /// Selected Cells.Used in combination with ActiveCell
|
---|
| 99 | /// </summary>
|
---|
| 100 | public string SelectedRange
|
---|
| 101 | {
|
---|
| 102 | get
|
---|
| 103 | {
|
---|
| 104 | string address = GetXmlNodeString(_selectionRangePath);
|
---|
| 105 | if (address == "")
|
---|
| 106 | {
|
---|
| 107 | return "A1";
|
---|
| 108 | }
|
---|
| 109 | return address;
|
---|
| 110 | }
|
---|
| 111 | set
|
---|
| 112 | {
|
---|
| 113 | int fromCol, fromRow, toCol, toRow;
|
---|
| 114 | if(_selectionNode==null) CreateSelectionElement();
|
---|
| 115 | ExcelCellBase.GetRowColFromAddress(value, out fromRow, out fromCol, out toRow, out toCol);
|
---|
| 116 | SetXmlNodeString(_selectionRangePath, value);
|
---|
| 117 | if (((XmlElement)TopNode).GetAttribute("activeCell") == "")
|
---|
| 118 | {
|
---|
| 119 |
|
---|
| 120 | ActiveCell = ExcelCellBase.GetAddress(fromRow, fromCol);
|
---|
| 121 | }
|
---|
| 122 | else
|
---|
| 123 | {
|
---|
| 124 | //TODO:Add fix for out of range here
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | private ExcelWorksheet _worksheet;
|
---|
| 130 |
|
---|
| 131 | #region ExcelWorksheetView Constructor
|
---|
| 132 | /// <summary>
|
---|
| 133 | /// Creates a new ExcelWorksheetView which provides access to all the view states of the worksheet.
|
---|
| 134 | /// </summary>
|
---|
| 135 | /// <param name="ns"></param>
|
---|
| 136 | /// <param name="node"></param>
|
---|
| 137 | /// <param name="xlWorksheet"></param>
|
---|
| 138 | internal ExcelWorksheetView(XmlNamespaceManager ns, XmlNode node, ExcelWorksheet xlWorksheet) :
|
---|
| 139 | base(ns, node)
|
---|
| 140 | {
|
---|
| 141 | _worksheet = xlWorksheet;
|
---|
| 142 | SchemaNodeOrder = new string[] { "sheetViews", "sheetView", "pane", "selection" };
|
---|
| 143 | Panes = LoadPanes();
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | #endregion
|
---|
| 147 | private ExcelWorksheetPanes[] LoadPanes()
|
---|
| 148 | {
|
---|
| 149 | XmlNodeList nodes = TopNode.SelectNodes("//d:selection", NameSpaceManager);
|
---|
| 150 | if(nodes.Count==0)
|
---|
| 151 | {
|
---|
| 152 | return new ExcelWorksheetPanes[] { new ExcelWorksheetPanes(NameSpaceManager, TopNode) };
|
---|
| 153 | }
|
---|
| 154 | else
|
---|
| 155 | {
|
---|
| 156 | ExcelWorksheetPanes[] panes = new ExcelWorksheetPanes[nodes.Count];
|
---|
| 157 | int i=0;
|
---|
| 158 | foreach(XmlElement elem in nodes)
|
---|
| 159 | {
|
---|
| 160 | panes[i++] = new ExcelWorksheetPanes(NameSpaceManager, elem);
|
---|
| 161 | }
|
---|
| 162 | return panes;
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 | #region SheetViewElement
|
---|
| 166 | /// <summary>
|
---|
| 167 | /// Returns a reference to the sheetView element
|
---|
| 168 | /// </summary>
|
---|
| 169 | protected internal XmlElement SheetViewElement
|
---|
| 170 | {
|
---|
| 171 | get
|
---|
| 172 | {
|
---|
| 173 | return (XmlElement)TopNode;
|
---|
| 174 | }
|
---|
| 175 | }
|
---|
| 176 | #endregion
|
---|
| 177 | #region TabSelected
|
---|
| 178 | private XmlElement _selectionNode = null;
|
---|
| 179 | private XmlElement SelectionNode
|
---|
| 180 | {
|
---|
| 181 | get
|
---|
| 182 | {
|
---|
| 183 | _selectionNode = SheetViewElement.SelectSingleNode("//d:selection", _worksheet.NameSpaceManager) as XmlElement;
|
---|
| 184 | if (_selectionNode == null)
|
---|
| 185 | {
|
---|
| 186 | _selectionNode = _worksheet.WorksheetXml.CreateElement("selection", ExcelPackage.schemaMain);
|
---|
| 187 | SheetViewElement.AppendChild(_selectionNode);
|
---|
| 188 | }
|
---|
| 189 | return _selectionNode;
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | #endregion
|
---|
| 193 | #region Public Methods & Properties
|
---|
| 194 | /// <summary>
|
---|
| 195 | /// The active cell.
|
---|
| 196 | /// </summary>
|
---|
| 197 | public string ActiveCell
|
---|
| 198 | {
|
---|
| 199 | get
|
---|
| 200 | {
|
---|
| 201 | return Panes[Panes.GetUpperBound(0)].ActiveCell;
|
---|
| 202 | }
|
---|
| 203 | set
|
---|
| 204 | {
|
---|
| 205 | Panes[Panes.GetUpperBound(0)].ActiveCell = value;
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 | /// <summary>
|
---|
| 209 | /// Selected Cells in the worksheet.Used in combination with ActiveCell
|
---|
| 210 | /// </summary>
|
---|
| 211 | public string SelectedRange
|
---|
| 212 | {
|
---|
| 213 | get
|
---|
| 214 | {
|
---|
| 215 | return Panes[Panes.GetUpperBound(0)].SelectedRange;
|
---|
| 216 | }
|
---|
| 217 | set
|
---|
| 218 | {
|
---|
| 219 | Panes[Panes.GetUpperBound(0)].SelectedRange = value;
|
---|
| 220 | }
|
---|
| 221 | }
|
---|
| 222 | /// <summary>
|
---|
| 223 | /// Indicates if the worksheet is selected within the workbook
|
---|
| 224 | /// </summary>
|
---|
| 225 | public bool TabSelected
|
---|
| 226 | {
|
---|
| 227 | get
|
---|
| 228 | {
|
---|
| 229 | return GetXmlNodeBool("@tabSelected");
|
---|
| 230 | }
|
---|
| 231 | set
|
---|
| 232 | {
|
---|
| 233 | if (value)
|
---|
| 234 | {
|
---|
| 235 | // // ensure no other worksheet has its tabSelected attribute set to 1
|
---|
| 236 | foreach (ExcelWorksheet sheet in _worksheet._package.Workbook.Worksheets)
|
---|
| 237 | sheet.View.TabSelected = false;
|
---|
| 238 |
|
---|
| 239 | SheetViewElement.SetAttribute("tabSelected", "1");
|
---|
| 240 | XmlElement bookView = _worksheet.Workbook.WorkbookXml.SelectSingleNode("//d:workbookView", _worksheet.NameSpaceManager) as XmlElement;
|
---|
| 241 | if (bookView != null)
|
---|
| 242 | {
|
---|
| 243 | bookView.SetAttribute("activeTab", (_worksheet.PositionID - 1).ToString());
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 | else
|
---|
| 247 | SetXmlNodeString("@tabSelected", "0");
|
---|
| 248 |
|
---|
| 249 | }
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | /// <summary>
|
---|
| 253 | /// Sets the view mode of the worksheet to pagelayout
|
---|
| 254 | /// </summary>
|
---|
| 255 | public bool PageLayoutView
|
---|
| 256 | {
|
---|
| 257 | get
|
---|
| 258 | {
|
---|
| 259 | return GetXmlNodeString("@view") == "pageLayout";
|
---|
| 260 | }
|
---|
| 261 | set
|
---|
| 262 | {
|
---|
| 263 | if (value)
|
---|
| 264 | SetXmlNodeString("@view", "pageLayout");
|
---|
| 265 | else
|
---|
| 266 | SheetViewElement.RemoveAttribute("view");
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
| 269 | /// <summary>
|
---|
| 270 | /// Sets the view mode of the worksheet to pagebreak
|
---|
| 271 | /// </summary>
|
---|
| 272 | public bool PageBreakView
|
---|
| 273 | {
|
---|
| 274 | get
|
---|
| 275 | {
|
---|
| 276 | return GetXmlNodeString("@view") == "pageBreakPreview";
|
---|
| 277 | }
|
---|
| 278 | set
|
---|
| 279 | {
|
---|
| 280 | if (value)
|
---|
| 281 | SetXmlNodeString("@view", "pageBreakPreview");
|
---|
| 282 | else
|
---|
| 283 | SheetViewElement.RemoveAttribute("view");
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
| 286 | /// <summary>
|
---|
| 287 | /// Show gridlines in the worksheet
|
---|
| 288 | /// </summary>
|
---|
| 289 | public bool ShowGridLines
|
---|
| 290 | {
|
---|
| 291 | get
|
---|
| 292 | {
|
---|
| 293 | return GetXmlNodeBool("@showGridLines");
|
---|
| 294 | }
|
---|
| 295 | set
|
---|
| 296 | {
|
---|
| 297 | SetXmlNodeString("@showGridLines", value ? "1" : "0");
|
---|
| 298 | }
|
---|
| 299 | }
|
---|
| 300 | /// <summary>
|
---|
| 301 | /// Show the Column/Row headers (containg column letters and row numbers)
|
---|
| 302 | /// </summary>
|
---|
| 303 | public bool ShowHeaders
|
---|
| 304 | {
|
---|
| 305 | get
|
---|
| 306 | {
|
---|
| 307 | return GetXmlNodeBool("@showRowColHeaders");
|
---|
| 308 | }
|
---|
| 309 | set
|
---|
| 310 | {
|
---|
| 311 | SetXmlNodeString("@showRowColHeaders", value ? "1" : "0");
|
---|
| 312 | }
|
---|
| 313 | }
|
---|
| 314 | /// <summary>
|
---|
| 315 | /// Window zoom magnification for current view representing percent values.
|
---|
| 316 | /// </summary>
|
---|
| 317 | public int ZoomScale
|
---|
| 318 | {
|
---|
| 319 | get
|
---|
| 320 | {
|
---|
| 321 | return GetXmlNodeInt("@zoomScale");
|
---|
| 322 | }
|
---|
| 323 | set
|
---|
| 324 | {
|
---|
| 325 | if (value < 10 || value > 400)
|
---|
| 326 | {
|
---|
| 327 | throw new ArgumentOutOfRangeException("Zoome scale out of range (10-400)");
|
---|
| 328 | }
|
---|
| 329 | SetXmlNodeString("@zoomScale", value.ToString());
|
---|
| 330 | }
|
---|
| 331 | }
|
---|
| 332 | /// <summary>
|
---|
| 333 | /// Flag indicating whether the sheet is in 'right to left' display mode. When in this mode,Column A is on the far right, Column B ;is one column left of Column A, and so on. Also,information in cells is displayed in the Right to Left format.
|
---|
| 334 | /// </summary>
|
---|
| 335 | public bool RightToLeft
|
---|
| 336 | {
|
---|
| 337 | get
|
---|
| 338 | {
|
---|
| 339 | return GetXmlNodeBool("@rightToLeft");
|
---|
| 340 | }
|
---|
| 341 | set
|
---|
| 342 | {
|
---|
| 343 | SetXmlNodeString("@rightToLeft", value == true ? "1" : "0");
|
---|
| 344 | }
|
---|
| 345 | }
|
---|
| 346 | internal bool WindowProtection
|
---|
| 347 | {
|
---|
| 348 | get
|
---|
| 349 | {
|
---|
| 350 | return GetXmlNodeBool("@windowProtection",false);
|
---|
| 351 | }
|
---|
| 352 | set
|
---|
| 353 | {
|
---|
| 354 | SetXmlNodeBool("@windowProtection",value,false);
|
---|
| 355 | }
|
---|
| 356 | }
|
---|
| 357 | /// <summary>
|
---|
| 358 | /// Reference to the panes
|
---|
| 359 | /// </summary>
|
---|
| 360 | public ExcelWorksheetPanes[] Panes
|
---|
| 361 | {
|
---|
| 362 | get;
|
---|
| 363 | internal set;
|
---|
| 364 | }
|
---|
| 365 | string _paneNodePath = "d:pane";
|
---|
| 366 | string _selectionNodePath = "d:selection";
|
---|
| 367 | /// <summary>
|
---|
| 368 | /// Freeze the columns/rows to left and above the cell
|
---|
| 369 | /// </summary>
|
---|
| 370 | /// <param name="Row"></param>
|
---|
| 371 | /// <param name="Column"></param>
|
---|
| 372 | public void FreezePanes(int Row, int Column)
|
---|
| 373 | {
|
---|
| 374 | //TODO:fix this method to handle splits as well.
|
---|
| 375 | if (Row == 1 && Column == 1) UnFreezePanes();
|
---|
| 376 | string sqRef = SelectedRange, activeCell = ActiveCell;
|
---|
| 377 |
|
---|
| 378 | XmlElement paneNode = TopNode.SelectSingleNode(_paneNodePath, NameSpaceManager) as XmlElement;
|
---|
| 379 | if (paneNode == null)
|
---|
| 380 | {
|
---|
| 381 | CreateNode(_paneNodePath);
|
---|
| 382 | paneNode = TopNode.SelectSingleNode(_paneNodePath, NameSpaceManager) as XmlElement;
|
---|
| 383 | }
|
---|
| 384 | paneNode.RemoveAll(); //Clear all attributes
|
---|
| 385 | if (Column > 1) paneNode.SetAttribute("xSplit", (Column - 1).ToString());
|
---|
| 386 | if (Row > 1) paneNode.SetAttribute("ySplit", (Row - 1).ToString());
|
---|
| 387 | paneNode.SetAttribute("topLeftCell", ExcelCellBase.GetAddress(Row, Column));
|
---|
| 388 | paneNode.SetAttribute("state", "frozen");
|
---|
| 389 |
|
---|
| 390 | RemoveSelection();
|
---|
| 391 |
|
---|
| 392 | if (Row > 1 && Column==1)
|
---|
| 393 | {
|
---|
| 394 | paneNode.SetAttribute("activePane", "bottomLeft");
|
---|
| 395 | XmlElement sel=TopNode.OwnerDocument.CreateElement("selection", ExcelPackage.schemaMain);
|
---|
| 396 | sel.SetAttribute("pane", "bottomLeft");
|
---|
| 397 | if (activeCell != "") sel.SetAttribute("activeCell", activeCell);
|
---|
| 398 | if (sqRef != "") sel.SetAttribute("sqref", sqRef);
|
---|
| 399 | sel.SetAttribute("sqref", sqRef);
|
---|
| 400 | TopNode.InsertAfter(sel, paneNode);
|
---|
| 401 | }
|
---|
| 402 | else if (Column > 1 && Row == 1)
|
---|
| 403 | {
|
---|
| 404 | paneNode.SetAttribute("activePane", "topRight");
|
---|
| 405 | XmlElement sel = TopNode.OwnerDocument.CreateElement("selection", ExcelPackage.schemaMain);
|
---|
| 406 | sel.SetAttribute("pane", "topRight");
|
---|
| 407 | if (activeCell != "") sel.SetAttribute("activeCell", activeCell);
|
---|
| 408 | if (sqRef != "") sel.SetAttribute("sqref", sqRef);
|
---|
| 409 | TopNode.InsertAfter(sel, paneNode);
|
---|
| 410 | }
|
---|
| 411 | else
|
---|
| 412 | {
|
---|
| 413 | paneNode.SetAttribute("activePane", "bottomRight");
|
---|
| 414 | XmlElement sel1 = TopNode.OwnerDocument.CreateElement("selection", ExcelPackage.schemaMain);
|
---|
| 415 | sel1.SetAttribute("pane", "topRight");
|
---|
| 416 | string cell = ExcelCellBase.GetAddress(1, Column);
|
---|
| 417 | sel1.SetAttribute("activeCell", cell);
|
---|
| 418 | sel1.SetAttribute("sqref", cell);
|
---|
| 419 | paneNode.ParentNode.InsertAfter(sel1, paneNode);
|
---|
| 420 |
|
---|
| 421 | XmlElement sel2 = TopNode.OwnerDocument.CreateElement("selection", ExcelPackage.schemaMain);
|
---|
| 422 | cell = ExcelCellBase.GetAddress(Row, 1);
|
---|
| 423 | sel2.SetAttribute("pane", "bottomLeft");
|
---|
| 424 | sel2.SetAttribute("activeCell", cell);
|
---|
| 425 | sel2.SetAttribute("sqref", cell);
|
---|
| 426 | sel1.ParentNode.InsertAfter(sel2, sel1);
|
---|
| 427 |
|
---|
| 428 | XmlElement sel3 = TopNode.OwnerDocument.CreateElement("selection", ExcelPackage.schemaMain);
|
---|
| 429 | sel3.SetAttribute("pane", "bottomRight");
|
---|
| 430 | if(activeCell!="") sel3.SetAttribute("activeCell", activeCell);
|
---|
| 431 | if(sqRef!="") sel3.SetAttribute("sqref", sqRef);
|
---|
| 432 | sel2.ParentNode.InsertAfter(sel3, sel2);
|
---|
| 433 |
|
---|
| 434 | }
|
---|
| 435 | Panes=LoadPanes();
|
---|
| 436 | }
|
---|
| 437 | private void RemoveSelection()
|
---|
| 438 | {
|
---|
| 439 | //Find selection nodes and remove them
|
---|
| 440 | XmlNodeList selections = TopNode.SelectNodes(_selectionNodePath, NameSpaceManager);
|
---|
| 441 | foreach (XmlNode sel in selections)
|
---|
| 442 | {
|
---|
| 443 | sel.ParentNode.RemoveChild(sel);
|
---|
| 444 | }
|
---|
| 445 | }
|
---|
| 446 | /// <summary>
|
---|
| 447 | /// Unlock all rows and columns to scroll freely
|
---|
| 448 | /// /// </summary>
|
---|
| 449 | public void UnFreezePanes()
|
---|
| 450 | {
|
---|
| 451 | string sqRef = SelectedRange, activeCell = ActiveCell;
|
---|
| 452 |
|
---|
| 453 | XmlElement paneNode = TopNode.SelectSingleNode(_paneNodePath, NameSpaceManager) as XmlElement;
|
---|
| 454 | if (paneNode != null)
|
---|
| 455 | {
|
---|
| 456 | paneNode.ParentNode.RemoveChild(paneNode);
|
---|
| 457 | }
|
---|
| 458 | RemoveSelection();
|
---|
| 459 |
|
---|
| 460 | Panes=LoadPanes();
|
---|
| 461 |
|
---|
| 462 | SelectedRange = sqRef;
|
---|
| 463 | ActiveCell = activeCell;
|
---|
| 464 | }
|
---|
| 465 | #endregion
|
---|
| 466 | }
|
---|
| 467 | }
|
---|