在C#中打印excel(使用Spreadsheetgear生成) [英] Print excel (generated using Spreadsheetgear) in C#

查看:270
本文介绍了在C#中打印excel(使用Spreadsheetgear生成)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Ps-Microsoft Office不可用,使用SpreadSheetGear for

解决方案

如果您有一个UI并且正在使用WorkbookView控件,那么我推荐Chuck的答案。如果您在不使用用户界面的情况下生成此代码,则还可以使用 WorkbookPrintDocument 类。以下是在控制台应用程序中演示的示例:

 使用SpreadsheetGear; 
使用SpreadsheetGear.Printing;
使用SpreadsheetGear.Drawing.Printing;

static void Main(string [] args)
{
//创建一个包含一些单元格数据的工作簿
IWorkbook工作簿= Factory.GetWorkbook();
IWorksheet工作表= workbook.ActiveWorksheet;
IRange cells = worksheet.Cells;
cells [A1:D10]。Value == RAND();

//创建一个WorkbookPrintDocument。
WorkbookPrintDocument printDocument = new WorkbookPrintDocument(工作表,PrintWhat.Sheet);

//如果需要,请设置打印机...让我们去老学校。
printDocument.PrinterSettings.PrinterName =Epson MX-80;

//打印
printDocument.Print();
}

如果您使用SpreadsheetGear 2012的.NET 4.0版本,您将需要添加SpreadsheetGear2012.Drawing.dll程序集的参考访问SpreadsheetGear.Drawing.Printing命名空间。



另请注意,我通过PrintWhat.Sheet指定打印(使用的范围)。请参阅SpreadsheetGear.Printing。 PrintWhat 枚举更多选项。


In c# i am generating an excel in specific format, i have to print the same excel format on click_print.

P.s.-Microsoft Office not available,using SpreadSheetGear for this.

解决方案

I recommend Chuck's answer if you have a UI and are utilizing the WorkbookView control. If you are generating this workbook in-code without a UI, you can also print a workbook by using the WorkbookPrintDocument class. Below is an example that demonstrates this in a console application:

using SpreadsheetGear;
using SpreadsheetGear.Printing;
using SpreadsheetGear.Drawing.Printing;

static void Main(string[] args)
{
    // Create a workbook with some cell data
    IWorkbook workbook = Factory.GetWorkbook();
    IWorksheet worksheet = workbook.ActiveWorksheet;
    IRange cells = worksheet.Cells;
    cells["A1:D10"].Value = "=RAND()";

    // Create a WorkbookPrintDocument.  
    WorkbookPrintDocument printDocument = new WorkbookPrintDocument(worksheet, PrintWhat.Sheet);

    // Set the printer if necessary...let's go old school.
    printDocument.PrinterSettings.PrinterName = "Epson MX-80";

    // Print it
    printDocument.Print();
}

If you are using the .NET 4.0 build of SpreadsheetGear 2012, you'll need to add a reference to the SpreadsheetGear2012.Drawing.dll assembly to access the SpreadsheetGear.Drawing.Printing namespace.

Also note that I specify to print (the used range of) the sheet via PrintWhat.Sheet. See the SpreadsheetGear.Printing.PrintWhat enum for more options.

这篇关于在C#中打印excel(使用Spreadsheetgear生成)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