Getting the number of rows PER PAGE in Excel using C# - c#

I stated the question above the way I did, because I can already do the following, and it doesn't give me the solution I need.
I can:
Get the number of pages per sheet
Get the last USED row on any given SHEET (not per printed page, so that doesn't help)
I can find out if manual or automatic page breaks have been inserted
(just not WHERE)
And that's what I need: To know the number of the row for each page break on any given sheet. Some sheets have 1 page, some have 100.
I need to do this because I need to put something at the bottom of each page, however, the number of pages per sheet is not static. Some sheets have been set to landscape, others to portrait. Some sheets have the "Put all columns on one page" option set, which CHANGES the number of rows on that page.
PLEASE NOTE: The solution to this problem is not "Use the footer", as the footer will not accept the data that I need to put on each page.
I also cannot statically decide the number of rows each page type might have -- I let Excel decide that when I set the orientation.
I've been looking for several days now, and I have been unable to find how to determine what the last row on a printed page will be.
I'm using office and VS 2010 and I'm using the office interop. I'm not posting much code because I have no idea what to use at this point. However, it is safe to say that to access any given worksheet I'm using the following code:
Excel.Application excelApp = new Excel.Application();
Excel.Worksheet workSheet;
I'd be willing to bet that there's a very simple way to do this, and I just haven't hit across the right combination of google search terms. But it's been a few days now, so I thought I'd post something here. Thanks for any help.

I don't think you have a way to calculate that on your own... It is very complicated, because the number of rows on each page depends on many variables (Printer page size, orientation, zoom level, etc.).
You can try to use this: Repeat specific rows or columns on every printed page

Related

Gembox Spreadsheet - Remove blank pages

I dont know how achieve this, I have to create an informative report with images. For this, I use an excell template (It helps me with the format and with the position of the text where to place the respective information). The images are generated perfectly. I convert this XLS to PDF with the property
DocumentToMemoryStream (excelTemplate, XlsxSaveOptions.XlsxDefault);
This report generate blank pages in the PDF. How can I remove these blank pages, before the excel is generated?
Without checking your spreadsheet it's impossible to tell from where do those empty pages come from, so can you upload your XLS?
Anyway, do you perhaps have some explicitly defined horizontal or vertical page breaks?
If yes, can you remove the ones you don't need?
Or, do you perhaps have empty columns at the end which have some kind of styling or formatting?
If yes, then you could try using something like this to remove those empty columns from exporting to PDF:
var workbook = ExcelFile.Load("input.xls");
var worksheet = workbook.Worksheets.ActiveWorksheet;
worksheet.NamedRanges.SetPrintArea(
worksheet.GetUsedCellRange(true));
var options = new PdfSaveOptions();
options.SelectionType = SelectionType.ActiveSheet;
workbook.Save("output.pdf", options);
I had a counter of the rows of the document I was writing on, thanks to the help of #Mario Z, I use this function, before the save and solve my problem of blank pages
wsimgs.NamedRanges.SetPrintArea(wsimgs.Cells.GetSubrange("A1",CellRange.RowColumnToPosition (countRow, 10)));

How to generate freeform rdlc reports with one page per row

Given a data set containing multiple rows, from within a .NET console application I need to generate a report on a single page for each row, sending those pages directly to the printer.
I am attempting to use Microsoft Report for this by attaching it to a data set and placing TextBoxes where I wish. Generating the report and sending it to the printer are not a problem. Unfortunately, the data only seems to be available in aggregates -- First, Sum, Last, Max, etc. I cannot latch the text box to a bare field.
Some poking around here and other sites seems to address this, but only when the data is presented in a table. One post even said without elaboration, "My mistake was using Text Boxes"
Am I using the wrong tool for what I am attempting to accomplish?
I ran into the same problem and managed to solve it. The solution seems a little convoluted to me so don't quote me on the "right" way to do this, but here is what I did:
Make sure you have a Dataset defined for your report.
Add a "Table" control to the report. This seems to be needed in order to iterate the rows in your Dataset.
Delete the header row and two of the default columns from the table so that you are left with a single row with a single column.
Expand the table to the width of your layout and make it as tall as you will need for your "free form" layout.
By default, there is a TextBox inside the table cell. Right-click the empty table cell and choose "delete" to remove that TextBox.
Drag a "Rectangle" control into the empty table cell. It seems to automatically "dock" to the width/height of the table cell.
Now you should be able to drag the fields from your DataSet (TextBoxes, etc) into the Rectangle to produce the desired layout.
Note that I am in the early stages of using this approach so I'm not sure if I am going to hit any walls... but for a basic report that uses TextBoxes and a page break after each "row" it seems to be working ok.
Or you try to use a list.
In the list you can arange textboxes (and other controls) as you want and they will be filled for each record in the recordset.
This work for me. :-)

