Epplus conditional formatting based on a rule - c#

How can we add three icon sets to each cell in excel using epplus conditional formatting. I'm using following code to add three icon set:
using (ExcelRange scoreRange = workSheet.Cells[1, 2, 1, 10])
{
ExcelAddress rangeAddress = new ExcelAddress(scoreRange.Address);
var ruleIconSet = workSheet.ConditionalFormatting.AddThreeIconSet(rangeAddress, eExcelconditionalFormatting3IconsSetType.Arrows); // This calculates based on the value in the range
}
I want to create a rule like if value in a cell is less than 0, the green color icon should be displayed, if value is greater than 0, the red color icon should be displayed.
What should be the rule statement that can perform this stuff?

for(int j =2; j <=9; j++) //Loop through columns
{
for(int i = 3; i <= 12; i++) // Loop through rows
{
// gets only the current cell as range
ExcelRange rng = worksheet.Cells[i, j, i, j];
ExcelAddress address = new ExcelAddress(rng.Address);
// Get the value of the current cell
if(Convert.ToDouble(worksheet.Cells[i, j].Value) >= 4.0)
{
var v = worksheet.ConditionalFormatting.AddThreeIconSet(address, eExcelconditionalFormatting3IconSetType.Arrows);
v.Reverse = true;
v.Icon1.Type = eExcelConditionalFormattingValueObjectType.Num;
}
else if(Convert.ToDouble(workSheet.Cells[i, j].Value) > 1.0 && Convert.ToDouble(workSheet.Cells[i, j].Value) < 4.0)
{
var v = worksheet.ConditionalFormatting.AddThreeIconSet(address , eExcelconditionalFormatting3IconsSetType.Arrows);
v.Icon3.Type = eExcelConditionalFormattingValueObjectType.Num;
}
else (Convert.ToDouble(workSheet.Cells[i, j].Value) < 1.0)
{
var v = worksheet.ConditionalFormatting.AddThreeIconSet(address , eExcelconditionalFormatting3IconsSetType.Arrows);
v.Icon2.Type = eExcelConditionalFormattingValueObjectType.Num;
}
}
}
This works for me.

Related

C# Word Interop Table alignment issue, while moving to server below code not able to resize the table cell

This is my code:
Private void TAbleconvertion () {
// Table alignment issue
for (int TableCount = 1; TableCount <= wdDocConv.Tables.Count; TableCount++) {
if (wdDocConv.Tables[TableCount] != null) {
Microsoft.Office.Interop.Word.Table tbl = wdDocConv.Tables[TableCount];
tbl.AllowAutoFit = true;
Range r = tbl.Range;
Cells cells = r.Cells;
for (int count = 1; count <= cells.Count; count++) {
// Resizing width - but not working when this code move to windows server
int size = 100 / cells.Count;
Cell cell = cells[count];
cell.PreferredWidth = size;
cells.PreferredWidth = size;
Marshal.ReleaseComObject(cell);
}
}
}
}

How to use mutliple colors in same cell in excel with C#

I need to use multiple colors in one excel cell but the code below works only last condition.
I want like: grren-red-green-red-green-red
It works like: green-green-green-green-green-red
List<string> myTexts = new List<string>
{
"aaaaaaaaaaaaaaaaa", // green expected
"bbbbbbbbbbbbbbbbb", // red expected
"ccccccccccccccccc", // green expected
"ddddddddddddddddd", // red expected
"eeeeeeeeeeeeeeeee", // green expected
"fffffffffffffffff" // red expected
};
var startIndex = 1;
for (int i = 0; i < liste.Count; i++)
{
cell.Value2 += myTexts[i];
if (i%2 == 0)
{
cell.Characters(startIndex, myTexts[i].Length).Font.Color= Color.Green;
}
else
{
cell.Characters(startIndex, myTexts[i].Length).Font.Color = Color.Red;
}
startIndex += myTexts[i].Length;
}
it should work if you set the cell.value2 text outside the loop to the complete string and manipulate the style afterwards:
cell.Value2 = string.Join("", myTexts);
var startIndex = 1;
for (int i = 0; i < myTexts.Count; i++)
{
if (i % 2 == 0)
{
cell.Characters[startIndex, myTexts[i].Length].Font.Color = Color.Green;
}
else
{
cell.Characters[startIndex, myTexts[i].Length].Font.Color = Color.Red;
}
startIndex += myTexts[i].Length;
}

How to color specific row in excel table?

