Detecting last row and calculating total rows on page - epplus

I am currently working with a client that generates their invoices manually in Excel. I have used epplus to generate this file automatically for them.
One of the requirements I have is that each page should have a border around it and start with a header that is about 7 rows long (and each row has different heights based on what I have to put in them). In order to do this, I need to detect when I am at the end of a page so that I can pause my data printing and print out the header, then print out all the data for that page and print the border on the page based on how many rows are on the page.
This was a bit easier when the row heights were static because I could just assume that based on the page settings, that there were a specific amount of rows per page. However, some of the rows on the invoice can have multiple lines which means that WrapText = true. This obviously alters the numbers of rows on the page which throws all my formatting off.
Is there a way to calculate how many rows are on a page and detect when I am on a row where the page ends?
Thanks in advance for any help

To get the row count, and column count, I used the following code:
// get the first worksheet in the workbook
ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets[1];
//Gets the row count
var rowCnt = worksheet.Dimension.End.Row;
//Gets the column count
var colCnt = worksheet.Dimension.End.Column;

Related

Epplus row height issue

I have an excel file with over 1000 rows. Each row contains some data and 2 images.
The images are attached as OfficeOpenXml.Drawing.eEditAs.OneCell
After populating the Excel I run this, to set the row height.
int prodTableStart = 3;
int prodTableEnd = 1025;
while (prodTableStart <= prodTableEnd)
{
ws.Row(prodTableStart).Height = 112d; // works, but mega slow
prodTableStart++;
}
I tried to speed up with something like this: ws.Cells["A" + prodTableStart + ":L" + prodTableEnd].Rows but that returns an int?
So how can I set the row height efficient on a selected range of rows?
When I have so many rows, it even never ends. No exception is thrown. The process just takes for ever.
ps. I am using epplus latest nuget (4.1.0) on .Net 4.6.2 in C#
Setting the row height in EPPlus can be really slow. Instead of updating the row height of multiple rows one by one, you can set the row height for all rows very fast like:
workSheet.DefaultRowHeight = 500;
If you don't want to set all row heights you now can set back the row height to the previous default value of these rows one by one. This solution is faster if the rows the height needs to be updated are more than the rows the height not needs to be updated.
To anyone looking for this 4 years later: for some reason, resizing a row that contains an image slows things significantly. For efficiency, calculate and set the rows heights before adding the images. You will likely need to add the rows with all of their text information, with string.Empty cells where the images will go, and then add them after setting heights for the rows.

Count number of times data is repeated in a column and show number in another column?

I have a UserForm that finds and copies a range of data and pastes it along with new data from the UserForm into a different sheet. I am wanting for it to also count the number of times some of that data is repeated in the new sheet. I have tried several different approches but none are working. Right now this is what I have:
.Range("I1").Value = iVal = Application.WorksheetFunction.CountIf(Range("A3:A103"), "CrisisNameTextBox1.Value")
This puts "TRUE" in the corresponding cell instead of the number of times the data is repeated.
You probably could use this function in the corresponding cell:
=COUNTIF( [range], [cell] )
The [range] is the range you want to check (use $ to make sure its value does not change from one cell to another)
The [cell] is the cell you are currently checking.
Assign the function to the cells you want and it should work
If you want to drag/copy this formula from one cell to an other : use the $ character to lock the coordinates you don't want to be changed. I.e :
$A1 Will prevent the column from being changed, but will allow the row
A$1 Will prevent the row from being changed, but will allow the column
$A$1 Will prevent both coordinates from being changed
The same applies to ranges ($A1:$F15, $A1:F1, $A$1:F15, A1:$F15, etc.)

Finding last written row in Google Spreadsheet API

