EPPlus - Row Out Of Range exception - c#

I'm trying to import datatable with 1000000 records into Excel using EPPlus library.
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");
var recordCount = dt.Rows.Count;
ws.Cells["A5"].LoadFromDataTable(dt, true);
I'm getting Row Out of Range exception on ws.Cells["A5"].LoadFromDataTable(dt, true); line
It worked when I had 1000 records.
Is there row limit size when working with EPPlus?

You probably tried to import a little bit more than 1.000.000 records. The maximum before throwing this exception is 1.048.576 (which exactly equals the maximum number of rows specified for Excel version 2010).
Have a look at the source code:
The last line of the LoadFromDataTable method in ExcelRangeBase.cs returns the imported excelrange: _worksheet.Cells[...]
You can find the implementation of this indexer in ExcelRange.cs and there you'll see that the range is validated by ValidateRowCol (all the way at the bottom).
Finally, this validation throws the exception you mentioned when the requested row is larger than the MaxRows property of an excelpackage, which equals 1.048.576

When you get "Row out of range", the row parameter is probably too high or too low.
Remember that it starts to count from 1.

I got this error when I had placed a list in a list. Should have been only a list and not a list in a list.
To be exact i created a list of object that I then placed in a list, parsed to JSON and loaded to a DataTable with JsonConvert.DeserializeObject(json, jsonFormat). Then I got the error.

Related

Excel Worksheet used Range Count is to big C#

I've got a problem with my .UsedRange.Count in my code.
I try to count the used rows in my Worksheet to define a Range from the first Value row of the Sheet to the last. For this I need to get the used Rows in my Worksheet and I try to do it with
this.worksheet.UsedRange.Count
But the Result is to big. My Worksheet just got 140 Rows and .UsedRange.Count is counting something above 100.000
Also
this.worksheet.Rows.Count
won´t work. The result is something near 100.000
Solved the Problem:
However this.worksheet.UsedRange.Count is multiplying the Rows with the Columns (counts all cells that were used)
With this.worksheet.UsedRange.Rows.Count i just get the Row Count.

How to find the maximum row and column WITHOUT reading all data?

Using C# .Net Google Sheets API.
I am new to the API, so I may have missed it in the docs - but how do you find out the maximum row and column that contain a value without reading all the data in the sheet?
For example, if a sheet contains multiple values and the "last" cell in the sheet with a value is at C139 (no cells in the rows following have a value and no cells in any column after C have a value), then the maximum row would be 139 and the maximum column would be 2 (zero based) or 3 (one based).
I tried sheet.Properties.GridProperties.RowCount -- but that gives the TOTAL number of rows in the sheet (whether the cells have values or not).
Same goes for sheet.Properties.GridProperties.ColumnCount -- gives the TOTAL number of columns in the sheet (whether the cells have values or not).
Any links or ideas are welcome.
I understand that you want to know the last row of data in your Sheet. In that case, you can use a simple GET with a full range. For example let's assume that your Sheet only has two columns, in that case you can set up the range like A1:B. That range will include the full two columns, but the get will only get as far as the data goes. At this step you already have an array filled with your data range, so you only have to count the array index of the last element in order to know the last row value. If you don't know how many columns your Sheet have, you only have to modify the range in a similar way as before (i.e. A1:Z). Please ask me any doubts about this approach.

ExcelDataReader in C# - How to reference an individual Cell using row and column cordinates

I'm reading an .xlsx spreadsheet into a C# console app with a view to outputting the content as a formatted xml file (to be picked up by another part of the system further down the line).
The problem with the the .xslx file is that it's a pro-forma input document based on, and replacing, an old paper-based order form we used to provide to customers, and the input fields aren't organised as a series of similar rows (except in the lower part of the document which consists of up to 99 rows of order detail lines). Some of the rows in the header part of the form/sheet are a mixture of label text AND data; same with the columns.
Effectively, what I need to do is to be able to cherry pick data from the initial dozen or so rows in order to poke data into the xml structure; the latter part of the document I can process by iterating over the rows for the order detail lines.
I can't use Interop as this will end up as an Azure function - so I've used ExcelDataReader to convert the spreadsheet to a dataset, then convert that dataset to a new dataset entirely composed of string values. But I haven't been able to successfully point to individual cells as I had expected to be using syntax something like
var cellValue = MyDataSet.Cell[10, 2];
I'd be grateful for any advice as to how I might get the result I need.
A Dataset has Tables and those have Rows which hold ColumnValues
A WorkSheet transforms into a Table (with Columns) and the Cells transform to Rows and column values.
To find the cell value at [10,2] on the first Worksheet do:
var cellValue = MyDataSet.Tables[0].Rows[10][2];
Remember that cellValue will be of type object. Cast accordingly.

Interop Excel : PivotTable.RefreshTable() throws exception

I am trying to refresh my PivotTable which has been already rendered. Below is the code to refresh :
Excel.PivotTables pivotTables = worksheet.PivotTables();
Excel.PivotTable pvt = pivotTables.Cast<Excel.PivotTable>().FirstOrDefault(c => c.Name == pivotTableName);
pvt.RefreshTable(); //throws exception
I am getting below exception:
This command requires at least two rows of source data. You cannot use the command on a selection in only one row. Try the following:
• If you're using an advanced filter, select a range of cells that contains at least two rows of data. Then click the Advanced Filter command again.
• If you're creating a PivotTable report or PivotChart report, type a cell reference or select a range that includes at least two rows of data.
I am able to understand that the sourcedata gets corrupted or modified, but not able to reach the solution.
Any help or suggestion would be great.
Thanks.

Adding a Sumif formula to cell error using c#

I'm trying to add a formula to a cell in multiple rows in Excel through C#.
What i'm trying to achieve with the formula is to SUM all the cells which does not contain the string N/A, in column D to S in every row.
This is the formula i'm trying to add:
=SUMIF(DX:SX,"<>N/A")
(In the example above i changed the actual row number with X)
So i'm looping through the rows and doing this:
foreach (var row in rows)
{
ws.Cells[row, 2].Formula = string.Format("=SUMIF(D{0}:S{0},\"<>N/A\")", row + 1);
}
Here I get the exception:
System.ArgumentException : Failed to parse: =SUMIF(D2:S2,"<>N/A"). Error:
Unsupported function: SUMIF.For list of supported functions consult
GemBox.Spreadsheet documentation.
I've also tried to add the Swedish version of the formula which is:
=SUMMA.OM(DX:SX;"<>N/A")
(In the example above i changed the actual row number with X)
and here i get the exception:
System.ArgumentException : Failed to parse: =SUMMA.OM(D2:S2;"<>N/A"). Error:
Not expected: SUMMA
The weirdest part of this is that if i manually enter the swedish formula in the cell in Excel, it works.
Appreciate any help i can get, thank you so much.
I actually don't see any reason it would throw that error -- what you did seems like it should work. In the spirit of offering another avenue to test, you could also try the R1C1 version of the formula and see if that works:
foreach (var row in rows)
{
ws.Cells[row, 2].FormulaR1C1 = "=SUMIF(R[0]C[2]:R[0]C[17],\"<>N/A\")";
}
Unless your rows variable contains an invalid number, I'm not sure why it would throw that exception. In the example error you gave, that should be fine.

Categories