/// <summary>
/// 取得資料列Footer樣式
/// </summary>
/// <param name="rowColor">資料列顏色</param>
/// <returns>資料列Title樣式</returns>
private ICellStyle GetFooterStyle(Color rowColor)
{
ICellStyle style = _workBook.CreateCellStyle();
style.Alignment = HorizontalAlignment.RIGHT;
style.VerticalAlignment = VerticalAlignment.CENTER;
style.BorderBottom = NPOI.SS.UserModel.BorderStyle.DOUBLE;
style.FillPattern = FillPatternType.SOLID_FOREGROUND;
style.FillForegroundColor = GetColorIndex(rowColor);
style.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
style.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
style.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
style.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;
return style;
}
/// <summary>
/// 取得欄位樣式
/// </summary>
/// <param name="cell">HSSFCell</param>
/// <param name="rowColor">資料列顏色</param>
/// <param name="cellColor">資料欄位顏色</param>
private void SetCellStyle(ICell cell, Color rowColor, Color cellColor)
{
ICellStyle style = null;
string styleKey = String.Format("{0}_{1}", rowColor, cellColor);
if (_cellStyles.ContainsKey(styleKey))
{
style = _cellStyles[styleKey];
}
else
{
style = _workBook.CreateCellStyle();
if (cellColor != Color.Empty)
{
style.SetFont(GetCellFontColor(cellColor));
}
if (rowColor != Color.Empty)
{
style.FillPattern = FillPatternType.SOLID_FOREGROUND;
style.FillForegroundColor = GetColorIndex(rowColor);
}
style.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
style.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
style.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
style.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;
style.WrapText = true;
style.VerticalAlignment = VerticalAlignment.TOP;
_cellStyles.Add(styleKey, style);
}
cell.CellStyle = style;
}
/// <summary>
/// 設定欄位值
/// </summary>
/// <param name="columnIdx">欄位數</param>
/// <param name="cell">HSSFCell</param>
/// <param name="cellValue">欄位值</param>
private void SetCellValue(int columnIdx, ICell cell, string cellValue)
{
short format = GetDataFormat(columnIdx);
if (format >= 1 && format <= 4)
{
cell.SetCellValue(Double.Parse(cellValue));
}
else
{
cell.SetCellValue(cellValue);
}
}
/// <summary>
/// 取得資料格式
/// </summary>
/// <param name="columnIdx">欄位數</param>
/// <returns>資料格式</returns>
private short GetDataFormat(int columnIdx)
{
short format = -1;
if (_cellDataFormat.ContainsKey(columnIdx))
{
format = _cellDataFormat[columnIdx];
}
else
{
string cssClass =
_gvData.Columns[columnIdx].ItemStyle.CssClass;
if (!String.IsNullOrEmpty(cssClass))
{
string[] cssClasses = cssClass.Split(' ');
foreach (string css in cssClasses)
{
if (css.ToLower().StartsWith("decimal"))
{
format = GetDecimalDataFormat(css);
break;
}
}
}
_cellDataFormat.Add(columnIdx, format);
}
return format;
}
/// <summary>
/// 取得數字資料格式
/// </summary>
/// <param name="format">數字資料格式</param>
/// <returns>NPOI相對應的數字資料格式</returns>
private short GetDecimalDataFormat(string format)
{
if (_dataFormat.ContainsKey(format))
{
return _dataFormat[format];
}
else
{
short result;
if (String.Compare(format, "decimal", true) == 0)
{
result = HSSFDataFormat.GetBuiltinFormat("#,##0");
}
else
{
result = HSSFDataFormat.GetBuiltinFormat("#,##0.00");
}
_dataFormat.Add(format, result);
return result;
}
}
/// <summary>
/// 取得資料列背景顏色
/// </summary>
/// <param name="row">資料列</param>
/// <returns>資料列背景顏色</returns>
private Color GetRowColor(GridViewRow row)
{
if (row.BackColor != Color.Empty)
{
return row.BackColor;
}
else
{
return (row.RowIndex + 1) % 2 == 0 ?
_gvData.AlternatingRowStyle.BackColor :
_gvData.RowStyle.BackColor;
}
}
/// <summary>
/// 取得資料欄位背景顏色
/// </summary>
/// <param name="field">資料控制項欄位</param>
/// <param name="dataCell">資料欄位</param>
/// <returns>資料欄位背景顏色</returns>
private Color GetTableCellColor(DataControlField field, TableCell dataCell)
{
if (dataCell.ForeColor != Color.Empty)
{
return dataCell.ForeColor;
}
return field.ItemStyle.ForeColor;
}
/// <summary>
/// 取得欄位樣式
/// </summary>
/// <param name="cell">HSSFCell</param>
/// <param name="row">資料列</param>
/// <param name="dataCell">資料欄位</param>
private void SetCellStyleOld(ICell cell, GridViewRow row, TableCell dataCell)
{
if (dataCell.ForeColor != Color.Empty)
{
HSSFCellUtil.SetFont(cell, _workBook, (HSSFFont)GetCellFontColor(dataCell.ForeColor));
}
if (row.BackColor != Color.Empty)
{
HSSFCellUtil.SetCellStyleProperty(cell, _workBook, HSSFCellUtil.FILL_FOREGROUND_COLOR, GetColorIndex(row.BackColor));
}
}
/// <summary>
/// 取得欄位字型
/// </summary>
/// <param name="color">欄位字型顏色</param>
/// <returns>欄位字型</returns>
private IFont GetCellFontColor(Color color)
{
if (_fontColors.ContainsKey(color))
{
return _fontColors[color];
}
else
{
IFont font = _workBook.CreateFont();
font.Color = GetColorIndex(color);
_fontColors.Add(color, font);
return font;
}
}
/// <summary>
/// 轉換顏色為NPOI可接受的顏色值
/// </summary>
/// <param name="cellColor">欄位顏色</param>
/// <returns>NPOI可接受的顏色值</returns>
private short GetColorIndex(Color cellColor)
{
return GetColorIndex(cellColor, Color.White);
}
/// <summary>
/// 轉換顏色為NPOI可接受的顏色值
/// </summary>
/// <param name="cellColor">欄位顏色</param>
/// <param name="defaultColor">預設顏色</param>
/// <returns>NPOI可接受的顏色值</returns>
private short GetColorIndex(Color cellColor, Color defaultColor)
{
HSSFPalette palette = _workBook.GetCustomPalette();
if (cellColor != Color.Empty)
{
HSSFColor color =
palette.FindColor(cellColor.R, cellColor.G, cellColor.B);
if (color != null)
{
return color.GetIndex();
}
return palette.FindSimilarColor(cellColor.R, cellColor.G, cellColor.B).GetIndex();
}
else if (defaultColor != Color.Empty)
{
return palette.FindSimilarColor(defaultColor.R, defaultColor.G, defaultColor.B).GetIndex();
}
return palette.FindSimilarColor(Color.White.R, Color.White.G, Color.White.B).GetIndex();
}
/// <summary>
/// 設定欄位寬度
/// </summary>
/// <param name="sheet">HSSFSheet object</param>
/// <param name="gvData">GridView object</param>
private void SetAutoSizeColumn(ISheet sheet, GridView gvData)
{
for (int i = 0, iCount = gvData.HeaderRow.Cells.Count; i < iCount; i++)
{
sheet.AutoSizeColumn(i);
}
}
#endregion
}
/// <summary>
/// 輸出的檔案類型
/// </summary>
public enum ExportFileType
{
/// <summary>
/// Excel
/// </summary>
Excel,
/// <summary>
/// Word
/// </summary>
Word
}
}