I have successfully created a workbook with one sheet however while creatin one more sheet into the workbook after a point i am unable to add cell style to sheet . Please help please find the code below. The oldversion excel and new version excel write 2 data set into a sheet with formating. Frist sheet is created as required . The problem comes when i try to append the workbook and add another sheet. the second sheet is generated with partial cell styles . like blue and red colors are lost and some of the cells loose border formatting.
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
namespace VersionReportGen.DataComp
{
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
namespace VersionReportGen.DataComp
{
class GenerateReport
{
public static HSSFWorkbook NewVersionExcel(VersionCompTable newtbleTable, VersionCompTable oldtbleTable, HSSFWorkbook workbook)
{
ISheet sheet1 = workbook.CreateSheet(newtbleTable.FormName);
sheet1.DisplayGridlines = false;
int j = 0;
sheet1.CreateRow(3);
ICellStyle style2 = workbook.CreateCellStyle();
style2.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.GREY_25_PERCENT.index;
style2.FillPattern = FillPatternType.SOLID_FOREGROUND;
style2.BorderBottom = style2.BorderLeft = style2.BorderRight = style2.BorderTop = BorderStyle.THIN;
foreach (DataColumn dc in newtbleTable.UniqueTable.Columns)
{
if (j != 0)
{
sheet1.GetRow(3).CreateCell(j).CellStyle = style2;
sheet1.GetRow(3).GetCell(j).SetCellValue(dc.ColumnName.ToString(CultureInfo.InvariantCulture));
j++;
}
else j++;
}
for (int i = 0; i < newtbleTable.UniqueTable.Rows.Count; i++)
{
sheet1.CreateRow(i + 4);
for (int k = 1; k < newtbleTable.UniqueTable.Columns.Count; k++)
{
if (newtbleTable.CommonValueComparison.Rows[i][k].ToString() != "E")
{
ICellStyle style1 = workbook.CreateCellStyle();
style1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.WHITE.index;
style1.FillPattern = FillPatternType.SOLID_FOREGROUND;
style1.BorderBottom =
style1.BorderLeft = style1.BorderRight = style1.BorderTop = BorderStyle.THIN;
sheet1.GetRow(i + 4).CreateCell(k).SetCellValue(newtbleTable.UniqueTable.Rows[i][k].ToString());
sheet1.GetRow(i + 4).GetCell(k).CellStyle = style1;
}
else
{
ICellStyle style1 = workbook.CreateCellStyle();
style1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.YELLOW.index;
style1.FillPattern = FillPatternType.SOLID_FOREGROUND;
style1.BorderBottom =
style1.BorderLeft = style1.BorderRight = style1.BorderTop = BorderStyle.THIN;
sheet1.GetRow(i + 4).CreateCell(k).SetCellValue(newtbleTable.UniqueTable.Rows[i][k].ToString());
sheet1.GetRow(i + 4).GetCell(k).CellStyle = style1;
}
}
//row.CreateCell();
}
for (int i = 0; i < newtbleTable.ColumnList.Rows.Count; i++)
{
sheet1.CreateRow(i + newtbleTable.UniqueTable.Rows.Count + 4);
ICellStyle style1 = workbook.CreateCellStyle();
style1.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.LIGHT_GREEN.index;
style1.FillPattern = FillPatternType.SOLID_FOREGROUND;
for (int k = 1; k < newtbleTable.ColumnList.Columns.Count; k++)
{
style1.BorderBottom =
style1.BorderLeft = style1.BorderRight = style1.BorderTop = BorderStyle.THIN;
sheet1.GetRow(i + newtbleTable.UniqueTable.Rows.Count + 4).CreateCell(k).CellStyle = style1;
sheet1.GetRow(i + newtbleTable.UniqueTable.Rows.Count + 4).GetCell(k).SetCellValue(newtbleTable.ColumnList.Rows[i][k].ToString());
}
}
//freez
workbook.GetSheet(newtbleTable.FormName).CreateFreezePane(newtbleTable.UniqueTable.Columns.Count + 2, 0, newtbleTable.UniqueTable.Columns.Count + 2, 0);
int totalcount = (oldtbleTable.UniqueTable.Rows.Count + oldtbleTable.ColumnList.Rows.Count) -
newtbleTable.UniqueTable.Rows.Count + newtbleTable.ColumnList.Rows.Count;
if (totalcount > 0)
{
for (int i = 0; i <= totalcount; i++)
{
sheet1.CreateRow(newtbleTable.UniqueTable.Rows.Count + newtbleTable.ColumnList.Rows.Count + 4 + i);
}
}
OldVersionExcel(oldtbleTable, workbook, newtbleTable.UniqueTable.Columns.Count + 4, newtbleTable.FormName);
return workbook;
//FileStream file = new FileStream ( "D:\\test.xls" , FileMode.Create );
//workbook.Write ( file );
//file.Close ( );
}
private static void OldVersionExcel(VersionCompTable oldtbleTable, HSSFWorkbook workbook, int width, string formName)
{
//workbook = new HSSFWorkbook ( );
ISheet sheet1 = workbook.GetSheet(formName);
int j = 0;
sheet1.GetRow(3);
ICellStyle style2 = workbook.CreateCellStyle();
style2.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.GREY_25_PERCENT.index;
style2.FillPattern = FillPatternType.SOLID_FOREGROUND;
style2.BorderBottom =
style2.BorderLeft = style2.BorderRight = style2.BorderTop = BorderStyle.THIN;
foreach (DataColumn dc in oldtbleTable.UniqueTable.Columns)
{
if (j != 0)
{
sheet1.GetRow(3).CreateCell(j + width).CellStyle = style2;
sheet1.GetRow(3)
.GetCell(j + width)
.SetCellValue(dc.ColumnName.ToString(CultureInfo.InvariantCulture));
j++;
}
else
{
j++;
}
}
for (int i = 0; i < oldtbleTable.UniqueTable.Rows.Count; i++)
{
sheet1.GetRow(i + 4);
for (int k = 1; k < oldtbleTable.UniqueTable.Columns.Count; k++)
{
ICellStyle cellStyle = workbook.CreateCellStyle();
cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.BLUE.index;
cellStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;
cellStyle.BorderBottom =
cellStyle.BorderLeft = cellStyle.BorderRight = cellStyle.BorderTop = BorderStyle.THIN;
sheet1.GetRow(i + 4).CreateCell(k + width).SetCellValue(oldtbleTable.UniqueTable.Rows[i][k].ToString());
sheet1.GetRow(i + 4).GetCell(k + width).CellStyle = cellStyle;
}
//row.CreateCell();
}
for (int i = 0; i < oldtbleTable.ColumnList.Rows.Count; i++)
{
sheet1.GetRow(i + oldtbleTable.UniqueTable.Rows.Count + 4);
ICellStyle cellStyle = workbook.CreateCellStyle();
cellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.RED.index;
cellStyle.FillPattern = FillPatternType.SOLID_FOREGROUND;
cellStyle.BorderBottom =
cellStyle.BorderLeft = cellStyle.BorderRight = cellStyle.BorderTop = BorderStyle.THIN;
for (int k = 1; k < oldtbleTable.ColumnList.Columns.Count; k++)
{
sheet1.GetRow(i + oldtbleTable.UniqueTable.Rows.Count + 4).CreateCell(k + width).CellStyle = cellStyle;
sheet1.GetRow(i + oldtbleTable.UniqueTable.Rows.Count + 4).GetCell(k + width).SetCellValue(oldtbleTable.ColumnList.Rows[i][k].ToString());
}
}
}
}
}