[9580] | 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 | using OfficeOpenXml.Style;
|
---|
| 35 | namespace OfficeOpenXml
|
---|
| 36 | {
|
---|
| 37 | /// <summary>
|
---|
| 38 | /// Represents one or more columns within the worksheet
|
---|
| 39 | /// </summary>
|
---|
| 40 | public class ExcelColumn : IRangeID
|
---|
| 41 | {
|
---|
| 42 | private ExcelWorksheet _worksheet;
|
---|
| 43 | private XmlElement _colElement = null;
|
---|
| 44 |
|
---|
| 45 | #region ExcelColumn Constructor
|
---|
| 46 | /// <summary>
|
---|
| 47 | /// Creates a new instance of the ExcelColumn class.
|
---|
| 48 | /// For internal use only!
|
---|
| 49 | /// </summary>
|
---|
| 50 | /// <param name="Worksheet"></param>
|
---|
| 51 | /// <param name="col"></param>
|
---|
| 52 | protected internal ExcelColumn(ExcelWorksheet Worksheet, int col)
|
---|
| 53 | {
|
---|
| 54 | _worksheet = Worksheet;
|
---|
| 55 | _columnMin = col;
|
---|
| 56 | _columnMax = col;
|
---|
| 57 | _width = _worksheet.DefaultColWidth;
|
---|
| 58 | }
|
---|
| 59 | #endregion
|
---|
| 60 | int _columnMin;
|
---|
| 61 | /// <summary>
|
---|
| 62 | /// Sets the first column the definition refers to.
|
---|
| 63 | /// </summary>
|
---|
| 64 | public int ColumnMin
|
---|
| 65 | {
|
---|
| 66 | get { return _columnMin; }
|
---|
| 67 | //set { _columnMin=value; }
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | internal int _columnMax;
|
---|
| 71 | /// <summary>
|
---|
| 72 | /// Sets the last column the definition refers to.
|
---|
| 73 | /// </summary>
|
---|
| 74 | public int ColumnMax
|
---|
| 75 | {
|
---|
| 76 | get { return _columnMax; }
|
---|
| 77 | set
|
---|
| 78 | {
|
---|
| 79 | if (value < _columnMin && value > ExcelPackage.MaxColumns)
|
---|
| 80 | {
|
---|
| 81 | throw new Exception("ColumnMax out of range");
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | foreach (ExcelColumn c in _worksheet._columns)
|
---|
| 85 | {
|
---|
| 86 | if (c.ColumnMin > _columnMin && c.ColumnMax <= value && c.ColumnMin!=_columnMin)
|
---|
| 87 | {
|
---|
| 88 | throw new Exception(string.Format("ColumnMax can not spann over existing column {0}.",c.ColumnMin));
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | _columnMax = value;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
| 94 | /// <summary>
|
---|
| 95 | /// Internal range id for the column
|
---|
| 96 | /// </summary>
|
---|
| 97 | internal ulong ColumnID
|
---|
| 98 | {
|
---|
| 99 | get
|
---|
| 100 | {
|
---|
| 101 | return ExcelColumn.GetColumnID(_worksheet.SheetID, ColumnMin);
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 | #region ExcelColumn Hidden
|
---|
| 105 | /// <summary>
|
---|
| 106 | /// Allows the column to be hidden in the worksheet
|
---|
| 107 | /// </summary>
|
---|
| 108 | internal bool _hidden=false;
|
---|
| 109 | public bool Hidden
|
---|
| 110 | {
|
---|
| 111 | get
|
---|
| 112 | {
|
---|
| 113 | return _hidden;
|
---|
| 114 | }
|
---|
| 115 | set
|
---|
| 116 | {
|
---|
| 117 | if (_worksheet._package.DoAdjustDrawings)
|
---|
| 118 | {
|
---|
| 119 | var pos = _worksheet.Drawings.GetDrawingWidths();
|
---|
| 120 | _hidden = value;
|
---|
| 121 | _worksheet.Drawings.AdjustWidth(pos);
|
---|
| 122 | }
|
---|
| 123 | else
|
---|
| 124 | {
|
---|
| 125 | _hidden = value;
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | #endregion
|
---|
| 130 |
|
---|
| 131 | #region ExcelColumn Width
|
---|
| 132 | internal double VisualWidth
|
---|
| 133 | {
|
---|
| 134 | get
|
---|
| 135 | {
|
---|
| 136 | if (_hidden || (Collapsed && OutlineLevel>0))
|
---|
| 137 | {
|
---|
| 138 | return 0;
|
---|
| 139 | }
|
---|
| 140 | else
|
---|
| 141 | {
|
---|
| 142 | return _width;
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 | }
|
---|
| 146 | internal double _width;
|
---|
| 147 | /// <summary>
|
---|
| 148 | /// Sets the width of the column in the worksheet
|
---|
| 149 | /// </summary>
|
---|
| 150 | public double Width
|
---|
| 151 | {
|
---|
| 152 | get
|
---|
| 153 | {
|
---|
| 154 | return _width;
|
---|
| 155 | }
|
---|
| 156 | set
|
---|
| 157 | {
|
---|
| 158 | if (_worksheet._package.DoAdjustDrawings)
|
---|
| 159 | {
|
---|
| 160 | var pos = _worksheet.Drawings.GetDrawingWidths();
|
---|
| 161 | _width = value;
|
---|
| 162 | _worksheet.Drawings.AdjustWidth(pos);
|
---|
| 163 | }
|
---|
| 164 | else
|
---|
| 165 | {
|
---|
| 166 | _width = value;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | if (_hidden && value!=0)
|
---|
| 170 | {
|
---|
| 171 | _hidden = false;
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 | /// <summary>
|
---|
| 176 | /// If set to true a column automaticlly resize(grow wider) when a user inputs numbers in a cell.
|
---|
| 177 | /// </summary>
|
---|
| 178 | public bool BestFit
|
---|
| 179 | {
|
---|
| 180 | get;
|
---|
| 181 | set;
|
---|
| 182 | }
|
---|
| 183 | /// <summary>
|
---|
| 184 | /// If the column is collapsed in outline mode
|
---|
| 185 | /// </summary>
|
---|
| 186 | public bool Collapsed { get; set; }
|
---|
| 187 | /// <summary>
|
---|
| 188 | /// Outline level. Zero if no outline
|
---|
| 189 | /// </summary>
|
---|
| 190 | public int OutlineLevel { get; set; }
|
---|
| 191 | /// <summary>
|
---|
| 192 | /// Phonetic
|
---|
| 193 | /// </summary>
|
---|
| 194 | public bool Phonetic { get; set; }
|
---|
| 195 | #endregion
|
---|
| 196 |
|
---|
| 197 | #region ExcelColumn Style
|
---|
| 198 | /// <summary>
|
---|
| 199 | /// The Style applied to the whole column. Only effects cells with no individual style set.
|
---|
| 200 | /// Use Range object if you want to set specific styles.
|
---|
| 201 | /// </summary>
|
---|
| 202 | public ExcelStyle Style
|
---|
| 203 | {
|
---|
| 204 | get
|
---|
| 205 | {
|
---|
| 206 | string letter = ExcelCell.GetColumnLetter(ColumnMin);
|
---|
| 207 | string endLetter = ExcelCell.GetColumnLetter(ColumnMax);
|
---|
| 208 | return _worksheet.Workbook.Styles.GetStyleObject(_styleID, _worksheet.PositionID, letter + ":" + endLetter);
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 | internal string _styleName="";
|
---|
| 212 | /// <summary>
|
---|
| 213 | /// Sets the style for the entire column using a style name.
|
---|
| 214 | /// </summary>
|
---|
| 215 | public string StyleName
|
---|
| 216 | {
|
---|
| 217 | get
|
---|
| 218 | {
|
---|
| 219 | return _styleName;
|
---|
| 220 | }
|
---|
| 221 | set
|
---|
| 222 | {
|
---|
| 223 | _styleID = _worksheet.Workbook.Styles.GetStyleIdFromName(value);
|
---|
| 224 | _styleName = value;
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 | internal int _styleID = 0;
|
---|
| 228 | /// <summary>
|
---|
| 229 | /// Sets the style for the entire column using the style ID.
|
---|
| 230 | /// </summary>
|
---|
| 231 | public int StyleID
|
---|
| 232 | {
|
---|
| 233 | get
|
---|
| 234 | {
|
---|
| 235 | return _styleID;
|
---|
| 236 | }
|
---|
| 237 | set
|
---|
| 238 | {
|
---|
| 239 | _styleID = value;
|
---|
| 240 | }
|
---|
| 241 | }
|
---|
| 242 | /// <summary>
|
---|
| 243 | /// Adds a manual page break after the column.
|
---|
| 244 | /// </summary>
|
---|
| 245 | public bool PageBreak
|
---|
| 246 | {
|
---|
| 247 | get;
|
---|
| 248 | set;
|
---|
| 249 | }
|
---|
| 250 | #endregion
|
---|
| 251 |
|
---|
| 252 | /// <summary>
|
---|
| 253 | /// Returns the range of columns covered by the column definition.
|
---|
| 254 | /// </summary>
|
---|
| 255 | /// <returns>A string describing the range of columns covered by the column definition.</returns>
|
---|
| 256 | public override string ToString()
|
---|
| 257 | {
|
---|
| 258 | return string.Format("Column Range: {0} to {1}", _colElement.GetAttribute("min"), _colElement.GetAttribute("min"));
|
---|
| 259 | }
|
---|
| 260 | /// <summary>
|
---|
| 261 | /// Set the column width from the content of the range. The minimum width is the value of the ExcelWorksheet.defaultColumnWidth property.
|
---|
| 262 | /// Note: Cells containing formulas are ignored since EPPlus don't have a calculation engine.
|
---|
| 263 | /// Wrapped and merged cells are also ignored.
|
---|
| 264 | /// </summary>
|
---|
| 265 | public void AutoFit()
|
---|
| 266 | {
|
---|
| 267 | _worksheet.Cells[1, _columnMin, ExcelPackage.MaxRows, _columnMax].AutoFitColumns();
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | /// <summary>
|
---|
| 271 | /// Set the column width from the content.
|
---|
| 272 | /// Note: Cells containing formulas are ignored since EPPlus don't have a calculation engine.
|
---|
| 273 | /// Wrapped and merged cells are also ignored.
|
---|
| 274 | /// </summary>
|
---|
| 275 | /// <param name="MinimumWidth">Minimum column width</param>
|
---|
| 276 | public void AutoFit(double MinimumWidth)
|
---|
| 277 | {
|
---|
| 278 | _worksheet.Cells[1, _columnMin, ExcelPackage.MaxRows, _columnMax].AutoFitColumns(MinimumWidth);
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | /// <summary>
|
---|
| 282 | /// Set the column width from the content.
|
---|
| 283 | /// Note: Cells containing formulas are ignored since EPPlus don't have a calculation engine.
|
---|
| 284 | /// Wrapped and merged cells are also ignored.
|
---|
| 285 | /// </summary>
|
---|
| 286 | /// <param name="MinimumWidth">Minimum column width</param>
|
---|
| 287 | /// <param name="MaximumWidth">Maximum column width</param>
|
---|
| 288 | public void AutoFit(double MinimumWidth, double MaximumWidth)
|
---|
| 289 | {
|
---|
| 290 | _worksheet.Cells[1, _columnMin, ExcelPackage.MaxRows, _columnMax].AutoFitColumns(MinimumWidth, MaximumWidth);
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | /// <summary>
|
---|
| 294 | /// Get the internal RangeID
|
---|
| 295 | /// </summary>
|
---|
| 296 | /// <param name="sheetID">Sheet no</param>
|
---|
| 297 | /// <param name="column">Column</param>
|
---|
| 298 | /// <returns></returns>
|
---|
| 299 | internal static ulong GetColumnID(int sheetID, int column)
|
---|
| 300 | {
|
---|
| 301 | return ((ulong)sheetID) + (((ulong)column) << 15);
|
---|
| 302 | }
|
---|
| 303 |
|
---|
| 304 | #region IRangeID Members
|
---|
| 305 |
|
---|
| 306 | ulong IRangeID.RangeID
|
---|
| 307 | {
|
---|
| 308 | get
|
---|
| 309 | {
|
---|
| 310 | return ColumnID;
|
---|
| 311 | }
|
---|
| 312 | set
|
---|
| 313 | {
|
---|
| 314 | int prevColMin = _columnMin;
|
---|
| 315 | _columnMin = ((int)(value >> 15) & 0x3FF);
|
---|
| 316 | _columnMax += prevColMin - ColumnMin;
|
---|
| 317 | //Todo:More Validation
|
---|
| 318 | if (_columnMax > ExcelPackage.MaxColumns) _columnMax = ExcelPackage.MaxColumns;
|
---|
| 319 | }
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | #endregion
|
---|
| 323 |
|
---|
| 324 | /// <summary>
|
---|
| 325 | /// Copies the current column to a new worksheet
|
---|
| 326 | /// </summary>
|
---|
| 327 | /// <param name="added">The worksheet where the copy will be created</param>
|
---|
| 328 | internal void Clone(ExcelWorksheet added)
|
---|
| 329 | {
|
---|
| 330 | ExcelColumn newCol = added.Column(ColumnMin);
|
---|
| 331 | newCol.ColumnMax = ColumnMax;
|
---|
| 332 | newCol.BestFit = BestFit;
|
---|
| 333 | newCol.Collapsed = Collapsed;
|
---|
| 334 | newCol.Hidden = Hidden;
|
---|
| 335 | newCol.OutlineLevel = OutlineLevel;
|
---|
| 336 | newCol.PageBreak = PageBreak;
|
---|
| 337 | newCol.Phonetic = Phonetic;
|
---|
| 338 | newCol.StyleName = StyleName;
|
---|
| 339 | newCol.StyleID = StyleID;
|
---|
| 340 | newCol.Width = Width;
|
---|
| 341 | }
|
---|
| 342 | }
|
---|
| 343 | }
|
---|