```
```
```
#region GetCellStyle
public ICellStyle GetCellStyle(HSSFWorkbook wb, string type, string backcolor)
{
ICellStyle cstyle = null;
DataRow[] drFilter = this.CellStyleList.Select("Type = '" + type + "' AND Color = '" + backcolor + "'");
if (drFilter.Length == 0)
{
cstyle = wb.CreateCellStyle();
cstyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
cstyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
cstyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
cstyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
System.Drawing.Color c = System.Drawing.ColorTranslator.FromHtml("#" + backcolor);
NPOI.HSSF.UserModel.HSSFPalette p = wb.GetCustomPalette();
NPOI.HSSF.Util.HSSFColor hc = p.FindColor(c.R, c.G, c.B);
if (hc == null)
{
p.SetColorAtIndex(NPOI.HSSF.Util.HSSFColor.Lavender.Index, c.R, c.G, c.B);
hc = p.GetColor(NPOI.HSSF.Util.HSSFColor.Lavender.Index);
}
cstyle.FillPattern = FillPattern.SolidForeground;
cstyle.FillBackgroundColor = hc.Indexed;
cstyle.FillForegroundColor = hc.Indexed;
//cstyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Lavender.Index;
CellStyleList.Rows.Add(new object[] { type, backcolor, cstyle });
}
else
cstyle = (ICellStyle)drFilter[0]["Style"];
return cstyle;
}
#endregion
protected void ExportToExcel()
{
try
{
/*Get Data*/
TableData = new DataAccess.GetDate();
/* Building CellStyle List */
this.CellStyleList = new DataTable();
this.CellStyleList.Columns.Add("Type");
this.CellStyleList.Columns.Add("Color");
this.CellStyleList.Columns.Add("Style", typeof(ICellStyle));
/* Prepare for workbook */
HSSFWorkbook hssfwb = new HSSFWorkbook();
/* Create Sheet */
ISheet sheetContent = hssfwb.CreateSheet("Name");
/* Create Header Row*/
int iRowCounter = 0;
int iColCounter = 0;
IRow row = sheetContent.CreateRow(iRowCounter);
#region Render Header Row
DataTable dtHeader = this.TableData.Tables[0];
/*Loop to create each Header cell.*/
ICell cell;
if (dtHeader != null && dtHeader.Rows.Count > 0)
{
foreach (DataRow dr in dtHeader.Rows)
{
cell = row.CreateCell(iColCounter);
cell.SetCellValue(dr["HeaderName"].ToString());
cell.CellStyle = GetCellStyle(hssfwb, "HEADER", "EDEDE3");
cell.CellStyle.Alignment = HorizontalAlignment.Center;
iColCounter++;
}
}
#endregion
#region Render Data Row
DataTable dtRow = this.TableData.Tables[1];
DataTable dtData = this.TableData.Tables[2];
string value = "";
string backColor = "FFFFFF";
string type = "";
int cellIdx = 0;
foreach (DataRow drRow in dtRow.Rows)
{
iRowCounter++;
int rowIdx = dtRow.Rows.IndexOf(drRow);
row = sheetContent.CreateRow(iRowCounter);
foreach (DataRow drCell in dtHeader.Rows)
{
backColor = "FFFFFF";
value = "";
type = "CONTENT";
cellIdx = dtHeader.Rows.IndexOf(drCell);
cell = row.CreateCell(cellIdx);
if (cellIdx == 0)
{
//cell.SetCellValue(rowIdx + 1);
value = drRow["ImpactTypeName"].ToString();
type = "CONTENT_CENTER";
row.Cells[cellIdx].SetCellValue(value);
row.Cells[cellIdx].CellStyle = GetCellStyle(hssfwb, type, backColor);
cell.CellStyle.Alignment = HorizontalAlignment.Center;
}
else
{
type = "CONTENT_LEFT";
DataRow[] drResult = dtData.Select("TargetID=" + drRow["tbBCMmImpactTypeID"].ToString()
+ " AND tbBCMmResourceTimelineID=" + drCell["HeaderID"].ToString()
+ " AND ObjectType = '" + drCell["Type"].ToString() + "'");
if (drResult.Length > 0)
{
value = (string)drResult[0]["ImpactRatingName"];
backColor = (string)drResult[0]["ColorCode"];
}
row.Cells[cellIdx].SetCellValue(value);
row.Cells[cellIdx].CellStyle = GetCellStyle(hssfwb, type, backColor);
cell.CellStyle.Alignment = HorizontalAlignment.Left;
}
}
}
#endregion
/*Write to File.*/
string ExcelFileName = "{0}_File";
System.IO.MemoryStream ms = new System.IO.MemoryStream();
hssfwb.Write(ms);
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + string.Format(ExcelFileName, DateTime.Now.ToString("yyyyMMdd")) + ".xls");
HttpContext.Current.Response.AddHeader("Content-Length", ms.Length.ToString());
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
catch (Exception ex)
{
Framework.Error.AppError.BoxErrorTech(this.Page, ex);
}
}
```
```
```
ColorCode "FF8040", only this color code will replace 1st row of color. Other colors work fine.
```
```
#region GetCellStyle
public ICellStyle GetCellStyle(HSSFWorkbook wb, string type, string backcolor)
{
ICellStyle cstyle = null;
DataRow[] drFilter = this.CellStyleList.Select("Type = '" + type + "' AND Color = '" + backcolor + "'");
if (drFilter.Length == 0)
{
cstyle = wb.CreateCellStyle();
cstyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
cstyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
cstyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
cstyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
System.Drawing.Color c = System.Drawing.ColorTranslator.FromHtml("#" + backcolor);
NPOI.HSSF.UserModel.HSSFPalette p = wb.GetCustomPalette();
NPOI.HSSF.Util.HSSFColor hc = p.FindColor(c.R, c.G, c.B);
if (hc == null)
{
p.SetColorAtIndex(NPOI.HSSF.Util.HSSFColor.Lavender.Index, c.R, c.G, c.B);
hc = p.GetColor(NPOI.HSSF.Util.HSSFColor.Lavender.Index);
}
cstyle.FillPattern = FillPattern.SolidForeground;
cstyle.FillBackgroundColor = hc.Indexed;
cstyle.FillForegroundColor = hc.Indexed;
//cstyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Lavender.Index;
CellStyleList.Rows.Add(new object[] { type, backcolor, cstyle });
}
else
cstyle = (ICellStyle)drFilter[0]["Style"];
return cstyle;
}
#endregion
protected void ExportToExcel()
{
try
{
/*Get Data*/
TableData = new DataAccess.GetDate();
/* Building CellStyle List */
this.CellStyleList = new DataTable();
this.CellStyleList.Columns.Add("Type");
this.CellStyleList.Columns.Add("Color");
this.CellStyleList.Columns.Add("Style", typeof(ICellStyle));
/* Prepare for workbook */
HSSFWorkbook hssfwb = new HSSFWorkbook();
/* Create Sheet */
ISheet sheetContent = hssfwb.CreateSheet("Name");
/* Create Header Row*/
int iRowCounter = 0;
int iColCounter = 0;
IRow row = sheetContent.CreateRow(iRowCounter);
#region Render Header Row
DataTable dtHeader = this.TableData.Tables[0];
/*Loop to create each Header cell.*/
ICell cell;
if (dtHeader != null && dtHeader.Rows.Count > 0)
{
foreach (DataRow dr in dtHeader.Rows)
{
cell = row.CreateCell(iColCounter);
cell.SetCellValue(dr["HeaderName"].ToString());
cell.CellStyle = GetCellStyle(hssfwb, "HEADER", "EDEDE3");
cell.CellStyle.Alignment = HorizontalAlignment.Center;
iColCounter++;
}
}
#endregion
#region Render Data Row
DataTable dtRow = this.TableData.Tables[1];
DataTable dtData = this.TableData.Tables[2];
string value = "";
string backColor = "FFFFFF";
string type = "";
int cellIdx = 0;
foreach (DataRow drRow in dtRow.Rows)
{
iRowCounter++;
int rowIdx = dtRow.Rows.IndexOf(drRow);
row = sheetContent.CreateRow(iRowCounter);
foreach (DataRow drCell in dtHeader.Rows)
{
backColor = "FFFFFF";
value = "";
type = "CONTENT";
cellIdx = dtHeader.Rows.IndexOf(drCell);
cell = row.CreateCell(cellIdx);
if (cellIdx == 0)
{
//cell.SetCellValue(rowIdx + 1);
value = drRow["ImpactTypeName"].ToString();
type = "CONTENT_CENTER";
row.Cells[cellIdx].SetCellValue(value);
row.Cells[cellIdx].CellStyle = GetCellStyle(hssfwb, type, backColor);
cell.CellStyle.Alignment = HorizontalAlignment.Center;
}
else
{
type = "CONTENT_LEFT";
DataRow[] drResult = dtData.Select("TargetID=" + drRow["tbBCMmImpactTypeID"].ToString()
+ " AND tbBCMmResourceTimelineID=" + drCell["HeaderID"].ToString()
+ " AND ObjectType = '" + drCell["Type"].ToString() + "'");
if (drResult.Length > 0)
{
value = (string)drResult[0]["ImpactRatingName"];
backColor = (string)drResult[0]["ColorCode"];
}
row.Cells[cellIdx].SetCellValue(value);
row.Cells[cellIdx].CellStyle = GetCellStyle(hssfwb, type, backColor);
cell.CellStyle.Alignment = HorizontalAlignment.Left;
}
}
}
#endregion
/*Write to File.*/
string ExcelFileName = "{0}_File";
System.IO.MemoryStream ms = new System.IO.MemoryStream();
hssfwb.Write(ms);
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + string.Format(ExcelFileName, DateTime.Now.ToString("yyyyMMdd")) + ".xls");
HttpContext.Current.Response.AddHeader("Content-Length", ms.Length.ToString());
HttpContext.Current.Response.BinaryWrite(ms.ToArray());
}
catch (Exception ex)
{
Framework.Error.AppError.BoxErrorTech(this.Page, ex);
}
}
```
```
```
ColorCode "FF8040", only this color code will replace 1st row of color. Other colors work fine.