2012-06-05 42 views
5

我需要使用下面的打印设置打印Excel工作表的选定区域(我与Range.Select()选择):Excel的互操作打印

打印机:微软XPS文档写入
打印选择
横向
A4
正常边距
在一页良券

我怎样才能做到这一点使用_Worksheet.PrintOut或_Worksheet.PrintOutEx?

在此先感谢!

回答

13

试试这个(久经考验

我假设你已经设置参考Excel和已申报的对象,如

Microsoft.Office.Interop.Excel.Application xlexcel; 
Microsoft.Office.Interop.Excel.Workbook xlWorkBook; 
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet; 
Microsoft.Office.Interop.Excel.Range xlRange; 
object misValue = System.Reflection.Missing.Value; 

这正好中的代码的最新部分。

// Get the current printer 
string Defprinter = null; 
Defprinter = xlexcel.ActivePrinter; 

// Set the printer to Microsoft XPS Document Writer 
xlexcel.ActivePrinter = "Microsoft XPS Document Writer on Ne01:"; 

// Setup our sheet 
var _with1 = xlWorkSheet.PageSetup; 
// A4 papersize 
_with1.PaperSize = Excel.XlPaperSize.xlPaperA4; 
// Landscape orientation 
_with1.Orientation = Excel.XlPageOrientation.xlLandscape; 
// Fit Sheet on One Page 
_with1.FitToPagesWide = 1; 
_with1.FitToPagesTall = 1; 
// Normal Margins 
_with1.LeftMargin = xlexcel.InchesToPoints(0.7); 
_with1.RightMargin = xlexcel.InchesToPoints(0.7); 
_with1.TopMargin = xlexcel.InchesToPoints(0.75); 
_with1.BottomMargin = xlexcel.InchesToPoints(0.75); 
_with1.HeaderMargin = xlexcel.InchesToPoints(0.3); 
_with1.FooterMargin = xlexcel.InchesToPoints(0.3); 

// Print the range 
xlRange.PrintOutEx(misValue, misValue, misValue, misValue, 
misValue, misValue, misValue, misValue); 

// Set printer back to what it was 
xlexcel.ActivePrinter = Defprinter; 
+0

非常感谢您!但你怎么知道“在Ne01上”? – MemphiZ

+2

您可以使用'Defprinter = xlexcel.ActivePrinter;'找到它,然后将它打印在即时窗口中。该代码会告诉你什么是你的活动打印机。要测试它,请将打印机更改为XPS,然后运行上述代码。 –

+1

我发现这种方式来检测使用哪个Ne端口:http://stackoverflow.com/questions/5424932/how-to-determine-what-ne-port-the-adobe-pdf-printer-is-on。除了pageSetup.Zoom必须设置为false以使FitToPagesWide/Tall工作并使用range.ExportAsFixedFormat(XlFixedFormatType.xlTypeXPS,outputPath);而不是PrintOutEx。也许你可以更新你的代码:) – MemphiZ

0

对于'单页上的匹配页'的工作,我们也应该将缩放属性设置为false。在一页

//良券

_with1.FitToPagesWide = 1; 

_with1.FitToPagesTall = 1; 

_with1.Zoom = False;