Using selection and ranges in word interop

How do ranges and selections work?
If you insert something at the top of the page through a range, is that overridden by inserting something with selection? Does selection affect ranges?
I'm having a problem where I do inserts in this order, but the InlineShapes show up at the top of the document above the Range.Inserts. Also, the Selection.InsertBefore gets replaced by the InlineShapes.
I would like my range insert to appear above each picture, like a heading. How can this be done?
Selection.InsertBefore
**Loop**
Range.InsertParagraphAfter
Selection.InlineShapes.AddPicture
**End Loop**
I've asked a question like this three times now with no responses. Anything would help!
EDIT:
I'm a step closer. Now, when I add my InlineShapes, I use Content.InlineShapes.AddPicture(FileName: x, range: y). Now the pictures are showing up in between paragraphs, but they are in the first cell of a table I add to the document before adding the picture. I add the table with with a collapsed range.
You write:
I've asked a question like this three times now with no responses. Anything would help!
And for reference, the questions referred to (I guess) are:
How to add items one at a time to to a new line a word document using word interop
Using interop.word ranges
... which all now have been answered :)
You ask multiple questions in this post however - first:
How do ranges and selections work?
Actually, the documentation covers this quite well:
A range:
"Represents a contiguous area in a document.
Each Range object is
defined by a starting and ending character position. Similar to the
way bookmarks are used in a document, Range objects are used to
identify specific portions of a document..."
Reference: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.range(v=office.14).aspx.
If you insert something at the top of the page through a range, is
that overridden by inserting something with selection?
It can be. This depends on what has been selected. If you select something that intersects with the range of the content you just inserted and changes the content, then you will make changes to the content of the range.
To understand this completely, I will recommend that you read about:
The Selection Interface
The Range Interface
Does selection affect ranges?
It depends. The documentation states:
Range objects are independent of the selection.
Reference: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.range(v=office.14).aspx
... so if you just do a simple selection in order to, e.g., read content or copy content to
the clipboard, nothing would change. However, if you use the selection to change content, of course this would affect the content pointed to by the ranges. In continuation of this you should note that MS recommends using Range objects for document manipulation:
Because Range objects share many of the same methods and properties as
Selection objects, using Range objects is preferable for manipulating
a document when there isn't a reason to physically change the current
selection.
Reference: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.selection.aspx
And finally, you ask:
I'm having a problem where I do inserts in this order, but the
InlineShapes show up at the top of the document above the
Range.Inserts. Also, the Selection.InsertBefore gets replaced by the
InlineShapes.
I would like my range insert to appear above each picture, like a
heading. How can this be done?
See my previous answer to your question: " How to add items one at a time to to a new line a word document using word interop " here: https://stackoverflow.com/a/12751805/700926 - that gives you a complete answer.

Hyperlink within a OpenOffice calc using c#

I've made a spreadsheet in openoffice using c# with several different sheets. On sheet 1 is a list of all the names of the rest of the sheet. how is it possible to set up a hyperlink from each of these cells to there corresponding sheet.
I have looked through the internet but i have struggled to find any helpful information. Can anybody point me in the right direction on how to program this?
I'm not sure if i understand you right, but this may be helpful, put it in your cell:
=HYPERLINK("MyTableName.C3")
This (should) jump to cell C3 in table MyTableName.
To just jump to a table use
=HYPERLINK("#MyTableName")
I cannot try it because i don't use OpenOffice
Source: http://www.ooowiki.de/HyperLink

Excel: Ctrl + DownArrow action

Anybody please tell me which action program does when I click CTRL+↓ hotkey?
I know that program navigates down to the next non-empty cell. But when I create Excel document from Excel.Interop the CTRL+ ↓ doesn't work like it have to. It navigates to the last cell in the column...
Any help is greatly appreciated.
UPDATE: I'm sorry for being not so clear. I don't care about CTRL+↓ behaviour actually. I'm concerning about what causes data acting like one big cell?
Jeffrey,
Not sure what you mean by, "acting like one big cell". If you want to get the entire used area of a sheet, used the property UsedRange on the Worksheet object. It will return a two-dimensional array of the used range for that Worksheet. I've used it when a Worksheet contained all the data in a table. Much easier than iterating the Worksheet.
Cheers!

Categories