I create excel file using NPOI dll's.
I have this code that create excel table from List<someObjects> :
IWorkbook workbook = new XSSFWorkbook();
ISheet sheet1 = workbook.CreateSheet("Sheet 1");
IRow header = sheet1.CreateRow(0);
header.CreateCell(0).SetCellValue("Id");
header.CreateCell(1).SetCellValue("Name");
header.CreateCell(2).SetCellValue("E-Mail");
header.CreateCell(3).SetCellValue("PhoneNumber");
for (int i = 0; i < list.Count(); i++)
{
IRow row = sheet1.CreateRow(i + 1);
row.CreateCell(0).SetCellValue(list[i].id);
row.CreateCell(1).SetCellValue(list[i].name);
row.CreateCell(2).SetCellValue(list[i].email);
row.CreateCell(3).SetCellValue(list[i].phoneNumber);
}
Then I make each cell bordered in the table created above.
Here is the code:
public void setBorderExcel()
{
XSSFCellStyle myStyle = (XSSFCellStyle)workbook.CreateCellStyle();
myStyle.BorderBottom = BorderStyle.Medium;
myStyle.BorderTop = BorderStyle.Medium;
myStyle.BorderLeft = BorderStyle.Medium;
myStyle.BorderRight = BorderStyle.Medium;
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 4; j++)
{
workbook.GetSheetAt(0).GetRow(i).GetCell(j).CellStyle = myStyle;
}
}
}
Then I make each odd row in the table created above colored.
Here is the code:
public void setColorExcel()
{
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 4; j++)
{
if (i % 2 == 0) continue;
workbook.GetSheetAt(0).GetRow(i).GetCell(j).CellStyle.FillForegroundColor = HSSFColor.Grey25Percent.Index;
workbook.GetSheetAt(0).GetRow(i).GetCell(j).CellStyle.FillPattern = FillPattern.SolidForeground;
}
}
}
And here is the result that I get:
As you can see the color applied to all rows in the table while, I wanted to color only the odd rows.
My question is why I get colored all rows? And how can I make colored only specific rows?
I think I understand it. You have applied the same XSSFCellStyle instance to all cells' Style property in (setBorderExcel). So, now they all have the same instance, so when you change a property on the CellStyle of one of the cells, it's changing the CellStyle instance which is associated with all cells.
You'll most likely need two XSSFCellStyle instances. One for odd rows and another for even rows.
I tried it out, and although I'm not sure why your way doesn't work, there is an easier way with less lines (simply declare another CellStyle with grey background, and use that instead of myStyle):
XSSFCellStyle myStyle = (XSSFCellStyle)workbook.CreateCellStyle();
myStyle.BorderBottom = BorderStyle.Medium;
myStyle.BorderTop = BorderStyle.Medium;
myStyle.BorderLeft = BorderStyle.Medium;
myStyle.BorderRight = BorderStyle.Medium;
XSSFCellStyle myStyleGrey = (XSSFCellStyle)workbook.CreateCellStyle();
myStyleGrey.BorderBottom = BorderStyle.Medium;
myStyleGrey.BorderTop = BorderStyle.Medium;
myStyleGrey.BorderLeft = BorderStyle.Medium;
myStyleGrey.BorderRight = BorderStyle.Medium;
myStyleGrey.FillForegroundColor = HSSFColor.Grey25Percent.Index;
myStyleGrey.FillPattern = FillPattern.SolidForeground;
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 4; j++)
{
if (i % 2 == 0)
workbook.GetSheetAt(0).GetRow(i).GetCell(j).CellStyle = myStyle;
else
workbook.GetSheetAt(0).GetRow(i).GetCell(j).CellStyle = myStyleGrey;
}
}
You can remove your code for setColorExcel() and it should work as expected, setting odd rows to grey.

C# Gridview to Excel with number formatting and table line

Hello I want import my data in Gridview to Excel, my data in Gridview is "00, 02, 04" but in excel my data change to "0, 2, 4" I dont wont it, i want my data in Excel like same in Gridview.And How to move rows to new columns, for exp i have 160 rows data, i want in excel split to 3 columns, 1 column have 40 rows. This my code :
private void exToExcel()
{
if (RekapdataGridView.Rows.Count > 0)
{
Microsoft.Office.Interop.Excel.Application XcelApp = new Microsoft.Office.Interop.Excel.Application();
XcelApp.Application.Workbooks.Add(Type.Missing);
//XcelApp.Cells[5,2].NumberFormat = "00";
XcelApp.Cells[1, 1] = "Tanggal";
XcelApp.Cells[1, 2] = labelTglRekap.Text;
XcelApp.Cells[2, 1] = "Kode Pemain";
XcelApp.Cells[2, 2] = labelKodePemain.Text;
XcelApp.Cells[4, 1] = "No";
int x = RekapdataGridView.RowCount;
for (int y = 1; y <= RekapdataGridView.RowCount; y++)
{
XcelApp.Cells[4+y, 1] = y;
}
for (int i = 2; i < RekapdataGridView.Columns.Count + 2; i++)
{
XcelApp.Cells[4, i] = RekapdataGridView.Columns[i - 2].HeaderText;
}
for (int i = 0; i < RekapdataGridView.Rows.Count; i++)
{
for (int j = 0; j < RekapdataGridView.Columns.Count; j++)
{
XcelApp.Cells[i + 5, j + 2] =string.Format("{0:00}", RekapdataGridView.Rows[i].Cells[j].Value);
}
}
XcelApp.Columns.AutoFit();
XcelApp.Visible = true;
}
}
This is my data in excel :
I want my data can showing like this image :
Sorry if my English is bad, i hope someone can help me.
You nearly had it with
XcelApp.Cells[5,2].NumberFormat = "00";
Just change it to
XcelApp.Cells[5,2].NumberFormat = "#";
Which will make the cell format be 'Text' instead of number. Excel always tries to simplify numbers from my experience.
Just don't forget to do it to ALL cells you are writing into.
Hope this helps!

