Browse Source

POI:bug 59336: patch from Mark Murphy: deprecate functions in CellUtil that require an unnecessary Workbook argument.

pull/290/head
antony liu 6 years ago
parent
commit
9aa8f3be06
  1. 29
      main/HSSF/UserModel/HSSFSheet.cs
  2. 427
      main/HSSF/Util/HSSFCellUtil.cs
  3. 16
      main/HSSF/Util/HSSFRegionUtil.cs
  4. 406
      main/SS/Util/CellUtil.cs
  5. 23
      main/SS/Util/RegionUtil.cs
  6. 33
      testcases/main/SS/Util/TestCellUtil.cs
  7. 4
      testcases/ooxml/XSSF/UserModel/TestUnfixedBugs.cs

29
main/HSSF/UserModel/HSSFSheet.cs

@ -2896,7 +2896,7 @@ namespace NPOI.HSSF.UserModel
return CopySheet(Name, true);
}
[Obsolete("since poi 3.15")]
public ISheet CopySheet(String Name, Boolean copyStyle)
{
int maxColumnNum = 0;
@ -2941,7 +2941,7 @@ namespace NPOI.HSSF.UserModel
newSheet.PrintSetup.FitWidth = PrintSetup.FitWidth;
return newSheet;
}
[Obsolete("since poi 3.15")]
public void CopyTo(HSSFWorkbook dest, String name, Boolean copyStyle, Boolean keepFormulas)
{
int maxColumnNum = 0;
@ -3125,7 +3125,7 @@ namespace NPOI.HSSF.UserModel
}
return retval;
}
[Obsolete("since poi 3.15")]
private static void CopyRow(HSSFSheet srcSheet, HSSFSheet destSheet, HSSFRow srcRow, HSSFRow destRow, IDictionary<Int32, HSSFCellStyle> styleMap, Dictionary<short, short> paletteMap, bool keepFormulas)
{
List<SS.Util.CellRangeAddress> mergedRegions = destSheet.Sheet.MergedRecords.MergedRegions;
@ -3146,18 +3146,19 @@ namespace NPOI.HSSF.UserModel
{
newCell = (HSSFCell)destRow.CreateCell(j);
}
HSSFCellUtil.CopyCell(oldCell, newCell, styleMap, paletteMap, keepFormulas);
CellRangeAddress mergedRegion = GetMergedRegion(srcSheet, srcRow.RowNum, (short)oldCell.ColumnIndex);
if (mergedRegion != null)
{
CellRangeAddress newMergedRegion = new CellRangeAddress(mergedRegion.FirstRow,
mergedRegion.LastRow, mergedRegion.FirstColumn, mergedRegion.LastColumn);
throw new NotImplementedException();
//HSSFCellUtil.CopyCell(oldCell, newCell, styleMap, paletteMap, keepFormulas);
//CellRangeAddress mergedRegion = GetMergedRegion(srcSheet, srcRow.RowNum, (short)oldCell.ColumnIndex);
//if (mergedRegion != null)
//{
// CellRangeAddress newMergedRegion = new CellRangeAddress(mergedRegion.FirstRow,
// mergedRegion.LastRow, mergedRegion.FirstColumn, mergedRegion.LastColumn);
if (IsNewMergedRegion(newMergedRegion, mergedRegions))
{
mergedRegions.Add(newMergedRegion);
}
}
// if (IsNewMergedRegion(newMergedRegion, mergedRegions))
// {
// mergedRegions.Add(newMergedRegion);
// }
//}
}
}
}

427
main/HSSF/Util/HSSFCellUtil.cs

