package com.easyway.tbs.commons.ext.views.printer;
import org.ceclipse.reporting.IReport;
import org.ceclipse.reporting.IReportPage;
import org.ceclipse.reporting.Report;
import org.ceclipse.reporting.ReportData;
import org.ceclipse.reporting.ReportUtil;
import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.swt.printing.PrintDialog;
import org.eclipse.swt.printing.Printer;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.PlatformUI;
/**
* 采用网上流传的ceclipse.org的组件
* 通用的表格打印组件,目前提供两个方法分别用于打印表格(Grid,Table)
* 我封装了两个方法,一个方法处理SWT提供的Table的打印,另一个处理SWT-星云(Nebula)-Gird的打印,
* 基本上满足我的需要了。贡献出了一个SWT表格打印的项目,可以做到不同表格的直接打印,而且还有跨行跨列的实现.
* Jar下载:
* http://www.blogjava.net/Files/leeguannan/org.ceclipse.printer_1.0.rar
*
* @author longgangbai
*
*/
public class PrintTools {
/**
* 对SWT Table进行打印的操纵
* @param table SWT Table 的对象
* @param title 表头文字描述
*/
public static void printTable(Table table, String title) {
IReportPage page = ReportUtil.convert(table, title);
Report report = new Report();
report.addPage(page);
printToPrinter(report);
}
/**
* 对SWT Gird进行打印的操纵
* @param gird SWT Gird 的对象
* @param title 表头文字描述
*/
public static void printGird(Grid grid, String title) {
IReportPage page = ReportUtil.convert(grid, title);
Report report = new Report();
report.addPage(page);
printToPrinter(report);
printToPrinter(report);
}
/**
* 处理打印以及调用Printer
*@param report
*/
private static void printToPrinter(IReport report) {
ReportData reportData = report.getReportData();
reportData.setJobName("printContent");
reportData.setPrinter(new Printer(new PrintDialog(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell()).open()));
report.print();
}
}
局限性:这个功能在打印的table或者Grid使用滚动条,但滚动条没有出现时打印正常。如果数据过多在表格中不能完全显示那么就不能满足了,最终采用报表工具实现打印报表功能。