How to merge first 3 columns for rows with same value in PDF

I'm using itextSharp to export a DataTable to a pdf table. I can export the data to a pdf table using the sample code i have posted below. The DataTable contains close to 21 columns.
The first column in the pdf (DataTable) might contain similar values for any number of rows. If the data values in first column for a group of rows is similar, i want to merge the first 3 columns of those rows as one cell.
I'm having trouble modifying the code below to achieve this.
public iTextSharp.text.Table GetItextTable(DataTable dtChartData, string reportType)
{
int intCols = dtChartData.Columns.Count; //Total number of columns
int intRows = dtChartData.Rows.Count; //Total number of rows
iTextSharp.text.Table pdfTable = new iTextSharp.text.Table(intCols, intRows);
try
{
pdfTable.BorderWidth = 1;
pdfTable.Width = 100;
pdfTable.Padding = 1;
pdfTable.Spacing = 1;
/*creating table headers */
for (int i = 0; i < intCols; i++)
{
iTextSharp.text.Cell cellCols = new iTextSharp.text.Cell();
iTextSharp.text.Font ColFont = iTextSharp.text.FontFactory.GetFont("Tahoma", 07,
iTextSharp.text.Font.BOLD);
for (int l = 0; l < dtChartData.Columns.Count; l++)
{
if (dtChartData.Columns[l].ColumnName.Contains("_"))
{
dtChartData.Columns[l].ColumnName = dtChartData.Columns[l].ColumnName.Replace("_", " ");
}
}
iTextSharp.text.Chunk chunkCols = new iTextSharp.text.Chunk(dtChartData.Columns[i].ColumnName,ColFont);
cellCols.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
if ((chunkCols.ToString().ToLower() == "ReportDetails"))
{
cellCols.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
}
}
/* loop that take values from every row in datatable and insert in itextsharp table */
for (int k = 0; k < intRows; k++)
{
for (int j = 0; j < intCols; j++)
{
iTextSharp.text.Cell cellRows = new iTextSharp.text.Cell();
iTextSharp.text.Font RowFont = iTextSharp.text.FontFactory.GetFont("Tahoma", 07);
iTextSharp.text.Chunk chunkRows = new iTextSharp.text.Chunk(dtChartData.Rows[k][j].ToString(),RowFont);
cellRows.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
cellRows.Add(chunkRows);
pdfTable.AddCell(cellRows);
}
}
}
catch (Exception ex)
{
//error handling code here removed
}
return pdfTable;
}
Have you tried to change the Colspan?
iTextSharp.text.Cell cell = new iTextSharp.text.Cell();
cell.AddElement(new Paragraph("colspan 3"));
cell.Colspan = 3;
table.AddCell(cell);
In this case, cell will span three columns.
try this. i just edited your code, not checked
here i have edited 1 line of your code and added a new line (total 2 lines of change only).
dont forget to combine headding row like this, if you need
/* loop that take values from every row in datatable and insert in itextsharp table */
for (int k = 0; k < intRows; k++)
{
for (int j = 2; j < intCols; j++)// EDIT: if all first 3 cols are same, then starts with 2
{
iTextSharp.text.Cell cellRows = new iTextSharp.text.Cell();
if(j == 2) cellRows.Colspan = 3;// ADD: it'll gives a 3 times long cell
iTextSharp.text.Font RowFont = iTextSharp.text.FontFactory.GetFont("Tahoma", 07);
iTextSharp.text.Chunk chunkRows = new iTextSharp.text.Chunk(dtChartData.Rows[k][j].ToString(),RowFont);
cellRows.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
cellRows.Add(chunkRows);
pdfTable.AddCell(cellRows);
}
}

Categories

Resources