I have a code in C# with EPPLUS for Excel that fills a cell green if the value of the cell is over 100. It works:
ExcelAddress _formatRangeAddress = new ExcelAddress("G4:G" + (c.Count + 4));
string _statement = "IF(G4>100,1,0)";
var _cond2 = hoja.ConditionalFormatting.AddExpression(_formatRangeAddress);
_cond2.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
_cond2.Style.Fill.BackgroundColor.Color = System.Drawing.Color.LimeGreen;
_cond2.Formula = _statement;
But what I really need is to fill the entire row. If I change the range:
ExcelAddress _formatRangeAddress = new ExcelAddress("A4:G" + (c.Count + 4));
Applies only for the cells in the A column, not to the entire row.
What I am doing wrong?
Make the reference to G4 absolute otherwise the formula will be apply relative to that cell. So change this line:
string _statement = "IF(G4>100,1,0)";
to apply to the entire row based ONLY on G4
string _statement = "IF($G$4>100,1,0)";
(Response to Comments)
to apply to entire rows relative to row number but keep the column fixed to G:
string _statement = "IF($G4>100,1,0)";
Related
I create an Excel file in code. So far, everything works out fine.
Created excel file (image)
I want to create the following chart in code
Excel with Chart (image)
(Notice the selected cells and xseries names)
In Excel its easy.
But how do I do that in code?
My experiment
string values = "='Overall Results'!B3;'Overall Results'!D3;'Overall Results'!F3;'Overall Results'!H3;'Overall Results'!J3";
string xSerie = "='Overall Results'!$B$1:$K$1";
linechart.Series.Add(values, xSerie);
didn't work.
You have to create a string with the EPPlus cell adresses.
string values = worksheet.Cells[3, 2].Address + ":" + worksheet.Cells[3, 4].Address + ":" + worksheet.Cells[3, 6].Address;
linechart.Series.Add(values, ExcelRange.GetAddress(1, 2, 1, 11));
Had this problem aswell, but i found a solution:
For some reason, in order to get multiple specific cells in EPPlus, every cell has to be in a range.
So creating multiple ranges, which each only contains 1 cell, is the way to go
string values = "sheetName!B3:sheetName!B3,sheetName!D3:sheetName!D3,sheetName!F3:sheetName!F3,sheetName!H3:sheetName!H3,sheetName!J3:sheetName!J3";
var valueCells = sheet.Cells[values];
string xSerie = (Same concept);
var xCells = sheet.Cells[xSerie];
linechart.Series.Add(valueCells, xCells);
I am trying to copy the data in excel sheet but it does not show properly it is show like ####### but I want 17-09-2016 like this.kindly suggest me what code I am write to export the excel in proper format.
Code:
var rngTable2 = ws.Range("A:G");
var rngHeaders2 = rngTable2.Range("F4:G4");
rngHeaders2.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.General;
rngHeaders2.Style.Alignment.Vertical = XLAlignmentVerticalValues.Bottom;
Date comes from this code:
Label lblpkgdate = (Label)gvvessel.Rows[j].FindControl("lblpackagedate");
string myVal1 = lblpkgdate.Text;
ws.Cell("F" + index5.ToString("dd/MM/yyyy")).Value = myVal1;
index5++;
Ultimately, it seems like you're trying to get a date from a label and then put this value into a load of cells within column F somewhere. I'm guessing you have this within a for loop as well seeing as you're incrementing index5. So something like this should work:
//Make column F a date column. Alter to a specific range if the whole column shouldn't be of date type.
Range rg = ws.Range("F:F");
rg.EntireColumn.NumberFormat = "DD/MM/YYYY";
var lblpkgdate = (Label).gvvessel.Rows[j].FindControl("lblpackagedate");
//Convert lblpkgdate text to DateTime object assuming format of dd/MM/yyyy to ensure it is actually a date.
DateTime pkgDate = DateTime.ParseExact(lblpkgdate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture);
for(int i = 1, i < YourMaxRowValue, i++)
{
ws.Cell("F" + i).Value = pkgDate;
}
NOTE - I've altered index5 to 'i' as this is less misleading if you're looping. I've also altered myVal1 to pkgDate as I think this is more meaningful.
you can use NumberFormat
Label lblpkgdate = (Label)gvvessel.Rows[j].FindControl("lblpackagedate");
string myVal1 = lblpkgdate.Text;
ws.Cell("F" + index5.ToString()).Style.NumberFormat.Format = "DD-MM-YYYY";
ws.Cell("F" + index5.ToString()).Value = myVal1;
index5++;
so as the title intends, I want to creaete a cell in a NPOI 2.1.3-Workbook containing 2 strings: A "normal"-sized string and a "small"-sized string. => I want to change the font-size for a part of the cell.
My code so far:
var planCell = planRow.CreateCell(lineCnt + 1);
var planCellStyle = workbook.CreateCellStyle();
planCellStyle.WrapText = true;
planCellStyle.VerticalAlignment = VerticalAlignment.Top;
planCellStyle.Alignment = HorizontalAlignment.Left;
var font = workbook.CreateFont();
font.FontName = HSSFFont.FONT_ARIAL;
font.FontHeightInPoints = 16;
font.Boldweight = (short)FontBoldWeight.Bold;
planCellStyle.SetFont(font);
planCell.CellStyle = planCellStyle;
string planTitleContent = string.Empty;
... (some logic to get desired string for planTitleContent)
string planInfoContent = string.Empty;
... (some logic to get desired string for planInfoContent)
planCell.SetCellValue(planTitleContent + "\n\n"+planInfoContent);
To be precise, I want the "planInfoContent"-part to be shown in a smaller font-size than the "planCellContent"-part. I searched a lot, but I just found the CellStyle-value which applies to the whole cell. So I hope I am missing something for two cells are not really an option.
Just figured it out myself :)
First, create 2 fonts of the desired format (for me and for simplicities sake, only the font size is of relevance):
var font1 = excel.CreateFont();
font1.FontName = HSSFFont.FONT_ARIAL;
font1.FontHeightInPoints = 12;
font1.Boldweight = (short)FontBoldWeight.Normal;
var font2 = excel.CreateFont();
font2.FontName = HSSFFont.FONT_ARIAL;
font2.FontHeightInPoints = 8;
font2.Boldweight = (short)FontBoldWeight.Normal;
Then, after you got your string(s), make use of (N)POIs applyFont-Method.
One of its implementations in NPOI has the following signature:
applyFont(int startIndex, int endIndex, IFont font)
so now, having string planTitleContent and string planInfoContent, the remaining steps are pretty obvious: Just create an Instance of IRichTextString and add your strings to it via constructor-parameter. Then, apply the wanted fonts via the index like so:
IRichTextString formattedCellContent = new HSSFRichTextString(planTitleContent + "\n"+planInfoContent);
richString.ApplyFont(0, planTitleContent.Length, font1);
richString.ApplyFont(planTitleContent.Length + 1, (planTitleContent + "\n" + planInfoContent).Length, font2);
planCell.SetCellValue(formattedCellContent);
so, this works like a charm for me. Hope it helps some other folks out!
I am using interop.excel dll and creating an Excel file. I am having issues while writing long numbers as text.
Below is the code I am using. I can use apostrophe, but if you look in formula bar, you can see the cell is formatted.
Is there any other way of writing long numbers to an Excel spreadsheet without seeing the formatted value in the formula bar (using numberformat)?
worksheet.Cells[i, j] = "'" + dr[p];
//var startCell = (Excel.Range)worksheet.Cells[i, j];
//var endCell = (Excel.Range)worksheet.Cells[i, j];
//worksheetRange = worksheet.get_Range(startCell, endCell);
//worksheetRange.NumberFormat = "#####";
If you wanted to do it in one line, you could use the Text function.
Example:
worksheets.Cells[i, j] = "=Text(" + dr[p] + ", \"#\"";
But that probably isn't what you're looking for because this would store the number as a text and display the formula in the formula bar. In that case, like user194076 suggested, you could use .NumberFormat = "#";
or .NumberFormat = "#"; if you don't want it to show in scientific notation.
You can use NumberFormat with '#' or '#'
Example:
Range r= worksheet.Cells;
r.NumberFormat = "#";
I am loading data into excel from datatable using LoadFromDataTable method then changed cell format to integer still it is showing error "The number in this cell is formatted as text or preceded by apostrophe".
cell was showing to right side only and number format only on cell property.
still I am not understanding why I am getting this error??.
Dim wsManufacturing As ExcelWorksheet = pck.Workbook.Worksheets.Add("Manufacturing")
wsManufacturing.Cells("A1").LoadFromDataTable(dtManufacturing, True)
Using col As ExcelRange = wsManufacturing.Cells(2, 2, 2 + dtManufacturing.Rows.Count, 2)
col.Style.Numberformat.Format = "#,##0"
col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right
End Using
You can do like this:
//strValue="98.5%";
double realValue=double.Parse(strValue.Replace("%", string.Empty));
Worksheet.Cells[row + 1, col].Style.Numberformat.Format = "#0\\.00%";
Worksheet.Cells[row + 1, col].Value = realValue;
Changing the format from Text to Number does not change the nature of the entry that was in the cell prior to the format change.
To change entries that were originally textual representations of numbers
Change the cell format to Number
Enter a 1 in some cell
Edit/Copy
Select your cell(s) with the text numbers
Paste Special / Multiply
Delete the original 1