Is there any way to find the last row you have written in a google spreadsheet in Java?
I tried to do that by having a variable which I keep in another file and I update that every time I do another writing. Is there any other way?
Finding last written row in Google Spreadsheet API
I'll give you a concept first using REST calls but you'll have to apply that in Java.
Exceed the range of cells where you're writing.
So to find last written row between cells A1:A10, use the range A1:A11 or A1:B (using B means all the rows in cell A will be traversed).
Fetch a range of cells, using GET. Parse the response result. And get the length of the parsed values. The length will always indicate the last row since Sheetsv4 ignores blank/empty cells.
So example I have a range of cells between A1:A10. I wrote "hello" in A10. Following the concepts above I do this:
..
xhr.open('GET', 'https://sheets.googleapis.com/v4/spreadsheets/'+myspreadsheetId+'/values/Sheet1!A1:A10);
..
xhr.onload = function (oEvent) {
arrayBuffer = xhr.response;
myArray = JSON.parse(arrayBuffer);
console.log("last row is " + myArray.values.length)
.. //output is 'last row is 10'
This part of my XHR request returns 10. Now I know that the last written row in my sheet is A10.
To do this in Java use the solution based on this SO thread.
mService.spreadsheets().values().get("ID_SHEET", "Sheet1!A1:B").execute();
Also check this thread about writing on data cells using Java. I think it offers additional insight as well.

Creating a Macro in Excel 2007 to extract moving data

I have one sheet which is comprised of data that was imported from a web page. In a second sheet I have a cell asking for min price and max price which comes from the imported data. The issue is that the data can move to different cells when it's refreshed so my code needs to look for some specific wording to find the prices that I need rather than just directing it to the same cell each time
The info in the imported data sheet which I need to extract from will always be in this string of text which will always be found somewhere between row 15-35 but will move when the website is refreshed. The 1st number (71.00) needs to be extracted to another sheet in a cell asking for min price while the second number (75.00) is the max price. These prices can change so I can just look for those numbers and extract them.
SLAUGHTER BULLS: Yield grade 1-2 1000-1500 lbs
1500-2000 lbs 71.00-75.00.
Could someone please help me with the coding for this macro?
Use the InStrRev function (similar to the LastIndexOf function in .NET) to find the last index of "lbs" in the test string.
lastIndex = InStrRev(testString, "lbs", 1, 1)
Then search from that last index to the end of the string spliting on the -
splitArray = Split(Mid(testString, lastIndex, Len(testString) - lastIndex), "-", 1)
The lowest value will be in splitArray(0) and the upper value will be in splitArray(1) assuming a zero indexed array.

Crystal Reports - Facilitating front/back printing

I have a single crystal report that usually ends up about 150 pages long. The single .rpt, through the use of clever grouping, actually includes records with little individual header and footer sections for each customer. Think of it as a sales report for 100 customers, each with their own section.
Each customer section is 2 to 7 pages in length.
We would like to print on the front and back of pages. However, my concern is that approximately half of the customer sections would begin printing on the back side of the last page of the previous customer section.
Edit: What this boils down to... I need to insert an extra page break if the current page number is odd. Any ideas?
well if all your asking for is a formula to insert a page break when page number is odd then add this to the New Page After or New Page Before formula on the desired Section of the report.
PageNumber Mod 2 = 1
Sorry I cant be of more help but Im having trouble understanding your question.
EDITED:
Thanks for the clarification
basically in your grouping for your customer (ie "little individual footer section"), you will need to do something like this in the New Page After formula
(Next({Customer.CustomerId}) <> {Customer.CustomerId})
AND
(PageNumber MOD 2 = 1)
Note Customer.CustomerID should reference a unique value for your customer in your recordset. I just made something up since I dont know your data field names. Basically the forumla above will look at the Next Unique Customer and if it is not the same value as the current customer, then you know that a new Customer section is starting. Combine that logic with weather or not the PageNumber is odd and you can tell weather or not you need to insert a page break.
EDITED 2
(ISNULL(Next({Customer.CustomerId})) OR Next({Customer.CustomerId}) = "")
AND
(Next({Customer.CustomerId}) <> {Customer.CustomerId})
AND
(PageNumber MOD 2 = 1)
You may want to check if Next Customer ID (IE Unique Customer Value) is null this way you dont end up with a blank page at the very end of the report.