【问题标题】:Printing Excel 2003 sheet using c#使用 c# 打印 Excel 2003 工作表
【发布时间】:2013-09-06 20:09:27
【问题描述】:

我正在尝试使用 c# 以编程方式打印 excel (2003) 工作表。我可能需要添加一些参考,但是在进行了一些搜索之后,即使有几个建议的参考,我也无法弄清楚要使用什么代码,因为我尝试过的所有东西都因为某种原因不起作用,所以我什至没有有任何示例代码可以发布以显示我到目前为止所拥有的内容。我基本上不知道如何让我的程序打印一个选定的 Excel 表,所以任何帮助都会很棒。

提前致谢。

【问题讨论】:

  • 我不能 100% 确定这是否能实现我想要的,但无论哪种方式,我应该将 xlexcel 和 xlWorkBook 设置为什么?我知道我会将 xlWorkSheet 设置为工作表名称,将 xlRange 设置为我要打印的单元格范围,并且可能将 xlWorkBook 设置为我的工作簿的路径,但我不确定要为 xlexcel 做什么。

标签: c# printing excel-2003


【解决方案1】:

所以基本上我想出的最好方法是使用 Microsoft.Office.Interop.Excel

object misValue = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb = excelApp.Workbooks.Open(Form1.excelPath, misValue, misValue, misValue, 
    misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.Worksheets[1];

//bring about print dialogue
bool userDidntCancel = excelApp.Dialogs[Microsoft.Office.Interop.Excel.XlBuiltInDialog.xlDialogPrint].Show(misValue,
    misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue,
    misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue,
    misValue, misValue, misValue, misValue, misValue);

userDidntCancel 会产生打印对话框,并将返回 true 或 false,指示用户在打印对话框中是否按下了“PRINT”或“CANCEL”,您可以使用或不使用。我更喜欢这种方法,因为它允许用户更改打印机和其他属性,而且我认为这种方法要好得多。

我发现的唯一问题是它会打印上次手动打开和保存的工作表。例如,就我而言,我的程序正在对工作表进行一些更改,然后将其保存。但是,如果用户在程序外部手动打开工作表并将某些内容保存在另一个工作表上,我的程序将打印该工作表而不是它刚刚编辑的工作表。我似乎无法弄清楚如何解决这个问题,所以如果有人有任何建议,请随时发表评论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-16
    • 1970-01-01
    • 2014-01-13
    • 2017-08-02
    • 2012-10-02
    • 2010-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多