c# edit excel file if print pages > 1? - c#

I have a function that writes a large list of data to an excel file. It is created on the fly, with a header and title being inserted at creation rather than using an existing template. At the minute it prints out at about 6 pages, I know i can use
xlWorkSheet2.PageSetup.Pages.Count
to get how many pages there are, but what i'm looking to do is detect when there will be a new page printed, and add my header/title to the top of this page before it prints.
Any ideas how to do this?

You can define the header once and then use this to set the range for a header which is repeated on each subsequent page.
xlWorkSheet2.PageSetup.PrintTitleRows = "$1:$1"

Related

Determining location of the automatic page breaks in OpenXML worksheet

I am working on a program that generates an output document in Excel xlsx format. I wrote the functionality and was getting the expected output/result using Excel Interop, originally, but it was taking quite awhile to complete a single output so I am trying to duplicate the same functionality using OpenXML (currently using the ClosedXML library to simplify the conversion from the Excel Interop but I'm open to any other libraries or methods...)
The basic process is opening the template file (which has the initial print area [A1:M27] and print settings are already set up and defined) and then populating the template with the data. The first row of data starts in cell A11 with additional rows are inserted below this, as needed. (There is additional stuff on the template that is below this table of data, so the last row that is inserted is not the bottom of the actual print area.)
After inserting the second row of data, I have a function that will check if the value of the given cell and the value of the cell located immediately above it are the same. If so, then it checks to make sure that there aren't any page breaks located there before merging them.
Here's that section of relevant code:
For Each HBreak In outputSheet.PageSetup.RowBreaks
If HBreak = FirstCell.CellAbove.WorksheetRow.RowNumber Then Exit Sub
Next HBreak
When I generate the output, however, it still ends up merging cells across page breaks. I stepped through the code and found that outputSheet.PageSetup.RowBreaks is always empty... yet when I open the output file in Excel, there will be an automatic page break going right through the middle of the cells it merged.
  
 
I did a test and inserted a horizontal page break right where the data rows are inserted into the template to see if it would correctly detect manually-placed page breaks and then the check for a page break worked as intended... but only for the manually inserted break.
So it seems my issue is with the page break locations that are determined automatically based on the existing printing and page setup.
Is it possible to determine where the automatic page breaks are with OpenXML (or anything other than Excel Interop)?
Any help or advice would be greatly appreciated.
Edit/Update:
I managed to come up with a way to work-around this for the current situation, but it doesn't work very well after inserting the first initial page break.
I took the template and incrementally increased the row height until I found the point where increasing any row heights by one additional point caused the automatic page-break to shift. I wrote a function that summed the row heights before that pagebreak, giving me a value of 943.
Then, before checking for any page breaks, I call this procedure:
Private Sub InsertPageBreaks(outputSheet As IXLWorksheet)
Dim TotalHeight As Double = 0
Dim PageBreakHeight As Double = 943.5 ' Worksheet/Page Layout-specific value
' Reset/clear all of the horizontal row breaks
outputSheet.PageSetup.RowBreaks.Clear()
For x = 1 To (outputSheet.LastRowUsed(True).RowNumber)
TotalHeight += outputSheet.Row(x).Height
If TotalHeight > PageBreakHeight Then
' If the total height exceeds the page break point, insert a new
' new page break at this point and then reset TotalHeight
outputSheet.PageSetup.AddHorizontalPageBreak(x - 1)
TotalHeight = 0
End If
Next x
End Sub
The downside is that I will need to use a variation of this procedure for each different output template I use because of the differences in page layouts, scaling, page orientation, etc., so it isn't a very practical solution beyond this one specific application. Additionally, there appears to be a bit of "drift" that occurs after the first page break is inserted, and the pagebreaks no longer fit where Excel's automatic pagebreaks are inserted by the third page.
So... still looking for a better solution.

How to print using c#/wpf a single flowdocument in two copies in single sheet?

I would like to print a flowdocument which is receipt. Now this should be printed in two copies,but on single page. How to do that?
I have print page defined in XAML while i get values of particular items in code behind file. I want to print this in two copies, but on single document.

Display two different section in crystal report

I want to display two different section in one crystal report. In my report i have created two section but they both display same data as you can saw in below image.
What setting is needed for displaying different data? Thanks.
Here image of my crystal report design
After seeing the report design, here are your issues:
Just because you have two detail sections doesn't mean it will show you two different values.
A report is structured like a book:
The Report Header and Footer are like the cover and back page - you only put information on them that you want to see once.
The Page Header and Footer should contain information that you want to see on every page at the top and bottom.
The Group Header and Footer are used to show information if your data is grouped by certain criteria - like chapters of a book.
The Detail Section shows either all data or the data that pertains to a group - like the text within the chapters.
So, in your case I would create a group that is based on your employee. Put all your header information (the top 3 lines in your report) in the Page Header. In the Group Header you put everything in line 4 and 5. Line 6 through 17 go into the Detail Section. Line 18 with all the totals goes into the Group Footer. Everything from line 19 on goes into the Page Footer.
I hope this makes sense and I'll be more than happy to help out further.

How can I determine if a MigraDoc table would be split among two pages?

I'm using MigraDoc to generate some PDFs. I have code to create a table of text for each element in an array and am printing out pages with these tables.
However, the requirements I am being given is that if I have 2 tables, and the 2nd table would not fit on the page due to the length of the first table, I need the 2nd table to start the next page. I then need to repeat this for each table I am adding to my document.
How would I go about doing this?
If all tables are small enough to fit a single page, then there is a simple solution: set the KeepWith property of the first row to row count minus one to keep the whole table on one page.
If tables do not always fit a single page: you could try a hack, e.g. setting KeepWith to 6 or 8 or 10 (depends on the height of your table rows). If the value is close to what fits on a single page (without going over), tables will start on a new page automatically.
Obviously this will work very good if table rows have a constant height; if tables rows have varying heights, this will not work reliably, but will still prevent tables that will only have one or two rows on the first page (this is not your requirement, but maybe the requirement can be discussed?).
The clean (but complicated) way to fulfill your requirements: get access to the internal GetRenderInfoFromPage method. You will have to start an incremental process:
1) render the document;
2) if you find a split table, insert a page break before that table and repeat from 1.
See also here:
http://forum.pdfsharp.net/viewtopic.php?p=1960#p1960

Controlling placement of footer sections on page in Crystal Reports

I use CrystalReport in Visual Studio 2005 and make a bill report ... this bill
I use the header - Details- and footer in this report ..
in details section I limit the record to 10 for every page
so if there is a bill contains 15 record ..
first 10 record in first page . and 5 in the second page when print
put the problems
1- the footer show in the second page only
2- in the second page the Fields in the footer Raised up beacuse the second page contains 5 records only
so if the record number in the report is not 10 so the footer fields raised up
so please any one help me to solve the problem
Notation : I limit the number of records for every page to 10 record only .. i need empty
records if the reports records < 10 \
Please any one help my
Thanks
Sorry I don't know 100% what your problem is (maybe post some screenshots) but there might be two possible problems/solutions:
in CR Reports you've got document and page footer - if you use the document footer (don't remember the exact name) you will see this only once - just like a document header
if the details parts get's to big the footers are AFAIK the first thing that get's striped so try to compress your details (or any other section) to get more area for the footer (or display only 9 items per page instead of 10 - just for trial)
To really fill your report with empty details you will have to append empty rows to your data and use conditional supresses in your CR to not show things like '0' but IMHO this will get a real pain and you should go without printing empty details - never seen a situation where you need this or where it is desired.

Categories

Resources