@ -23,6 +23,7 @@ namespace NPOI.HSSF.Util
using NPOI.SS.UserModel;
using System.Collections.Generic;
using NPOI.HSSF.Record;
using NPOI.SS.Util;
/// <summary>
/// Various utility functions that make working with a cells and rows easier. The various
@ -36,62 +37,6 @@ namespace NPOI.HSSF.Util
[Obsolete("deprecated 3.15 beta2. Removed in 3.17. Use {@link org.apache.poi.ss.util.CellUtil} instead.")]
public class HSSFCellUtil
{
public const string ALIGNMENT = "alignment";
public const string BORDER_BOTTOM = "borderBottom";
public const string BORDER_LEFT = "borderLeft";
public const string BORDER_RIGHT = "borderRight";
public const string BORDER_TOP = "borderTop";
public const string BOTTOM_BORDER_COLOR = "bottomBorderColor";
public const string DATA_FORMAT = "dataFormat";
public const string FILL_BACKGROUND_COLOR = "fillBackgroundColor";
public const string FILL_FOREGROUND_COLOR = "fillForegroundColor";
public const string FILL_PATTERN = "fillPattern";
public const string FONT = "font";
public const string HIDDEN = "hidden";
public const string INDENTION = "indention";
public const string LEFT_BORDER_COLOR = "leftBorderColor";
public const string LOCKED = "locked";
public const string RIGHT_BORDER_COLOR = "rightBorderColor";
public const string ROTATION = "rotation";
public const string TOP_BORDER_COLOR = "topBorderColor";
public const string VERTICAL_ALIGNMENT = "verticalAlignment";
public const string WRAP_TEXT = "wrapText";
private static UnicodeMapping[] unicodeMappings;
static HSSFCellUtil()
{
unicodeMappings = new UnicodeMapping[15];
unicodeMappings[0] = um("alpha", "\u03B1");
unicodeMappings[1] = um("beta", "\u03B2");
unicodeMappings[2] = um("gamma", "\u03B3");
unicodeMappings[3] = um("delta", "\u03B4");
unicodeMappings[4] = um("epsilon", "\u03B5");
unicodeMappings[5] = um("zeta", "\u03B6");
unicodeMappings[6] = um("eta", "\u03B7");
unicodeMappings[7] = um("theta", "\u03B8");
unicodeMappings[8] = um("iota", "\u03B9");
unicodeMappings[9] = um("kappa", "\u03BA");
unicodeMappings[10] = um("lambda", "\u03BB");
unicodeMappings[11] = um("mu", "\u03BC");
unicodeMappings[12] = um("nu", "\u03BD");
unicodeMappings[13] = um("xi", "\u03BE");
unicodeMappings[14] = um("omicron", "\u03BF");
}
private class UnicodeMapping
{
public String entityName;
public String resolvedValue;
public UnicodeMapping(String pEntityName, String pResolvedValue)
{
entityName = "&" + pEntityName + ";";
resolvedValue = pResolvedValue;
}
}
private HSSFCellUtil()
{
// no instances of this class
@ -104,15 +49,9 @@ namespace NPOI.HSSF.Util
/// <param name="sheet">The sheet that the row is part of.</param>
/// <returns>The row indicated by the rowCounter</returns>
[Obsolete("@deprecated 3.15 beta2. Removed in 3.17. Use {@link org.apache.poi.ss.util.CellUtil#getRow} instead.")]
public static IRow GetRow(int rowCounter, HSSFSheet sheet)
public static IRow GetRow(int rowIndex, HSSFSheet sheet)
{
IRow row = sheet.GetRow(rowCounter);
if (row == null)
{
row = sheet.CreateRow(rowCounter);
}
return row;
return (HSSFRow)CellUtil.GetRow(rowIndex, sheet);
}
@ -123,15 +62,9 @@ namespace NPOI.HSSF.Util
/// <param name="column">The column index that the cell is in.</param>
/// <returns>The cell indicated by the column.</returns>
[Obsolete("@deprecated 3.15 beta2. Removed in 3.17. Use {@link org.apache.poi.ss.util.CellUtil#getCell} instead.")]
public static ICell GetCell(IRow row, int column)
public static ICell GetCell(IRow row, int columnIndex)
{
ICell cell = row.GetCell(column);
if (cell == null)
{
cell = row.CreateCell(column);
}
return cell;
return (HSSFCell)CellUtil.GetCell(row, columnIndex);
}
@ -146,15 +79,7 @@ namespace NPOI.HSSF.Util
[Obsolete("@deprecated 3.15 beta2. Removed in 3.17. Use {@link org.apache.poi.ss.util.CellUtil#createCell} instead.")]
public static ICell CreateCell(IRow row, int column, String value, HSSFCellStyle style)
{
ICell cell = GetCell(row, column);
cell.SetCellValue(new HSSFRichTextString(value));
if (style != null)
{
cell.CellStyle = (style);
}
return cell;
return (HSSFCell)CellUtil.CreateCell(row, column, value, style);
}
@ -171,143 +96,6 @@ namespace NPOI.HSSF.Util
return CreateCell(row, column, value, null);
}
/// <summary>
/// Translate color palette entries from the source to the destination sheet
/// </summary>
private static void RemapCellStyle(HSSFCellStyle stylish, Dictionary<short, short> paletteMap)
{
if (paletteMap.ContainsKey(stylish.BorderDiagonalColor))
{
stylish.BorderDiagonalColor = paletteMap[stylish.BorderDiagonalColor];
}
if (paletteMap.ContainsKey(stylish.BottomBorderColor))
{
stylish.BottomBorderColor = paletteMap[stylish.BottomBorderColor];
}
if (paletteMap.ContainsKey(stylish.FillBackgroundColor))
{
stylish.FillBackgroundColor = paletteMap[stylish.FillBackgroundColor];
}
if (paletteMap.ContainsKey(stylish.FillForegroundColor))
{
stylish.FillForegroundColor = paletteMap[stylish.FillForegroundColor];
}
if (paletteMap.ContainsKey(stylish.LeftBorderColor))
{
stylish.LeftBorderColor = paletteMap[stylish.LeftBorderColor];
}
if (paletteMap.ContainsKey(stylish.RightBorderColor))
{
stylish.RightBorderColor = paletteMap[stylish.RightBorderColor];
}
if (paletteMap.ContainsKey(stylish.TopBorderColor))
{
stylish.TopBorderColor = paletteMap[stylish.TopBorderColor];
}
}
[Obsolete("@deprecated 3.15 beta2. Removed in 3.17. Use {@link org.apache.poi.ss.util.CellUtil#createCell} instead.")]
public static void CopyCell(HSSFCell oldCell, HSSFCell newCell, IDictionary<Int32, HSSFCellStyle> styleMap, Dictionary<short, short> paletteMap, Boolean keepFormulas)
{
if (styleMap != null)
{
if (oldCell.CellStyle != null)
{
if (oldCell.Sheet.Workbook == newCell.Sheet.Workbook)
{
newCell.CellStyle = oldCell.CellStyle;
}
else
{
int styleHashCode = oldCell.CellStyle.GetHashCode();
if (styleMap.ContainsKey(styleHashCode))
{
newCell.CellStyle = styleMap[styleHashCode];
}
else
{
HSSFCellStyle newCellStyle = (HSSFCellStyle)newCell.Sheet.Workbook.CreateCellStyle();
newCellStyle.CloneStyleFrom(oldCell.CellStyle);
RemapCellStyle(newCellStyle, paletteMap); //Clone copies as-is, we need to remap colors manually
newCell.CellStyle = newCellStyle;
//Clone of cell style always clones the font. This makes my life easier
IFont theFont = newCellStyle.GetFont(newCell.Sheet.Workbook);
if (theFont.Color > 0 && paletteMap.ContainsKey(theFont.Color))
{
theFont.Color = paletteMap[theFont.Color]; //Remap font color
}
styleMap.Add(styleHashCode, newCellStyle);
}
}
}
else
{
newCell.CellStyle = null;
}
}
switch (oldCell.CellType)
{
case CellType.String:
HSSFRichTextString rts= oldCell.RichStringCellValue as HSSFRichTextString;
newCell.SetCellValue(rts);
if(rts!=null)
{
for (int j = 0; j < rts.NumFormattingRuns; j++)
{
short fontIndex = rts.GetFontOfFormattingRun(j);
int startIndex = rts.GetIndexOfFormattingRun(j);
int endIndex = 0;
if (j + 1 == rts.NumFormattingRuns)
{
endIndex = rts.Length;
}
else
{
endIndex = rts.GetIndexOfFormattingRun(j+1);
}
FontRecord fr = newCell.BoundWorkbook.CreateNewFont();
fr.CloneStyleFrom(oldCell.BoundWorkbook.GetFontRecordAt(fontIndex));
HSSFFont font = new HSSFFont((short)(newCell.BoundWorkbook.GetFontIndex(fr)), fr);
newCell.RichStringCellValue.ApplyFont(startIndex,endIndex, font);
}
}
break;
case CellType.Numeric:
newCell.SetCellValue(oldCell.NumericCellValue);
break;
case CellType.Blank:
newCell.SetCellType(CellType.Blank);
break;
case CellType.Boolean:
newCell.SetCellValue(oldCell.BooleanCellValue);
break;
case CellType.Error:
newCell.SetCellValue(oldCell.ErrorCellValue);
break;
case CellType.Formula:
if (keepFormulas)
{
newCell.SetCellType(CellType.Formula);
newCell.CellFormula = oldCell.CellFormula;
}
else
{
try
{
newCell.SetCellType(CellType.Numeric);
newCell.SetCellValue(oldCell.NumericCellValue);
}
catch (Exception)
{
newCell.SetCellType(CellType.String);
newCell.SetCellValue(oldCell.ToString());
}
}
break;
default:
break;
}
}
/// <summary>
/// Take a cell, and align it.
/// </summary>
@ -317,7 +105,7 @@ namespace NPOI.HSSF.Util
[Obsolete("@deprecated 3.15 beta2. Removed in 3.17. Use {@link org.apache.poi.ss.util.CellUtil#setAlignment} instead.")]
public static void SetAlignment(ICell cell, HSSFWorkbook workbook, short align)
{
SetCellStyleProperty(cell, workbook, ALIGNMENT, align);
CellUtil.SetAlignment(cell, (HorizontalAlignment)align);
}
/// <summary>
@ -329,29 +117,9 @@ namespace NPOI.HSSF.Util
[Obsolete("@deprecated 3.15 beta2. Removed in 3.17. Use {@link org.apache.poi.ss.util.CellUtil#setFont} instead.")]
public static void SetFont(ICell cell, HSSFWorkbook workbook, HSSFFont font)
{
SetCellStyleProperty(cell, workbook, FONT, font);
CellUtil.SetFont(cell, font);
}
private static bool CompareHashTableKeyValueIsEqual(Hashtable a, Hashtable b)
{
foreach (DictionaryEntry a_entry in a)
{
foreach (DictionaryEntry b_entry in b)
{
if (a_entry.Key.ToString() == b_entry.Key.ToString())
{
if ((a_entry.Value is short && b_entry.Value is short
&& (short)a_entry.Value != (short)b_entry.Value) ||
(a_entry.Value is bool && b_entry.Value is bool
&& (bool)a_entry.Value != (bool)b_entry.Value))
{
return false;
}
}
}
}
return true;
}
/**
* This method attempt to find an already existing HSSFCellStyle that matches
* what you want the style to be. If it does not find the style, then it
@ -370,159 +138,7 @@ namespace NPOI.HSSF.Util
[Obsolete("@deprecated 3.15 beta2. Removed in 3.17. Use {@link org.apache.poi.ss.util.CellUtil#setCellStyleProperty} instead.")]
public static void SetCellStyleProperty(ICell cell, HSSFWorkbook workbook, String propertyName, Object propertyValue)
{
ICellStyle originalStyle = cell.CellStyle;
ICellStyle newStyle = null;
Hashtable values = GetFormatProperties(originalStyle);
values[propertyName] = propertyValue;
// index seems like what index the cellstyle is in the list of styles for a workbook.
// not good to compare on!
int numberCellStyles = workbook.NumCellStyles;
for (int i = 0; i < numberCellStyles; i++)
{
ICellStyle wbStyle = workbook.GetCellStyleAt(i);
Hashtable wbStyleMap = GetFormatProperties(wbStyle);
// if (wbStyleMap.Equals(values))
if (CompareHashTableKeyValueIsEqual(wbStyleMap, values))
{
newStyle = wbStyle;
break;
}
}
if (newStyle == null)
{
newStyle = workbook.CreateCellStyle();
SetFormatProperties(newStyle, workbook, values);
}
cell.CellStyle = (newStyle);
}
/// <summary>
/// Returns a map containing the format properties of the given cell style.
/// </summary>
/// <param name="style">cell style</param>
/// <returns>map of format properties (String -&gt; Object)</returns>
private static Hashtable GetFormatProperties(ICellStyle style)
{
Hashtable properties = new Hashtable();
PutShort(properties, ALIGNMENT, (short)style.Alignment);
PutShort(properties, BORDER_BOTTOM, (short)style.BorderBottom);
PutShort(properties, BORDER_LEFT, (short)style.BorderLeft);
PutShort(properties, BORDER_RIGHT, (short)style.BorderRight);
PutShort(properties, BORDER_TOP, (short)style.BorderTop);
PutShort(properties, BOTTOM_BORDER_COLOR, style.BottomBorderColor);
PutShort(properties, DATA_FORMAT, style.DataFormat);
PutShort(properties, FILL_BACKGROUND_COLOR, style.FillBackgroundColor);
PutShort(properties, FILL_FOREGROUND_COLOR, style.FillForegroundColor);
PutShort(properties, FILL_PATTERN, (short)style.FillPattern);
PutShort(properties, FONT, style.FontIndex);
PutBoolean(properties, HIDDEN, style.IsHidden);
PutShort(properties, INDENTION, style.Indention);
PutShort(properties, LEFT_BORDER_COLOR, style.LeftBorderColor);
PutBoolean(properties, LOCKED, style.IsLocked);
PutShort(properties, RIGHT_BORDER_COLOR, style.RightBorderColor);
PutShort(properties, ROTATION, style.Rotation);
PutShort(properties, TOP_BORDER_COLOR, style.TopBorderColor);
PutShort(properties, VERTICAL_ALIGNMENT, (short)style.VerticalAlignment);
PutBoolean(properties, WRAP_TEXT, style.WrapText);
return properties;
}
/// <summary>
/// Sets the format properties of the given style based on the given map.
/// </summary>
/// <param name="style">The cell style</param>
/// <param name="workbook">The parent workbook.</param>
/// <param name="properties">The map of format properties (String -&gt; Object).</param>
private static void SetFormatProperties(
ICellStyle style, HSSFWorkbook workbook, Hashtable properties)
{
style.Alignment = (HorizontalAlignment)GetShort(properties, ALIGNMENT);
style.BorderBottom = (BorderStyle)GetShort(properties, BORDER_BOTTOM);
style.BorderLeft = (BorderStyle)GetShort(properties, BORDER_LEFT);
style.BorderRight = (BorderStyle)GetShort(properties, BORDER_RIGHT);
style.BorderTop = (BorderStyle)GetShort(properties, BORDER_TOP);
style.BottomBorderColor = (GetShort(properties, BOTTOM_BORDER_COLOR));
style.DataFormat = (GetShort(properties, DATA_FORMAT));
style.FillBackgroundColor = (GetShort(properties, FILL_BACKGROUND_COLOR));
style.FillForegroundColor = (GetShort(properties, FILL_FOREGROUND_COLOR));
style.FillPattern = (FillPattern)GetShort(properties, FILL_PATTERN);
style.SetFont(workbook.GetFontAt(GetShort(properties, FONT)));
style.IsHidden = (GetBoolean(properties, HIDDEN));
style.Indention = (GetShort(properties, INDENTION));
style.LeftBorderColor = (GetShort(properties, LEFT_BORDER_COLOR));
style.IsLocked = (GetBoolean(properties, LOCKED));
style.RightBorderColor = (GetShort(properties, RIGHT_BORDER_COLOR));
style.Rotation = (GetShort(properties, ROTATION));
style.TopBorderColor = (GetShort(properties, TOP_BORDER_COLOR));
style.VerticalAlignment = (VerticalAlignment)GetShort(properties, VERTICAL_ALIGNMENT);
style.WrapText = (GetBoolean(properties, WRAP_TEXT));
}
/// <summary>
/// Utility method that returns the named short value form the given map.
/// Returns zero if the property does not exist, or is not a {@link Short}.
/// </summary>
/// <param name="properties">The map of named properties (String -&gt; Object)</param>
/// <param name="name">The property name.</param>
/// <returns>property value, or zero</returns>
private static short GetShort(Hashtable properties, String name)
{
Object value = properties[name];
if (value is short)
{
return (short)value;
}
else
{
return 0;
}
}
/// <summary>
/// Utility method that returns the named boolean value form the given map.
/// Returns false if the property does not exist, or is not a {@link Boolean}.
/// </summary>
/// <param name="properties">map of properties (String -&gt; Object)</param>
/// <param name="name">The property name.</param>
/// <returns>property value, or false</returns>
private static bool GetBoolean(Hashtable properties, String name)
{
Object value = properties[name];
if (value is Boolean)
{
return ((Boolean)value);
}
else
{
return false;
}
}
/// <summary>
/// Utility method that Puts the named short value to the given map.
/// </summary>
/// <param name="properties">The map of properties (String -&gt; Object).</param>
/// <param name="name">The property name.</param>
/// <param name="value">The property value.</param>
private static void PutShort(Hashtable properties, String name, short value)
{
properties[name] = value;
}
/// <summary>
/// Utility method that Puts the named boolean value to the given map.
/// </summary>
/// <param name="properties">map of properties (String -&gt; Object)</param>
/// <param name="name">property name</param>
/// <param name="value">property value</param>
private static void PutBoolean(Hashtable properties, String name, bool value)
{
properties[name] = value;
CellUtil.SetCellStyleProperty(cell, propertyName, propertyValue);
}
/// <summary>
@ -534,31 +150,8 @@ namespace NPOI.HSSF.Util
[Obsolete("@deprecated 3.15 beta2. Removed in 3.17. Use {@link org.apache.poi.ss.util.CellUtil#translateUnicodeValues} instead.")]
public static ICell TranslateUnicodeValues(ICell cell)
{
String s = cell.RichStringCellValue.String;
bool foundUnicode = false;
String lowerCaseStr = s.ToLower();
for (int i = 0; i < unicodeMappings.Length; i++)
{
UnicodeMapping entry = unicodeMappings[i];
String key = entry.entityName;
if (lowerCaseStr.IndexOf(key, StringComparison.Ordinal) != -1)
{
s = s.Replace(key, entry.resolvedValue);
foundUnicode = true;
}
}
if (foundUnicode)
{
cell.SetCellValue(new HSSFRichTextString(s));
}
CellUtil.TranslateUnicodeValues(cell);
return cell;
}
private static UnicodeMapping um(String entityName, String resolvedValue)
{
return new UnicodeMapping(entityName, resolvedValue);
}
}
}

16
main/HSSF/Util/HSSFRegionUtil.cs

@ -71,7 +71,7 @@ namespace NPOI.HSSF.Util
int rowEnd = region.LastRow;
int column = region.FirstColumn;
CellPropertySetter cps = new CellPropertySetter(workbook, HSSFCellUtil.BORDER_LEFT, (int)border);
CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.BORDER_LEFT, (int)border);
for (int i = rowStart; i <= rowEnd; i++)
{
cps.SetProperty(HSSFCellUtil.GetRow(i, sheet), column);
@ -92,7 +92,7 @@ namespace NPOI.HSSF.Util
int rowEnd = region.LastRow;
int column = region.FirstColumn;
CellPropertySetter cps = new CellPropertySetter(workbook, HSSFCellUtil.LEFT_BORDER_COLOR, color);
CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.LEFT_BORDER_COLOR, color);
for (int i = rowStart; i <= rowEnd; i++)
{
cps.SetProperty(HSSFCellUtil.GetRow(i, sheet), column);
@ -113,7 +113,7 @@ namespace NPOI.HSSF.Util
int rowEnd = region.LastRow;
int column = region.LastColumn;
CellPropertySetter cps = new CellPropertySetter(workbook, HSSFCellUtil.BORDER_RIGHT, (int)border);
CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.BORDER_RIGHT, (int)border);
for (int i = rowStart; i <= rowEnd; i++)
{
cps.SetProperty(HSSFCellUtil.GetRow(i, sheet), column);
@ -135,7 +135,7 @@ namespace NPOI.HSSF.Util
int rowEnd = region.LastRow;
int column = region.LastColumn;
CellPropertySetter cps = new CellPropertySetter(workbook, HSSFCellUtil.RIGHT_BORDER_COLOR, color);
CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.RIGHT_BORDER_COLOR, color);
for (int i = rowStart; i <= rowEnd; i++)
{
cps.SetProperty(HSSFCellUtil.GetRow(i, sheet), column);
@ -155,7 +155,7 @@ namespace NPOI.HSSF.Util
int colStart = region.FirstColumn;
int colEnd = region.LastColumn;
int rowIndex = region.LastRow;
CellPropertySetter cps = new CellPropertySetter(workbook, HSSFCellUtil.BORDER_BOTTOM, (int)border);
CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.BORDER_BOTTOM, (int)border);
NPOI.SS.UserModel.IRow row = HSSFCellUtil.GetRow(rowIndex, sheet);
for (int i = colStart; i <= colEnd; i++)
{
@ -177,7 +177,7 @@ namespace NPOI.HSSF.Util
int colStart = region.FirstColumn;
int colEnd = region.LastColumn;
int rowIndex = region.LastRow;
CellPropertySetter cps = new CellPropertySetter(workbook, HSSFCellUtil.BOTTOM_BORDER_COLOR, color);
CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.BOTTOM_BORDER_COLOR, color);
NPOI.SS.UserModel.IRow row = HSSFCellUtil.GetRow(rowIndex, sheet);
for (int i = colStart; i <= colEnd; i++)
{
@ -199,7 +199,7 @@ namespace NPOI.HSSF.Util
int colStart = region.FirstColumn;
int colEnd = region.LastColumn;
int rowIndex = region.FirstRow;
CellPropertySetter cps = new CellPropertySetter(workbook, HSSFCellUtil.BORDER_TOP, (int)border);
CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.BORDER_TOP, (int)border);
NPOI.SS.UserModel.IRow row = HSSFCellUtil.GetRow(rowIndex, sheet);
for (int i = colStart; i <= colEnd; i++)
{
@ -220,7 +220,7 @@ namespace NPOI.HSSF.Util
int colStart = region.FirstColumn;
int colEnd = region.LastColumn;
int rowIndex = region.FirstRow;
CellPropertySetter cps = new CellPropertySetter(workbook, HSSFCellUtil.TOP_BORDER_COLOR, color);
CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.TOP_BORDER_COLOR, color);
NPOI.SS.UserModel.IRow row = HSSFCellUtil.GetRow(rowIndex, sheet);
for (int i = colStart; i <= colEnd; i++)
{

406
main/SS/Util/CellUtil.cs

@ -20,6 +20,7 @@ namespace NPOI.SS.Util
using System;
using System.Collections.Generic;
using NPOI.SS.UserModel;
using NPOI.Util;
/**
@ -59,6 +60,33 @@ namespace NPOI.SS.Util
public const string VERTICAL_ALIGNMENT = "verticalAlignment";
public const string WRAP_TEXT = "wrapText";
private static ISet<String> shortValues = new HashSet<string>(new string[]{
BOTTOM_BORDER_COLOR,
LEFT_BORDER_COLOR,
RIGHT_BORDER_COLOR,
TOP_BORDER_COLOR,
FILL_FOREGROUND_COLOR,
FILL_BACKGROUND_COLOR,
INDENTION,
DATA_FORMAT,
ROTATION
});
private static ISet<String> intValues = new HashSet<string>(new string[]{
FONT
});
private static ISet<String> booleanValues = new HashSet<string>(new string[]{
LOCKED,
HIDDEN,
WRAP_TEXT
});
private static ISet<String> borderTypeValues = new HashSet<string>(new string[]{
BORDER_BOTTOM,
BORDER_LEFT,
BORDER_RIGHT,
BORDER_TOP
});
private static UnicodeMapping[] unicodeMappings;
private class UnicodeMapping
@ -225,11 +253,30 @@ namespace NPOI.SS.Util
*
* @see CellStyle for alignment options
*/
[Obsolete("deprecated 3.15-beta2. Use {@link #SetAlignment(ICell, HorizontalAlignment)} instead.")]
public static void SetAlignment(ICell cell, IWorkbook workbook, short align)
{
SetCellStyleProperty(cell, workbook, ALIGNMENT, align);
}
/**
* Take a cell, and align it.
*
* This is superior to cell.getCellStyle().setAlignment(align) because
* this method will not modify the CellStyle object that may be referenced
* by multiple cells. Instead, this method will search for existing CellStyles
* that match the desired CellStyle, creating a new CellStyle with the desired
* style if no match exists.
*
* @param cell the cell to set the alignment for
* @param align the horizontal alignment to use.
*
* @see HorizontalAlignment for alignment options
* @since POI 3.15 beta 3
*/
public static void SetAlignment(ICell cell, HorizontalAlignment align)
{
SetCellStyleProperty(cell, ALIGNMENT, align);
}
/**
* Take a cell, and apply a font to it
*
@ -237,6 +284,7 @@ namespace NPOI.SS.Util
*@param workbook The workbook that is being worked with.
*@param font The Font that you want to set...
*/
[Obsolete("deprecated 3.15-beta2. Use {@link #SetFont(ICell, IFont)} instead.")]
public static void SetFont(ICell cell, IWorkbook workbook, IFont font)
{
// Check if font belongs to workbook
@ -251,17 +299,53 @@ namespace NPOI.SS.Util
SetCellStyleProperty(cell, workbook, FONT, fontIndex);
}
/**
* Take a cell, and apply a font to it
*
* @param cell the cell to set the alignment for
* @param font The Font that you want to set.
* @throws IllegalArgumentException if <tt>font</tt> and <tt>cell</tt> do not belong to the same workbook
*/
public static void SetFont(ICell cell, IFont font)
{
// Check if font belongs to workbook
IWorkbook wb = cell.Sheet.Workbook;
short fontIndex = font.Index;
if (!wb.GetFontAt(fontIndex).Equals(font))
{
throw new ArgumentException("Font does not belong to this workbook");
}
// Check if cell belongs to workbook
// (checked in setCellStyleProperty)
SetCellStyleProperty(cell, FONT, fontIndex);
}
/**
* This method attempt to find an already existing CellStyle that matches what you want the
* style to be. If it does not find the style, then it creates a new one. If it does create a
* new one, then it applies the propertyName and propertyValue to the style. This is necessary
* because Excel has an upper limit on the number of Styles that it supports.
* <p>This method attempts to find an existing CellStyle that matches the <code>cell</code>'s
* current style plus styles properties in <code>properties</code>. A new style is created if the
* workbook does not contain a matching style.</p>
*
* <p>Modifies the cell style of <code>cell</code> without affecting other cells that use the
* same style.</p>
*
* <p>This is necessary because Excel has an upper limit on the number of styles that it supports.</p>
*
* <p>This function is more efficient than multiple calls to
* {@link #setCellStyleProperty(org.apache.poi.ss.usermodel.Cell, org.apache.poi.ss.usermodel.Workbook, String, Object)}
* if adding multiple cell styles.</p>
*
* <p>For performance reasons, if this is the only cell in a workbook that uses a cell style,
* this method does NOT remove the old style from the workbook.
* <!-- NOT IMPLEMENTED: Unused styles should be
* pruned from the workbook with [@link #removeUnusedCellStyles(Workbook)] or
* [@link #removeStyleFromWorkbookIfUnused(CellStyle, Workbook)]. -->
* </p>
*
*@param workbook The workbook that is being worked with.
*@param propertyName The name of the property that is to be changed.
*@param propertyValue The value of the property that is to be changed.
*@param cell The cell that needs it's style changes
* @param cell The cell to change the style of
* @param properties The properties to be added to a cell style, as {propertyName: propertyValue}.
* @since POI 3.14 beta 2
*/
public static void SetCellStyleProperties(ICell cell, Dictionary<String, Object> properties)
{
@ -269,14 +353,7 @@ namespace NPOI.SS.Util
ICellStyle originalStyle = cell.CellStyle;
ICellStyle newStyle = null;
Dictionary<String, Object> values = GetFormatProperties(originalStyle);
foreach(KeyValuePair<string, object> kv in properties)
{
if (values.ContainsKey(kv.Key))
values[kv.Key] = kv.Value;
else
values.Add(kv.Key, kv.Value);
}
PutAll(properties, values);
// index seems like what index the cellstyle is in the list of styles for a workbook.
// not good to compare on!
@ -285,34 +362,16 @@ namespace NPOI.SS.Util
for (int i = 0; i < numberCellStyles; i++)
{
ICellStyle wbStyle = workbook.GetCellStyleAt(i);
Dictionary<String, Object> wbStyleMap = GetFormatProperties(wbStyle);
if (values.Keys.Count != wbStyleMap.Keys.Count)
continue;
// the desired style already exists in the workbook. Use the existing style.
//if(wbStyleMap.equals(values))
bool found = true;
foreach (string key in values.Keys)
{
if (!wbStyleMap.ContainsKey(key))
{
found = false;
break;
}
if (values[key].Equals(wbStyleMap[key])) continue;
found = false;
break;
}
if (found)
if (wbStyleMap.Equals(values))
{
newStyle = wbStyle;
break;
}
}
// the desired style does not exist in the workbook. Create a new style with desired properties.
if (newStyle == null)
{
@ -323,6 +382,28 @@ namespace NPOI.SS.Util
cell.CellStyle = newStyle;
}
/**
* <p>This method attempts to find an existing CellStyle that matches the <code>cell</code>'s
* current style plus a single style property <code>propertyName</code> with value
* <code>propertyValue</code>.
* A new style is created if the workbook does not contain a matching style.</p>
*
* <p>Modifies the cell style of <code>cell</code> without affecting other cells that use the
* same style.</p>
*
* <p>If setting more than one cell style property on a cell, use
* {@link #setCellStyleProperties(org.apache.poi.ss.usermodel.Cell, Map)},
* which is faster and does not add unnecessary intermediate CellStyles to the workbook.</p>
*
* @param cell The cell that is to be changed.
* @param propertyName The name of the property that is to be changed.
* @param propertyValue The value of the property that is to be changed.
*/
public static void SetCellStyleProperty(ICell cell, String propertyName, Object propertyValue)
{
Dictionary<String, Object> values = new Dictionary<string, object>() { { propertyName, propertyValue } };
SetCellStyleProperties(cell, values);
}
/**
* <p>This method attempts to find an existing CellStyle that matches the <code>cell</code>'s
* current style plus a single style property <code>propertyName</code> with value
@ -341,6 +422,7 @@ namespace NPOI.SS.Util
* @param propertyValue The value of the property that is to be changed.
* @param cell The cell that needs it's style changes
*/
[Obsolete("deprecated 3.15-beta2. Use {@link #setCellStyleProperty(Cell, String, Object)} instead.")]
public static void SetCellStyleProperty(ICell cell, IWorkbook workbook, String propertyName,
Object propertyValue)
{
@ -349,9 +431,60 @@ namespace NPOI.SS.Util
throw new ArgumentException("Cannot set cell style property. Cell does not belong to workbook.");
}
Dictionary<String, Object> values = new Dictionary<string, object>() { {propertyName, propertyValue} };
Dictionary<String, Object> values = new Dictionary<string, object>() { { propertyName, propertyValue } };
SetCellStyleProperties(cell, values);
}
/**
* Copies the entries in src to dest, using the preferential data type
* so that maps can be compared for equality
*
* @param src the property map to copy from (read-only)
* @param dest the property map to copy into
* @since POI 3.15 beta 3
*/
private static void PutAll(Dictionary<String, Object> src, Dictionary<String, Object> dest)
{
foreach (String key in src.Keys)
{
if (shortValues.Contains(key))
{
dest.Add(key, GetShort(src, key));
}
else if (intValues.Contains(key))
{
dest.Add(key, GetInt(src, key));
}
else if (booleanValues.Contains(key))
{
dest.Add(key, GetBoolean(src, key));
}
else if (borderTypeValues.Contains(key))
{
dest.Add(key, GetBorderStyle(src, key));
}
else if (ALIGNMENT.Equals(key))
{
dest.Add(key, GetHorizontalAlignment(src, key));
}
else if (VERTICAL_ALIGNMENT.Equals(key))
{
dest.Add(key, GetVerticalAlignment(src, key));
}
else if (FILL_PATTERN.Equals(key))
{
dest.Add(key, GetFillPattern(src, key));
}
else
{
//if (log.check(POILogger.INFO))
//{
// log.log(POILogger.INFO, "Ignoring unrecognized CellUtil format properties key: " + key);
//}
}
}
}
/**
* Returns a map containing the format properties of the given cell style.
* The returned map is not tied to <code>style</code>, so subsequent changes
@ -406,7 +539,7 @@ namespace NPOI.SS.Util
style.BorderRight = GetBorderStyle(properties, BORDER_RIGHT);
style.BorderTop = GetBorderStyle(properties, BORDER_TOP);
style.BottomBorderColor = GetShort(properties, BOTTOM_BORDER_COLOR);
style.DataFormat =GetShort(properties, DATA_FORMAT);
style.DataFormat = GetShort(properties, DATA_FORMAT);
style.FillBackgroundColor = GetShort(properties, FILL_BACKGROUND_COLOR);
style.FillForegroundColor = GetShort(properties, FILL_FOREGROUND_COLOR);
style.FillPattern = (FillPattern)GetShort(properties, FILL_PATTERN);
@ -438,6 +571,25 @@ namespace NPOI.SS.Util
return result;
return 0;
}
/**
* Utility method that returns the named int value from the given map.
*
* @param properties map of named properties (String -> Object)
* @param name property name
* @return zero if the property does not exist, or is not a {@link Integer}
* otherwise the property value
*/
private static int GetInt(Dictionary<String, Object> properties, String name)
{
Object value = properties[name];
if (Number.IsNumber(value))
{
return (int)value;
}
return 0;
}
/**
* Utility method that returns the named BorderStyle value form the given map.
*
@ -447,10 +599,148 @@ namespace NPOI.SS.Util
*/
private static BorderStyle GetBorderStyle(Dictionary<String, Object> properties, String name)
{
BorderStyle value = (BorderStyle)properties[name];
return value;
//return (value != null) ? value : BorderStyle.NONE;
Object value = properties[name];
BorderStyle border;
if (value is BorderStyle)
{
border = (BorderStyle)value;
}
// @deprecated 3.15 beta 2. getBorderStyle will only work on BorderStyle enums instead of codes in the future.
else if (value is short)
{
//if (log.check(POILogger.WARN))
//{
// log.log(POILogger.WARN, "Deprecation warning: CellUtil properties map uses Short values for "
// + name + ". Should use BorderStyle enums instead.");
//}
short code = ((short)value);
border = (BorderStyle)code;
}
else if (value == null)
{
border = BorderStyle.None;
}
else
{
throw new RuntimeException("Unexpected border style class. Must be BorderStyle or Short (deprecated).");
}
return border;
}
/**
* Utility method that returns the named FillPatternType value from the given map.
*
* @param properties map of named properties (String -> Object)
* @param name property name
* @return FillPatternType style if set, otherwise {@link FillPatternType#NO_FILL}
* @since POI 3.15 beta 3
*/
private static FillPattern GetFillPattern(Dictionary<String, Object> properties, String name)
{
Object value = properties[name];
FillPattern pattern;
if (value is FillPattern)
{
pattern = (FillPattern)value;
}
// @deprecated 3.15 beta 2. getFillPattern will only work on FillPatternType enums instead of codes in the future.
else if (value is short)
{
//if (log.check(POILogger.WARN))
//{
// log.log(POILogger.WARN, "Deprecation warning: CellUtil properties map uses Short values for "
// + name + ". Should use FillPatternType enums instead.");
//}
short code = (short)value;
pattern = (FillPattern)code;
}
else if (value == null)
{
pattern = FillPattern.NoFill;
}
else
{
throw new RuntimeException("Unexpected fill pattern style class. Must be FillPatternType or Short (deprecated).");
}
return pattern;
}
/**
* Utility method that returns the named HorizontalAlignment value from the given map.
*
* @param properties map of named properties (String -> Object)
* @param name property name
* @return HorizontalAlignment style if set, otherwise {@link HorizontalAlignment#GENERAL}
* @since POI 3.15 beta 3
*/
private static HorizontalAlignment GetHorizontalAlignment(Dictionary<String, Object> properties, String name)
{
Object value = properties[name];
HorizontalAlignment align;
if (value is HorizontalAlignment)
{
align = (HorizontalAlignment)value;
}
// @deprecated 3.15 beta 2. getHorizontalAlignment will only work on HorizontalAlignment enums instead of codes in the future.
else if (value is short)
{
//if (log.check(POILogger.WARN))
//{
// log.log(POILogger.WARN, "Deprecation warning: CellUtil properties map used a Short value for "
// + name + ". Should use HorizontalAlignment enums instead.");
//}
short code = (short)value;
align = (HorizontalAlignment)code;
}
else if (value == null)
{
align = HorizontalAlignment.General;
}
else
{
throw new RuntimeException("Unexpected horizontal alignment style class. Must be HorizontalAlignment or Short (deprecated).");
}
return align;
}
/**
* Utility method that returns the named VerticalAlignment value from the given map.
*
* @param properties map of named properties (String -> Object)
* @param name property name
* @return VerticalAlignment style if set, otherwise {@link VerticalAlignment#BOTTOM}
* @since POI 3.15 beta 3
*/
private static VerticalAlignment GetVerticalAlignment(Dictionary<String, Object> properties, String name)
{
Object value = properties[name];
VerticalAlignment align;
if (value is VerticalAlignment)
{
align = (VerticalAlignment)value;
}
// @deprecated 3.15 beta 2. getVerticalAlignment will only work on VerticalAlignment enums instead of codes in the future.
else if (value is short)
{
//if (log.check(POILogger.WARN))
//{
// log.log(POILogger.WARN, "Deprecation warning: CellUtil properties map used a Short value for "
// + name + ". Should use VerticalAlignment enums instead.");
//}
short code = (short)value;
align = (VerticalAlignment)code;
}
else if (value == null)
{
align = VerticalAlignment.Bottom;
}
else
{
throw new RuntimeException("Unexpected vertical alignment style class. Must be VerticalAlignment or Short (deprecated).");
}
return align;
}
/**
* Utility method that returns the named boolean value form the given map.
*
@ -544,22 +834,22 @@ namespace NPOI.SS.Util
static CellUtil()
{
unicodeMappings = new UnicodeMapping[] {
um("alpha", "\u03B1" ),
um("beta", "\u03B2" ),
um("gamma", "\u03B3" ),
um("delta", "\u03B4" ),
um("epsilon", "\u03B5" ),
um("zeta", "\u03B6" ),
um("eta", "\u03B7" ),
um("theta", "\u03B8" ),
um("iota", "\u03B9" ),
um("kappa", "\u03BA" ),
um("lambda", "\u03BB" ),
um("mu", "\u03BC" ),
um("nu", "\u03BD" ),
um("xi", "\u03BE" ),
um("omicron", "\u03BF" ),
};
um("alpha", "\u03B1" ),
um("beta", "\u03B2" ),
um("gamma", "\u03B3" ),
um("delta", "\u03B4" ),
um("epsilon", "\u03B5" ),
um("zeta", "\u03B6" ),
um("eta", "\u03B7" ),
um("theta", "\u03B8" ),
um("iota", "\u03B9" ),
um("kappa", "\u03BA" ),
um("lambda", "\u03BB" ),
um("mu", "\u03BC" ),
um("nu", "\u03BD" ),
um("xi", "\u03BE" ),
um("omicron", "\u03BF" ),
};
}
private static UnicodeMapping um(String entityName, String resolvedValue)

23
main/SS/Util/RegionUtil.cs

@ -39,15 +39,12 @@ namespace NPOI.SS.Util
*/
private class CellPropertySetter
{
private IWorkbook _workbook;
private String _propertyName;
private short _propertyValue;
public CellPropertySetter(IWorkbook workbook, String propertyName, int value)
public CellPropertySetter(String propertyName, int value)
{
_workbook = workbook;
_propertyName = propertyName;
_propertyValue = (short)value;
}
@ -56,7 +53,7 @@ namespace NPOI.SS.Util
public void SetProperty(IRow row, int column)
{
ICell cell = CellUtil.GetCell(row, column);
CellUtil.SetCellStyleProperty(cell, _workbook, _propertyName, _propertyValue);
CellUtil.SetCellStyleProperty(cell, _propertyName, _propertyValue);
}
}
@ -76,7 +73,7 @@ namespace NPOI.SS.Util
int rowEnd = region.LastRow;
int column = region.FirstColumn;
CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.BORDER_LEFT, border);
CellPropertySetter cps = new CellPropertySetter(CellUtil.BORDER_LEFT, border);
for (int i = rowStart; i <= rowEnd; i++)
{
cps.SetProperty(CellUtil.GetRow(i, sheet), column);
@ -98,7 +95,7 @@ namespace NPOI.SS.Util
int rowEnd = region.LastRow;
int column = region.FirstColumn;
CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.LEFT_BORDER_COLOR,
CellPropertySetter cps = new CellPropertySetter(CellUtil.LEFT_BORDER_COLOR,
color);
for (int i = rowStart; i <= rowEnd; i++)
{
@ -121,7 +118,7 @@ namespace NPOI.SS.Util
int rowEnd = region.LastRow;
int column = region.LastColumn;
CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.BORDER_RIGHT, border);
CellPropertySetter cps = new CellPropertySetter(CellUtil.BORDER_RIGHT, border);
for (int i = rowStart; i <= rowEnd; i++)
{
cps.SetProperty(CellUtil.GetRow(i, sheet), column);
@ -143,7 +140,7 @@ namespace NPOI.SS.Util
int rowEnd = region.LastRow;
int column = region.LastColumn;
CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.RIGHT_BORDER_COLOR,
CellPropertySetter cps = new CellPropertySetter(CellUtil.RIGHT_BORDER_COLOR,
color);
for (int i = rowStart; i <= rowEnd; i++)
{
@ -165,7 +162,7 @@ namespace NPOI.SS.Util
int colStart = region.FirstColumn;
int colEnd = region.LastColumn;
int rowIndex = region.LastRow;
CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.BORDER_BOTTOM, border);
CellPropertySetter cps = new CellPropertySetter(CellUtil.BORDER_BOTTOM, border);
IRow row = CellUtil.GetRow(rowIndex, sheet);
for (int i = colStart; i <= colEnd; i++)
{
@ -187,7 +184,7 @@ namespace NPOI.SS.Util
int colStart = region.FirstColumn;
int colEnd = region.LastColumn;
int rowIndex = region.LastRow;
CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.BOTTOM_BORDER_COLOR,
CellPropertySetter cps = new CellPropertySetter(CellUtil.BOTTOM_BORDER_COLOR,
color);
IRow row = CellUtil.GetRow(rowIndex, sheet);
for (int i = colStart; i <= colEnd; i++)
@ -210,7 +207,7 @@ namespace NPOI.SS.Util
int colStart = region.FirstColumn;
int colEnd = region.LastColumn;
int rowIndex = region.FirstRow;
CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.BORDER_TOP, border);
CellPropertySetter cps = new CellPropertySetter(CellUtil.BORDER_TOP, border);
IRow row = CellUtil.GetRow(rowIndex, sheet);
for (int i = colStart; i <= colEnd; i++)
{
@ -232,7 +229,7 @@ namespace NPOI.SS.Util
int colStart = region.FirstColumn;
int colEnd = region.LastColumn;
int rowIndex = region.FirstRow;
CellPropertySetter cps = new CellPropertySetter(workbook, CellUtil.TOP_BORDER_COLOR, color);
CellPropertySetter cps = new CellPropertySetter(CellUtil.TOP_BORDER_COLOR, color);
IRow row = CellUtil.GetRow(rowIndex, sheet);
for (int i = colStart; i <= colEnd; i++)
{

33
testcases/main/SS/Util/TestCellUtil.cs

@ -41,13 +41,13 @@ namespace NPOI.SS.Util
// Add a border should create a new style
int styCnt1 = wb.NumCellStyles;
CellUtil.SetCellStyleProperty(c, wb, CellUtil.BORDER_BOTTOM, BorderStyle.Thin);
CellUtil.SetCellStyleProperty(c, CellUtil.BORDER_BOTTOM, BorderStyle.Thin);
int styCnt2 = wb.NumCellStyles;
Assert.AreEqual(styCnt1 + 1, styCnt2);
// Add same border to another cell, should not create another style
c = r.CreateCell(1);
CellUtil.SetCellStyleProperty(c, wb, CellUtil.BORDER_BOTTOM, BorderStyle.Thin);
CellUtil.SetCellStyleProperty(c, CellUtil.BORDER_BOTTOM, BorderStyle.Thin);
int styCnt3 = wb.NumCellStyles;
Assert.AreEqual(styCnt2, styCnt3);
@ -71,7 +71,7 @@ namespace NPOI.SS.Util
props.Add(CellUtil.BORDER_RIGHT, BorderStyle.Thin);
CellUtil.SetCellStyleProperties(c, props);
int styCnt2 = wb.NumCellStyles;
Assert.AreEqual(styCnt2, styCnt1 + 1);
Assert.AreEqual(styCnt1 + 1, styCnt2, "Only one additional style should have been created");
// Add same border another to same cell, should not create another style
c = r.CreateCell(1);
@ -174,7 +174,7 @@ namespace NPOI.SS.Util
Assert.AreEqual(HorizontalAlignment.General, B1.CellStyle.Alignment);
// Get/set alignment modifies the cell's style
CellUtil.SetAlignment(A1, wb, (int)HorizontalAlignment.Right);
CellUtil.SetAlignment(A1, HorizontalAlignment.Right);
Assert.AreEqual(HorizontalAlignment.Right, A1.CellStyle.Alignment);
// Get/set alignment doesn't affect the style of cells with
@ -208,7 +208,7 @@ namespace NPOI.SS.Util
Assert.AreEqual(defaultFontIndex, B1.CellStyle.FontIndex);
// Get/set alignment modifies the cell's style
CellUtil.SetFont(A1, wb, font);
CellUtil.SetFont(A1, font);
Assert.AreEqual(customFontIndex, A1.CellStyle.FontIndex);
// Get/set alignment doesn't affect the style of cells with
@ -232,12 +232,12 @@ namespace NPOI.SS.Util
ICell A1 = wb1.CreateSheet().CreateRow(0).CreateCell(0);
// okay
CellUtil.SetFont(A1, wb1, font1);
CellUtil.SetFont(A1, font1);
// font belongs to different workbook
try
{
CellUtil.SetFont(A1, wb1, font2);
CellUtil.SetFont(A1, font2);
Assert.Fail("setFont not allowed if font belongs to a different workbook");
}
catch (ArgumentException e)
@ -251,23 +251,10 @@ namespace NPOI.SS.Util
throw e;
}
}
// cell belongs to different workbook
try
{
CellUtil.SetFont(A1, wb2, font2);
Assert.Fail("setFont not allowed if cell belongs to a different workbook");
}
catch (ArgumentException e)
finally
{
if (e.Message.StartsWith("Cannot Set cell style property. Cell does not belong to workbook."))
{
// expected
}
else
{
throw e;
}
wb1.Close();
wb2.Close();
}
}
}

4
testcases/ooxml/XSSF/UserModel/TestUnfixedBugs.cs

@ -222,9 +222,9 @@ namespace NPOI.XSSF.UserModel
row2.GetCell(0).CellStyle.BorderBottom = BorderStyle.Thin;
row2.GetCell(1).CellStyle.BorderBottom = BorderStyle.Thin;
ICell cell0 = CellUtil.GetCell(row3, 0);
CellUtil.SetCellStyleProperty(cell0, wb, CellUtil.BORDER_BOTTOM, BorderStyle.Thin);
CellUtil.SetCellStyleProperty(cell0, CellUtil.BORDER_BOTTOM, BorderStyle.Thin);
ICell cell1 = CellUtil.GetCell(row3, 1);
CellUtil.SetCellStyleProperty(cell1, wb, CellUtil.BORDER_BOTTOM, BorderStyle.Thin);
CellUtil.SetCellStyleProperty(cell1, CellUtil.BORDER_BOTTOM, BorderStyle.Thin);
RegionUtil.SetBorderBottom((int)BorderStyle.Thin, range4, sheet, wb);
// write to file

Loading…
Cancel
Save