在C#中打印excel(使用Spreadsheetgear生成)

在C#中我生成特定格式的Excel,我必须在click_print上打印相同的Excel格式。

Ps-Microsoft Office不可用,使用SpreadSheetGear进行此操作。

如果您有一个用户界面,并且正在使用WorkbookView控件,我build议查克的答案。 如果您在没有UI的情况下生成此工作簿代码,则还可以使用WorkbookPrintDocument类打印工作簿 。 下面是一个在控制台应用程序中演示的例子:

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(); } 

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

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

Spreadsheetgear为它们提供的对象提供了一个打印方法。

http://www.spreadsheetgear.com/support/help/spreadsheetgear.net.3.0/SpreadsheetGear~SpreadsheetGear.Windows.Forms.WorkbookView~Print.html

这应该让你开始。