View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Jialiang Ge [MSFT] Jialiang Ge [MSFT] is offline
external usenet poster
 
Posts: 118
Default Conditional printing

Hello Juan,

From your post, my understanding on this issue is: you want to print the
rows of a Excel worksheet satisfying certain conditions and appropriately
adjust the serial number. If I'm off base, please feel free to let me know.

Here are two approaches:
(The sample codes below is written in C#. To learn how to bind Office
automation servers with Visual C# .NET, please refer to the kb:
http://support.microsoft.com/kb/302902/)

Approach 1. Call Hidden = true for the rows to be filtered, and
appropriately adjust the numbering.
Step 1: Hide the rows and adjust the numbering: (please refer to the
following pseudo code)
int hiddenRowsCount = 0; // used to count the hidden rows
Excel.Worksheet sheet = (Excel.Worksheet)document.Worksheets[index];
for (int i = 1; i <= sheet.UsedRange.Rows.Count; i++) {
Excel.Range range = (Excel.Range)sheet.Rows[i, missing];
// judge whether the row should not be printed out.
if (/*the row should not be printed out*/) {
range.EntireRow.Hidden = true; // hide the entire row
hiddenRowsCount++;
} else {
// adjust the numbering according to the hiddenRowsCount
Excel.Range numberingCell = (Excel.Range)range.Cells[missing,
/*the numbering column*/];
numberingCell.Value2 =
int.Parse(numberingCell.Value2.ToString()) - hiddenRowsCount;
}
}

Step 2: Print the worksheet by calling PrintOut

Step 3: Recover the worksheet by calling Hidden=false and re-adjust the
numbering column:
int hiddenRowsCount = 0; // used to count the hidden rows
Excel.Worksheet sheet = (Excel.Worksheet)document.Worksheets[index];
for (int i = 1; i <= sheet.UsedRange.Rows.Count; i++) {
Excel.Range range = (Excel.Range)sheet.Rows[i, missing];
// judge whether the row should not be printed out.
if (/*the row is hidden*/ range.EntireRow.Hidden) {
range.EntireRow.Hidden = false; // hide the entire row
hiddenRowsCount++;
} else {
// adjust the numbering according to the hiddenRowsCount
Excel.Range numberingCell = (Excel.Range)range.Cells[missing,
/*the numbering column*/];
numberingCell.Value2 =
int.Parse(numberingCell.Value2.ToString()) + hiddenRowsCount;
}
}

Approach 2. If the data in the worksheet is not big, we can copy the data
to a new worksheet, iterate the rows and Delete the rows that should not be
printed out. Then we could call AutoFill to adjust the numbering column and
print the sheet out. Finally, we delete this worksheet.
Excel.Range range = sheet.get_Range("A1", "A2");
Excel.Range destination = sheet.get_Range("A1", "A33");
range.AutoFill(destination,
Microsoft.Office.Interop.Excel.XlAutoFillType.xlFi llDefault);

Please let me know if you have any other concerns, or need anything else.

Sincerely,
Jialiang Ge , remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.