How to add a column rows interval in epplus? - c#

I'm trying to evaluate all the cell columns value from H to Y, I don't know the number of rows, I only know the number of cells.
What I tried:
var address = new ExcelAddress("H:Y");
var condition = ws.ConditionalFormatting.AddExpression(address);
condition.Style.Font.Color.Color = Color.Red;
condition.Formula = "IF(H2 < 25, 1, 0)";
As you can see I set as address H to Y and this working well but, there is a problem. I've an header on the first row H1, and I don't need to evaluate it, so I need to start from the second row H2 until Y2, but if I change the interval as: var address = new ExcelAddress("H2:Y2"); I get the color applied only on the second row and not for the other rows.
How can I manage this situation?

If I got your question well you can do the following:
First, know the rows count using:
int row = workSheet.Dimension.End.Row;
Then you can achieve what you want using the following address:
var address = new ExcelAddress($"H2:Y{rows}");

Related

How to insert data to a specific column in a DataGridView?

I have a dataGridView with 5 columns in which I enter my rectangle coordinates and with auto indexing. So the index increases with each new coordinate set. Index number is entered in 1st column, coordinate is entered in second. I have an array where i store my coordinates and index numbers, i process an image in the rectangle and want to enter result data in the corresponding third column of the datagridview. E.G if i process rectangle in 4th row, I want to enter the result data in the 3rd column of 4th row.
I tried the following but it doesnt work:
for (int i = 0; i < pb1rectincrcam4; i++)
{
framecam4.ROI = PB1Rectcam4[i];
framefullcam4.CopyTo(framecam4);
Bitmap bitmap2 = framefullcam4.Bitmap;
Tesseract tesseract = new Tesseract();
tesseract.Init("C:\\Language\\English", "eng", false);
var wordList = tesseract.DoOCR(bitmap2, Rectangle.Empty);
foreach (Word word in wordList)
{
CvInvoke.cvPutText(resultimg, word.Text,new System.Drawing.Point( PB1Rectcam4[i].X, PB1Rectcam4[i].Y) , ref font, color);
listBox1.Items.Add(word.Text.ToString());
g.DrawRectangle(PB1Rectpencam4[i], PB1Rectcam4[i]);
pictureBox5.Image = resultimg.Bitmap;
DataGridViewRow dr = new DataGridViewRow();
dr.CreateCells(dataGridView1);
dr.Cells[i].Value = word.Text;
dataGridView1.Rows.Insert((i),dr);
}
It adds data to the first row always. Please help me to fix this.
Found it.
We can directly add values to selected columns of selected rows using :
dataGridView1[5, (i)].Value = "Data";

I'm working with a table where the last column is of the 16368 place numerarii, and in this case, the code fails

I have already created a similar topic, I found how to find the last column.
The subsequent code only works with a small number of dates. I'm working with a table where the last column is of the 16368 place numerarii, and somehow, in this case, the code fails.
//number of the last column
int x = MatrixRange.Collumns.Count;
//here I check to see what is the numbering column
//(oddly enough, I get always the correct number, but the column the program does not issue
textBox1.Text = x.ToString();
for (int Rnum = 6; Rnum <= MatrixRange.Cells.Count; Rnum++)
{
if ((MatrixRange.Cells[Rnum,x] as Excel.Range).Value2 != null)
{
//at this point the program will either ignore this function or shows an error,
//if x = 10, not 16368, then the code works great
string TabZeile = (MatrixRange.Cells[Rnum,x] as Eexcel.Range).Value2.ToString();
neuXml.Cells [Rnum,4] = TabZeile;
}
}
This is because Excel has limitations:
One of them (you should check others in link above):
Worksheet size 1,048,576 rows by 16,384 columns

How to count blank row of excel from given range using c#

I'm trying to get the row count of rows which don't have any value (any of columns)
Sample image of the Excel file I'm using:
Highlighted rows have some values in some columns rest of rows are blank I need to count those rows.
I already used this method
int blankRows = 0;
double notEmpty = 1;
while (notEmpty > 0)
{
string aCellAddress = "A" + (rowIndex++).ToString();
Excel.Range row = excelApp.get_Range(aCellAddress, aCellAddress).EntireRow;
notEmpty = excelApp.WorksheetFunction.CountA(row);
if (notEmpty <= 0)
{
blankRows++;
}
}
but this is very time consuming process when file is large and minimum number of blank rows is there.
One thing that might help would be to find the last column that has data and last row that has data as to limit your search.
This is VBA code snippet, but could be easily transformed to C#:
'iterate through columns to determine which is longest to determine the highest row number.
For i = 1 To 16384 'number of columns in excel
'get the row
rowcount = ws.Cells(Rows.Count, i).End(xlUp).Row
'check to see if it's larger than what it is now, if it is, set the value of lRow.
If rowcount > lrow Then
lrow = rowcount
End If
Next
then use a similar loop to get the last row based on the last row, stepping through each row until the last one to get the last column with data.
You can use those values to limit the range that you're looking through. I'm not sure if it will be any faster, but it might help.

How to merge cell using closedxml with a dynamic column count?

This is how to merge cell using ClosedXML based on the documentation.
worksheet.Range("B2:D3").Row(1).Merge();
My problem is my column count is dynamic, I can't set column letter value to merge because i will based the merging of cell on my gridview column count.
Anyone who can help me to merge cell using closedXML?
I do it this way
int row = 1;
int col = 1;
worksheet.Range(worksheet.Cell(row, col++), worksheet.Cell(row, col++)).Merge();
Build up the range as a variable based on your column count and pass the variable to the Range method. You don't need to HAVE to pass a hard coded value.
Have you tried the code below?
worksheet.Cell("A1").Value = "Title";
var range = worksheet.Range("A1:F1");
range.Merge().Style.Font.SetBold().Font.FontSize = 16;
It works for me.

How can I get Excel cell contents to right justify using C#?

A portion of my dynamically-created spreadsheet looks like this:
At first, both currency values (the second and third rows in each block of four rows inside the bounded column) were (by default) offset a little from the right, as you can see the second currency value / third row is now.
Thus, the two currency rows were not quite aligning with the first (int) row and the last (%) row.
I want them all to hug the vertical border on the right side of the block, like the int and the % rows.
I experimented by adding this code for the first currency value / second row, to attempt to justify it, hoping it would justify right:
monthPurchasesCell.HorizontalAlignment = XlVAlign.xlVAlignJustify;
...but, as you can see in the screen shot snippet, it does the opposite (justifies left). There is no "xlVAlignJustifyRight" or so.
In context, the code to display the four rows in a month block is:
private void AddMonthData(int packages, decimal purchases, decimal avgPrice, double percent, int colToPopulate)
{
var monthPackagesCell = (Range)_xlSheet.Cells[_curDescriptionTopRow, colToPopulate];
monthPackagesCell.Value2 = packages.ToString("N0", CultureInfo.CurrentCulture);
var monthPurchasesCell = (Range)_xlSheet.Cells[_curDescriptionTopRow + 1, colToPopulate];
monthPurchasesCell.Value2 = purchases.ToString("C", CultureInfo.CurrentCulture);
monthPurchasesCell.HorizontalAlignment = XlVAlign.xlVAlignJustify;
var monthAvgPriceCell = (Range)_xlSheet.Cells[_curDescriptionTopRow + 2, colToPopulate];
monthAvgPriceCell.Value2 = avgPrice.ToString("C", CultureInfo.CurrentCulture);
var monthPercentOfTotalCell = (Range)_xlSheet.Cells[_curDescriptionTopRow + 3, colToPopulate];
monthPercentOfTotalCell.Value2 = percent.ToString("P", CultureInfo.CurrentCulture);
}
How can I justify the currency values to the right?
I think you have to use XlHAlign for horizontal alignments.

Categories

Resources