Advertisement
Guest User

warz

a guest
Apr 9th, 2010
769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.03 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. using ExcelInterop = Microsoft.Office.Interop.Excel;
  5.  
  6. namespace UniDocConverter.FileTypes.Office
  7. {
  8.     public static class Excel
  9.     {
  10.         public static void ToTiff(PrinterDriver printer, String input, String output)
  11.         {
  12.             // Create a Excel's Application object
  13.             ExcelInterop.Application ExcelApp = new ExcelInterop.ApplicationClass();
  14.  
  15.             Object ReadOnly = true;
  16.             Object Missing = Type.Missing;
  17.             Object Preview = false;
  18.             Object SaveChanges = false;
  19.  
  20.             // Open the document from a file
  21.             ExcelInterop.Workbook Workbook = ExcelApp.Workbooks.Open(input, 2, ReadOnly, Missing, Missing, Missing, true, Missing, Missing, Missing, Missing, Missing, Missing, Missing, true);
  22.            
  23.             // Set printer settings for excel files as specified in standards doc
  24.             printer.Orientation = UDC.PageOrientationID.PO_LANDSCAPE;
  25.  
  26.             // Loop through each worksheet
  27.             foreach (ExcelInterop.Worksheet Worksheet in Workbook.Worksheets)
  28.             {
  29.                 Console.WriteLine("Processing worksheet {0} ({1})", Worksheet.Index, Worksheet.Name);
  30.  
  31.                 // Activate this sheet
  32.                 Worksheet.Activate();
  33.  
  34.                 // Grab the page setup object
  35.                 Microsoft.Office.Interop.Excel.PageSetup PageSetup = Worksheet.PageSetup;
  36.  
  37.                 // Set specific settings
  38.                 PageSetup.Zoom = 80;
  39.                 PageSetup.PrintTitleRows = "";
  40.                 PageSetup.PrintTitleColumns = "";
  41.                 PageSetup.Order = Microsoft.Office.Interop.Excel.XlOrder.xlOverThenDown;
  42.                 PageSetup.PaperSize = Microsoft.Office.Interop.Excel.XlPaperSize.xlPaperLetter;
  43.                 PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;
  44.                 PageSetup.PrintComments = Microsoft.Office.Interop.Excel.XlPrintLocation.xlPrintNoComments;
  45.                 PageSetup.BlackAndWhite = true;
  46.                 PageSetup.PrintHeadings = true;
  47.                 PageSetup.CenterHorizontally = true;
  48.                 PageSetup.CenterVertically = true;
  49.  
  50.                 Console.WriteLine(" - Footer: {0}", PageSetup.RightFooter);
  51.  
  52.                 // Force the sheet to be visible
  53.                 Worksheet.Visible = Microsoft.Office.Interop.Excel.XlSheetVisibility.xlSheetVisible;
  54.  
  55.                 // Explicitly set the PrintArea to the UsedRange
  56.                 String formula = Worksheet.UsedRange.get_AddressLocal(Worksheet.UsedRange.Rows.Count, Worksheet.UsedRange.Columns.Count,
  57.                                                       Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1, null, Worksheet.UsedRange);
  58.                 PageSetup.PrintArea = formula;
  59.                 Console.WriteLine(" - Used range: {0}", PageSetup.PrintArea);
  60.  
  61.                 // Attempt to force the printer to use 300 dpi quality
  62.                 Int32 quality_x = Int32.Parse("" + Worksheet.PageSetup.get_PrintQuality(1));
  63.                 Int32 quality_y = Int32.Parse("" + Worksheet.PageSetup.get_PrintQuality(2));
  64.                 if ((quality_x != 200) || (quality_y != 200))
  65.                 {
  66.                     try
  67.                     {
  68.                         object[] x = new Object[] { 1, 2 };
  69.                         object[] y = new Object[] { 200, 200 };
  70.                         PageSetup.set_PrintQuality(x, y);
  71.  
  72.                         Console.WriteLine(" - Print quality adjusted from {0}x{1} to {2}x{3}", quality_x, quality_y,
  73.                                                                                             PageSetup.get_PrintQuality(1),
  74.                                                                                             PageSetup.get_PrintQuality(2));
  75.                     }
  76.                     catch (Exception ex)
  77.                     {
  78.                         Console.WriteLine(" - Unable to adjust print quality: {0}", ex.Message);
  79.                     }
  80.                 }
  81.  
  82.                 // Select the used range
  83.                 try
  84.                 {
  85.                     Worksheet.UsedRange.Select();
  86.                 }
  87.                 catch (Exception)
  88.                 {
  89.                     //Console.WriteLine(" - Caught except while selecting used range on {0}", Worksheet.Name);
  90.                 }
  91.  
  92.                 // Try to ungroup any groupings
  93.                 try
  94.                 {
  95.                     Worksheet.UsedRange.Ungroup();
  96.                 }
  97.                 catch (Exception)
  98.                 {
  99.                     //Console.WriteLine(" - Caught exception while ungrouping items on {0}", Worksheet.Name);
  100.                 }
  101.  
  102.                 // Try to unhide all rows and columns
  103.                 try
  104.                 {
  105.                     Worksheet.UsedRange.EntireColumn.Hidden = false;
  106.                     Worksheet.UsedRange.EntireRow.Hidden = false;
  107.                 }
  108.                 catch (Exception)
  109.                 {
  110.                     //Console.WriteLine(" - Caught exception while unhiding rows and columns on {0}", Worksheet.Name);
  111.                 }
  112.  
  113.                 // Try to apply autofit to rows and columns
  114.                 try
  115.                 {
  116.                     Worksheet.UsedRange.EntireRow.AutoFit();
  117.                     Worksheet.UsedRange.EntireColumn.AutoFit();
  118.                 }
  119.                 catch (Exception)
  120.                 {
  121.                     //Console.WriteLine(" - Caught exception while applying autofit to rows and columns on {0}", Worksheet.Name);
  122.                 }
  123.  
  124.                 // Try to apply wrap and disable shrink
  125.                 try
  126.                 {
  127.                     Worksheet.UsedRange.WrapText = true;
  128.                     Worksheet.UsedRange.ShrinkToFit = false;
  129.                 }
  130.                 catch (Exception)
  131.                 {
  132.                     //Console.WriteLine(" - Caught exception while applying wrap and shrink on {0}", Worksheet.Name);
  133.                 }
  134.  
  135.                 // Attempt to replace certain key words within the excel file.
  136.                 try
  137.                 {
  138.                     ExcelApp.DisplayAlerts = false;
  139.                     if (Worksheet.UsedRange.Find("=Today()", Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlLookAt.xlWhole, Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, false, Type.Missing, Type.Missing) != null)
  140.                         Worksheet.UsedRange.Replace("=Today()", "", Type.Missing, Type.Missing, false, false, Type.Missing, Type.Missing);
  141.  
  142.                     if (Worksheet.UsedRange.Find("=Now()", Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlLookAt.xlWhole, Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, false, Type.Missing, Type.Missing) != null)
  143.                         Worksheet.UsedRange.Replace("=Now()", "", Type.Missing, Type.Missing, false, false, Type.Missing, Type.Missing);
  144.  
  145.                     if (Worksheet.UsedRange.Find("=CELL(\"filename\")", Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlLookAt.xlWhole, Microsoft.Office.Interop.Excel.XlSearchOrder.xlByRows, Microsoft.Office.Interop.Excel.XlSearchDirection.xlNext, false, Type.Missing, Type.Missing) != null)
  146.                         Worksheet.UsedRange.Replace("=CELL(\"filename\")", "", Type.Missing, Type.Missing, false, false, Type.Missing, Type.Missing);
  147.                 }
  148.                 catch (Exception)
  149.                 {
  150.                     //Console.WriteLine(" - Caught exception while replacing cell contents on {0}", Worksheet.Name);
  151.                 }
  152.             }
  153.  
  154.             // Send the workbook to the printer driver
  155.             Workbook.PrintOut(Missing, Missing, Missing, Preview, "Universal Document Converter", Missing, Missing, Missing);
  156.  
  157.             //Close the spreadsheet without saving changes, and close Excel
  158.             Workbook.Close(SaveChanges, Missing, Missing);
  159.             ExcelApp.Quit();
  160.         }
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement