Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用互操作打印Excel_C#_Excel_Interop_Printing - Fatal编程技术网

C# 使用互操作打印Excel

C# 使用互操作打印Excel,c#,excel,interop,printing,C#,Excel,Interop,Printing,有人知道如何使用C#和excel互操作程序以编程方式打印excel文件吗?如果是,请提供代码?为了打印,您可以使用该方法。通过传入,可以忽略任何或所有可选参数。如果省略所有这些选项,则默认情况下将从活动打印机打印一份副本。但您可以使用参数设置要打印的副本数、排序规则等。有关更多信息,请参阅该方法的帮助 它们在帮助文件中显示的示例如下: private void PrintToFile() { // Make sure the worksheet has some data before

有人知道如何使用C#和excel互操作程序以编程方式打印excel文件吗?如果是,请提供代码?

为了打印,您可以使用该方法。通过传入,可以忽略任何或所有可选参数。如果省略所有这些选项,则默认情况下将从活动打印机打印一份副本。但您可以使用参数设置要打印的副本数、排序规则等。有关更多信息,请参阅该方法的帮助

它们在帮助文件中显示的示例如下:

private void PrintToFile()
{
    // Make sure the worksheet has some data before printing.
    this.Range["A1", missing].Value2 = "123";
    this.PrintOut(1, 2, 1, false, missing, true, false, missing);
}
但是,除非您需要更改默认设置,否则只需传入所有参数即可。下面是一个使用自动化打开Excel工作簿、打印第一页然后关闭的示例:

void PrintMyExcelFile()
{
    Excel.Application excelApp = new Excel.Application();

    // Open the Workbook:
    Excel.Workbook wb = excelApp.Workbooks.Open(
        @"C:\My Documents\Book1.xls",
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
        Type.Missing, Type.Missing,Type.Missing,Type.Missing);

    // Get the first worksheet.
    // (Excel uses base 1 indexing, not base 0.)
    Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];

    // Print out 1 copy to the default printer:
    ws.PrintOut(
        Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
        Type.Missing, Type.Missing, Type.Missing, Type.Missing);

    // Cleanup:
    GC.Collect();
    GC.WaitForPendingFinalizers();

    Marshal.FinalReleaseComObject(ws);

    wb.Close(false, Type.Missing, Type.Missing);
    Marshal.FinalReleaseComObject(wb);

    excelApp.Quit();
    Marshal.FinalReleaseComObject(excelApp);
}
希望这有帮助


Mike

重要的改进是选择打印机的代码,例如:

var printers = System.Drawing.Printing.PrinterSettings.InstalledPrinters;

int printerIndex = 0;

foreach(String s in printers)
{
    if (s.Equals("Name of Printer"))
    {
        break;
    }
    printerIndex++;
}

xlWorkBook.PrintOut(Type.Missing, Type.Missing, Type.Missing, Type.Missing,printers[printerIndex], Type.Missing, Type.Missing, Type.Missing);

所有已经给出的答案都很好,但我只是觉得我可以让它更简单,有更多的选项来显示对话框和定义打印页面的方向

private void PrintExcel()
{
    string filePath = "C:\file\location\here\";
            Excel.Application excelApp = new Excel.Application();

    // Open Workbook:
    Excel.Workbook wb = excelApp.Workbooks.Open(filePath);

    // Define the orientation for the page
    ((Excel._Worksheet)wb.ActiveSheet).PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape;

    //Decide which worksheet to print
    Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets[1];

    // Option to print with or to show dialogue box
    bool userDidntCancel = excelApp.Dialogs[Excel.XlBuiltInDialog.xlDialogPrint].Show();

    // Option to print out wihtout the dialogue box. 
    // WARNING: Do not use Dialogue option and this at the same time. 
    // It will print the page even if you cancel the dialogue print option.
    ws.PrintOut();

    // Cleanup your code
    GC.Collect();
    GC.WaitForPendingFinalizers();

    Marshal.FinalReleaseComObject(ws);

    wb.Close(false, Type.Missing, Type.Missing);
    Marshal.FinalReleaseComObject(wb);

    // Close/Exit File
    excelApp.Quit();
    Marshal.FinalReleaseComObject(excelApp);

}

我希望这一点能帮助别人D

你不会碰巧知道如何显示打印对话,而不是直接打印文档……是吗?是的,但我认为你应该为此问另一个问题。答案需要一点解释,命令本身在注释中看起来很糟糕(有30个可选参数,您必须使用30个Type.Missings)。这是一个简单的答案,但我认为在评论中插入内容有点太多了。。。所以,开始一个新的Q,我会寻找它名称空间:System.Runtime.InteropServices Hi John,右,“System.Runtime.InteropServices”名称空间用于“Marshal.FinalEleaseComObject”调用,以清理最后的内存。任何“PrintOut”方法或对Excel对象模型的任何其他调用都不需要它。很好,谢谢。这正是我需要做的,但我需要在Excel中用PDF打印机打印单独的工作表。然而,每次我这样做,PDF都会以某种方式损坏。我试过很多PDF打印机(Adobe、PDF955、PDFCreator、AMYUNI),它们似乎都能做同样的事情。我使用了您的示例,但是将printername、filename和printofile标志设置为true没有成功。你知道我该怎么做吗?