Hope someone can crack this issues with EPPLUS and Formulas.
I'm getting an invalid #REF! when I try to assign a formula to a cell, yet the last row seems to accept the formula without a problem and does the calculations right.
Here is what the logic looks like at the time of assignment of the Formula. I am referencing data from another sheet.
string formula1 = "";
string formula2 = "";
int uniqueTimeRow = 14;
if (uniqueTimes.Rows.Count != 0)
{
foreach (DataRow row in uniqueTimes.Rows)
{
if (row["ExecutionTime"].ToString() != "")
{
wsSummary.InsertRow(uniqueTimeRow, 1, uniqueTimeRow);
wsSummary.SetValue(uniqueTimeRow, 2, row["ExecutionTime"].ToString());
formula1 = "SUMIF(DataSummary[Strategy],$B" + uniqueTimeRow.ToString() + ",DataSummary[ExecQty])";
formula2 = "SUMIF(DataSummary[Strategy],$B" + uniqueTimeRow.ToString() + ",DataSummary[PrincipalAmount])";
wsSummary.Cells[uniqueTimeRow, 3].Formula = formula1;
wsSummary.Cells[uniqueTimeRow, 4].Formula = formula2;
uniqueTimeRow++;
}
}
}
This is what the result excel file looks like.
Table Produced in Excel with the invalid #REF!
This is the Formula Produced on the Last Cell:
=SUMIF(DataSummary[Strategy],$B28,DataSummary[ExecQty])
=SUMIF(DataSummary[Strategy],$B28,DataSummary[PrincipalAmount])
If I copy these two formulas upwards this is what is produced as expected:
=SUMIF(DataSummary[Strategy],$B27,DataSummary[ExecQty])
=SUMIF(DataSummary[Strategy],$B27,DataSummary[PrincipalAmount])
When it has the invalid #REF! this is what shows up in the formula:
=SUMIF(#REF!,$B27,#REF!)
Just hit the very same problem. I had it working and then it broke after refactoring. Before refactoring (by pure chance) I was copying rows upwards one at a time and this appears to be the only way it works properly.
To clarify you have to copy a row then paste it onto the row above. Now copy that row, and paste that onto the row above:
copy row 10, paste row 9, copy row 9, paste row 8... and so on...
Related
I'm trying to create a spreadsheet where the first sheet ("Catalog") contains some pre-filled and some empty values in a column. I want the values to be in a drop down list that are restricted to values found in the second sheet ("Products").
I would expect that if I set the the Excel validation formula for cells "A1:A1048576" in the "Catalog" sheet to be a list validation of "Products!A1:A100" that every cell would only allow values from "Products!A1:A100". However, I'm finding that my formula gets incremented for every row in the "Catalog" sheet (i.e. In row 2 the formula becomes "Products!A2:A101", in row 3 the formula becomes "Products!A3:A102").
If version matters I'm using EPPlus.Core v1.5.4 from NuGet.
I'm not sure if this is a bug or if I'm going about applying my formula wrong?
I've already tried directly applying the validation to every cell in the column one cell at a time. I found that not only does it moderately increase the size of the resulting Excel file but more importantly it also exponentially increases the time taken to generate the Excel file. Even applying the validation one cell at a time on the first 2000 rows more than doubles the generation time.
ExcelPackage package = new ExcelPackage();
int catalogProductCount = 10;
int productCount = 100;
var catalogWorksheet = package.Workbook.Worksheets.Add($"Catalog");
for (int i = 1; i <= catalogProductCount; i++)
{
catalogWorksheet.Cells[i, 1].Value = $"Product {i}";
}
var productsWorksheet = package.Workbook.Worksheets.Add($"Products");
for (int i = 1; i <= productCount; i++)
{
productsWorksheet.Cells[i, 1].Value = $"Product {i}";
}
var productValidation = catalogWorksheet.DataValidations.AddListValidation($"A1:A1048576");
productValidation.ErrorStyle = ExcelDataValidationWarningStyle.stop;
productValidation.ErrorTitle = "An invalid product was entered";
productValidation.Error = "Select a product from the list";
productValidation.ShowErrorMessage = true;
productValidation.Formula.ExcelFormula = $"Products!A1:A{productCount}";
I guess I'm not that adept at Excel formulas.
Changing this line:
productValidation.Formula.ExcelFormula = $"Products!A1:A{productCount}";
to this:
productValidation.Formula.ExcelFormula = $"Products!$A$1:$A${productCount}";
stopped the auto increment issue. Hopefully this answer will save someone else some sanity as I wasted half a day on this issue myself.
I have c# windows application that is reading files content. I wanted to extract values from used rows only.
I am using this code:
int rows = ExcelWorksheet.UsedRange.Rows.Count;
Everything works fine. Except when I have empty rows on top, the counting will be incorrect.
-File has no special characters, formula or such. Just plain text on it.
-The application can read excel xls and xlsx with no issue if the file has no empty rows on top.
Okay, now I've realized I'm doing it all wrong. Of course it will not read all of my UsedRange.Rows because in my for loop, I am starting the reading always on the first row. So I get the ((Microsoft.Office.Interop.Excel.Range)(ExcelWorksheet.UsedRange)).Row; as a starting point of reading
This code works:
int rows = ExcelWorksheet.UsedRange.Rows.Count;
int fRowIndex = ((Microsoft.Office.Interop.Excel.Range)(ExcelWorksheet.UsedRange)).Row;
int rowCycle = 1;
for (int rowcounter = fRowIndex; rowCycle <= rows; rowcounter++)
{
//code for reading
}
Instead of read Excel row-by-row, better to get it in C# as a Range, and then handle it as
Sheet.UsedRange.get_Value()
for whole UsedRange in Sheet. Whenever you'd like to get a part of UsedRange, do it as
Excel.Range cell1 = Sheet.Cells[r0, c0];
Excel.Range cell2 = Sheet.Cells[r1, c1];
Excel.Range rng = Sheet.Range[cell1, cell2];
var v = rng.get_Value();
You well know size of v in C# memory from the values of [r1-r0, c1-c0]
I want to delete every row that comes after my specific keyword and including the keyword row as well. Can anybody please help me with C# code.
Example :- I have not attached a dummy excel file as I am posting it from mobile.
I have a word in excel :- "hello user" so I have to delete all the lines of data after this including it as well. And most important this data is not on the fixed line it can be on any line number....Suppose for now I have given it on 10th line so it may come on 12th line in next file.
Line no. Data
10. "hello user"
11. A
12. B
And so on
.
.
.
.
.
I think this code should work for you:
const string magicWord = "HelloUser";
Excel.Application app = new Excel.Application();
Excel.Workbook workbook = app.Workbooks.Open(#"myWorkbook.xlsx");
Excel.Worksheet worksheet = workbook.Sheets[1]; //Excel has no zero based index!!!
int magicWordRowIndex = Int32.MaxValue;
//Here we find your magic word. But we can't delete the rows here, so just save the index
for (int row = 1; row <= worksheet.Rows.Count; row++)
{
for (int column = 1; column <= worksheet.Columns.Count; column++)
{
if (worksheet.Rows[row][column] == magicWord)
{
magicWordRowIndex = row;
break;
}
}
}
//Now we run reversed, because else our magicWordRowIndex become invalid if we delete rows
for (int row = worksheet.Rows.Count; row >= magicWordRowIndex; row--)
{
((Excel.Range) worksheet.Rows[row, Missing.Value]).Delete(Excel.XlDeleteShiftDirection.xlShiftUp);
}
Actualy I'm not able to test it but should do the job, maybe you need minor changes and fit it to your needs.
First it finds your magicword which is hardcoded on the beginning. After finding this word we go delete all rows from bottom up to your existing word.
Notice that you need to reference: Microsoft.Office.Interop.Excel
I am trying an automation work on an excel sheet using c#. I want to delete blank rows present at the end of the excel sheet based on the no. of records in the first column only. Any other column may have different no. of rows than that in the first column.
For this purpose first I tried to evaluate the range of the first column and based on that I tried to delete rows after this in the excel sheet.
I used the following lines of code for this purpose :-
Range lastrow_new = sourceSheet2.get_Range("A6", Type.Missing);
if (excelApp.WorksheetFunction.CountA((int)(sourceSheet2.Rows[lastrow_new])) == 0)
{
for (int i = 1; i <= (int)(sourceSheet2.Rows[lastrow_new]); i++)
{
if (excelApp.WorksheetFunction.CountA(sourceSheet2.Rows[i]) == 0)
{
Range BlankRows = sourceSheet2.get_Range("A" + i + ":" + "FN" + lastrow_new);
BlankRows.Select();
BlankRows.EntireRow.Delete();
i = (int)(sourceSheet2.Rows[lastrow_new]) + 1;
}
}
}
But it is giving error of 'Type mismatch'.
Can anyone help ?
Thanks in advance.
I'm not sure i understand what you want... but if you want clear the consecutive rows after 'A6' column, you can try this code:
string value = sourceSheet2.Range["A1"].SpecialCells(XlCellType.xlCellTypeLastCell).Address;
value = value.Replace("$","");
sourceSheet2.Range["A6", value].ClearContents();
What I do: populate & format an Excel file using a mix of Interop and ClosedXML.
First, the file is populated via Interop, then saved, closed, then I format the cells' RichText using ClosedXML.
Unfortunately, this formatting causes Excel to view my file as "corrupt" and needs to repair it.
This is the relevant part:
var workbook = new XLWorkbook(xlsPath);
var sheet = workbook.Worksheet("Error Log");
for (var rownum = 2; rownum <= 10000; rownum++)
{
var oldcell = sheet.Cell("C" + rownum);
var newcell = sheet.Cell("D" + rownum);
var oldtext = oldcell.GetFormattedString();
if(string.IsNullOrEmpty(oldtext.Trim()))
break;
XlHelper.ColorCellText(oldcell, "del", System.Drawing.Color.Red);
XlHelper.ColorCellText(newcell, "add", System.Drawing.Color.Green);
}
workbook.Save();
And the colouring method:
public static void ColorCellText(IXLCell cel, string tagName, System.Drawing.Color col)
{
var rex = new Regex("\\<g\\sid\\=[\\sa-z0-9\\.\\:\\=\\\"]+?\\>");
var txt = cel.GetFormattedString();
var mc = rex.Matches(txt);
var xlcol = XLColor.FromColor(col);
foreach (Match m in mc)
{
txt = txt.Replace(m.Value, "");
txt = txt.Replace("</g>", "");
}
var startTag = string.Format("[{0}]", tagName);
var endTag = string.Format("[/{0}]", tagName);
var crt = cel.RichText;
crt.ClearText();
while (txt.Contains(startTag) || txt.Contains(endTag))
{
var pos1 = txt.IndexOf(startTag);
if (pos1 == -1)
pos1 = 0;
var pos2 = txt.IndexOf(endTag);
if (pos2 == -1)
pos2 = txt.Length - 1;
var txtLen = pos2 - pos1 - 5;
crt.AddText(txt.Substring(0, pos1));
crt.AddText(txt.Substring(pos1 + 5, txtLen)).SetFontColor(xlcol);
txt = txt.Substring(pos2 + 6);
}
if (!string.IsNullOrEmpty(txt))
crt.AddText(txt);
}
Error in file myfile.xlsx
The following repairs were performed: _x000d__x000a__x000d__x000a_
Repaired records:
string properties of /xl/sharedStrings.xml-Part (strings)
I've been through all the xmls looking for clues. In the affected sheet, in comparison view of Productivity Tool, some blocks appear as inserted in the repaired file and deleted in the corrupt one, although nothing significant seemed changed - except for one thing: the style attribute of that cell. Here an example:
<x:c r="AA2" s="59">
<x:f>
(IFERROR(VLOOKUP(G2,Legende!$A$42:$B$45,2,FALSE),0))
</x:f>
</x:c>
I have checked the styles.xml for style 59, but there is none. In the repaired file, this style has been changed to 14, which in my styles.xml is listed as a number format.
Unfortunately, a global search/replace of these invalid style indexes did not resolve the issue.
Seeing the things going on here with corrupt indexes, renamed xmls, invalid named ranges etc., I took a different route: not to use interop at all, maybe the corruption was caused by Excel in the first place and the coloring was only the last straw.
Using ClosedXml only:
Wow. Just wow. This makes it even worse. I commented out the colouring part since without that, Interop produced a readable file without errors, so that's what I expect of ClosedXml too.
This is how I open the file and address the worksheet with ClosedXml:
var wb= new XLWorkbook(xlsPath);
var errors = wb.Worksheet("Error Log");
This is how I write the values into the file:
errors.Cell(zeile, 1).SetValue(fname);
With zeile being a simple int counter.
I then dare to set a column width:
errors.Column(2).Width = 50;
errors.Column(3).Width = 50;
errors.Column(4).Width = 50;
As well as setting some values in another sheet in exactly the same fashion before saving with validation.
wb.Save(true);
wb.Dispose();
Lo and behold: The validation throws errors:
Attribute 'name' should have unique value. Its current value 'Legende duplicates with others.
Attribute 'sheetId' should have unique value. Its current value '4' duplicates with others.
A couple more errors like attribute 'top' having invalid value '11.425781'.
Excel cannot open the file directly, must repair it. My Sheet "Legende" is now empty and the first sheet instead of third, and I get an additional fourth sheet "Restored_Table1" which contains my original "Legende" contents.
What the hell is going on with this file??
New attempt: re-create the Excel template from scratch - in LibreOffice.
I now think that the issue is entirely misleading. If I use the newly created file from LibreOffice, the validation causes a System.OutOfMemory exception due to too many validation errors. Opening in Excel requires repair, gives additional sheet and so forth.
Creating in LibreOffice, then opening in Excel, saving, then using that file as template produces a much better result albeit not perfect yet.
Since I copied parts over from the old Excel file into LO while creating the new file, I assume some corrupt remnant got copied over.
I cannot shake the feeling that this is the file itself after all and has nothing to do with how I edit it!
Will post updaate tomorrow.
OK. Stuff this.
I created a completely fresh file with LibreOffice, making sure not to copy over anything at all from the original file, and I ditched Interop in favour of ClosedXml.
=> This produced a corrupt file in which my first sheet was cleared and its contents move to a "Restored_Table1".
After I opened my fresh new template with Excel via Open/Repair and saved it, the resulting, uncoloured file was NOT corrupt.
=> Colouring it produces the "original" corruption, all sheets intact.
ClosedXml seems to be marginally slower than Interop but at this point I couldn't care less. I guess we will have to live with the "corrupt" message and just get on with it.
I hate xlsx.