c# - Excel 互操作打印

标签 c# excel printing interop

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

打印机:Microsoft XPS Document Writer
打印选择
横向
A4
正常边距
使工作表适合一页

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

提前致谢!

最佳答案

我假设您已经设置了对 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;

关于c# - Excel 互操作打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10900565/

相关文章:

html - A4 页面像 html 中的布局

c# - XAML 如何设置复选框的框颜色样式?

C# 如何使用存储在构造函数变量中的类名创建新对象?

excel - 检查单元格范围内的特定值并在辅助列中输出 'TRUE' 的函数

javascript - 求助: printing multiple "reports" from browser (IE 6 essential, 其他的就好了)

java - 显示数组中包含的值

c# - 解压错误/压缩错误

c# - 如何生成下一个主键值

Python - Win32Com - 如何从 Excel 电子表格单元格中提取超链接?

excel - 使 Excel 选项卡不打印?