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.
Related
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.
I've 2 excel sheets(Sheet1 & Sheet2) in my Excel workbook. I want to copy row data from Sheet2 to Sheet1.
Condition is:
if Sheet2 copied row doesn't exist in Sheet1 then paste it otherwise don't paste the row.
Copied Rows except 1st row in Sheet2:
Range dataWithoutFirstRow = xlAccrualSheet.Range[xlAccrualSheet.UsedRange.Cells[2, 1],
xlAccrualSheet.UsedRange.SpecialCells(XlCellType.xlCellTypeLastCell)];
dataWithoutFirstRow.Copy();
Paste in below used range in Sheet1:
Range DataRange = xlAccrualWorkSheet.Cells[emptycell, 1];
DataRange.PasteSpecial(XlPasteType.xlPasteAllUsingSourceTheme);
Please Tell me How to check already exist rows in Sheet1.
Awaiting for Your Response
Please Tell me How to check already exist rows in Sheet1.
Use Range to read out all data from the workSheet. You are already doing a similar thing accrualSheet. Then, you can use range.Cells(i,j) or range.Rows(r).Value or even range.Rows(r).Cells(i,j) to get the data inside each specific row.
When pasting occurs, loop over all pasted rows, and for each pasted row compare it with rows from workSheet. You may do it directly by reading that on the fly (as mentioned above), or you may read all rows from workSheet and store them in a List and compare incoming rows against that list - it will work much faster that way.
Now, one of the most interesting things is probably what does it mean to "compare rows". Either you will need to compare all cells within a row with another one, or you will need to compare just a specific set of "columns" like "date, time, cause, origin, caseId" etc. But that.. noone knows, you said nothing about that. If there's no info on that, then you probably should compare whole rows and assume that any difference in any cell means that the rows are different.
In C# using asp/MVC the app generates an Excel .xlsx file based on data thats filled in to the specific columns, works great.
But the goal is to provide additional worksheets that use the same columns but sort on specific colums, such as Column "J"
var wb = new XLWorkbook();
var ws = wb.Worksheets.Add("Proj Info");
var ws2 = wb.Worksheets.Add("Sort By Dates");
The worksheet ws has values filled in by variables or formulas, the data is correct, but cannot make it sort on a column
ws.AutoFilter.Column("J"); //no, nothing changes
ws.Column("J").Sort(); -> this shifts all the columns up but does not sort
ws.Column("J").Sort(XLSortOrder.Ascending); ->same, doesnt sort only shifts
Update: ws.Sort(9); worked in sorting, but the problem is that Column 10 has a Formula, and I need to sort on that Column.
ws.Cell("J" + c).FormulaR1C1 = "=C$2-F" + c;
With this? it WILL NOT SORT. The ws.Sort(10); works when the cell contains a final value, but when its got the Formula? Is there any workaround to force the Excel page to sort after its implemented the formula's in each cell?
You are not sorting the table, but the values in column J.
Try this:
ws.Sort("Column10");
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.
I've read a lot of where people have missing cell data due to null values but my issue is I have spreadsheets I'm parsing that have exactly 272 rows of data needing to be parsed. When I get the row count I'm only able to retrieve 269. Below is how I'm getting the row count.
// code above gets worksheet...
var rows = from row in worksheet.Descendants<Row>()
where row.RowIndex >= rowIndex
select row;
for (int i = 0; i <= rows.Count(); i++)
{
// processing...
}
The rowIndex above is used because I'm only pulling 272 rows after a few heading rows and it needs to be dynamic as some sheets have multiple heading rows. Basically I search for a particular cell value for a string and then from there get the rowIndex and use that to get the row count.
I've tracked down the two rows not being picked up and do not have values in them, but there are other rows exactly the same that do not have values and the rows are included. It is important as all sheets I need to parse have 272 rows of specific data with blank rows in the same spot. One sheet will work fine and include the rows while another won't.
So I'm trying to determine why some blank rows are included but others are totally ignored like they do not exist.
Any help would be appreciated.
The rows that don't have cells that have values should not be included as a rows in the total count. This makes sense since excel does not store row or cell information where the cell value is null.
The other rows that don't have any values in them but do show up most likely have a space in a cell somewhere. A space is a valid value for a cell and while it looks empty, it will be stored as a cell value and count towards the total number of rows.