Excel Interop打印

Mem*_*hiZ 7 c# printing excel interop

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

打印机:Microsoft XPS文档编写器
打印选择
横向方向
A4
正常边距
适合一页的页面

如何使用_Worksheet.PrintOut或_Worksheet.PrintOutEx实现此目的?

提前致谢!

Sid*_*out 14

试试这个(经过试验和测试)

我假设你已经设置了对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;
Run Code Online (Sandbox Code Playgroud)

这将在代码的后半部分进行.

// 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;
Run Code Online (Sandbox Code Playgroud)

  • 您可以使用`Defprinter = xlexcel.ActivePrinter;`找到它,然后在即时窗口中打印它.该代码将告诉您什么是您的活动打印机.要测试它,请将打印机更改为XPS,然后运行上面的代码. (2认同)