大梦想家

5年开发工程师,2年实施经理,X年售前顾问,......
数据加载中……
EclipseRCP打印表格
    做过RCP项目的朋友应该都遇到过打印的问题,在C/S构架中打印应该是最麻烦的事情。SWT提供的打印功能很简单,特别是在做表格打印的时候,需要大家使用GC自己绘出来,才能打印,对于初级的开发人员和人力不足的公司来说是非常麻烦的事情。
   还好ceclipse.org的老大yipsilon zheng贡献出了一个SWT表格打印的项目,可以做到不同表格的直接打印,而且还有跨行跨列的实现,我改造了一下(把项目做了个转码utf-8==》GBK),然后打成JAR包放上来给大家使用。
 1package com.glnpu.dmp.client.platform.core.internal.util;
 2
 3import org.ceclipse.reporting.IReport;
 4import org.ceclipse.reporting.IReportPage;
 5import org.ceclipse.reporting.Report;
 6import org.ceclipse.reporting.ReportData;
 7import org.ceclipse.reporting.ReportUtil;
 8import org.eclipse.nebula.widgets.grid.Grid;
 9import org.eclipse.swt.printing.PrintDialog;
10import org.eclipse.swt.printing.Printer;
11import org.eclipse.swt.widgets.Table;
12import org.eclipse.ui.PlatformUI;
13
14/**
15 * 通用表格打印组件,目前提供两个方法分别用于打印表格(Gird,Table);
16 * 工作任务名:printContent
17 * @author lign
18 *
19 */

20public class PrintContent {
21
22    /**
23     * 对Gird进行打印操作
24     * @param grid SWT 的nebula项目的Grid
25     * @param title 表头文字描述
26     */

27    public static void printGird(Grid grid, String title) {
28        IReportPage page = ReportUtil.convert(grid, title);
29        Report report = new Report();
30        report.addPage(page);
31        printToPrinter(report);
32        
33    }

34    
35    /**
36     * 对Table进行打印操作
37     * @param table SWT 的Table
38     * @param title 表头文字描述
39     */

40    public static void printTable(Table table, String title) {
41        IReportPage page = ReportUtil.convert(table, title);
42        Report report = new Report();
43        report.addPage(page);
44        printToPrinter(report);
45        
46    }

47    
48    /**
49     * 处理打印以及调用Printer
50     * @param report
51     */

52    private static void printToPrinter(IReport report) {
53         ReportData reportData = report.getReportData();
54         reportData.setJobName("printContent");
55         reportData.setPrinter(new Printer(new PrintDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()).open()));
56         report.print();
57    }

58}

59
我封装了两个方法,一个方法处理SWT提供的Table的打印,另一个处理SWT-星云(Nebula)-Gird的打印,基本上满足我的需要了。
Jar下载:
http://www.blogjava.net/Files/leeguannan/org.ceclipse.printer_1.0.rar

客户虐我千百遍,我待客户如初恋!

posted on 2007-07-20 16:21 阿南 阅读(3077) 评论(7)  编辑  收藏 所属分类: Eclipse-RCP西安java用户群

评论

# re: EclipseRCP打印表格 2007-07-24 10:04 阿源

请问有没有附带源码呢? 我想预览一下,调用report.preview();出错的。
  回复  更多评论    

# re: EclipseRCP打印表格 2007-10-01 12:09 李世虎

我的信息表格
  回复  更多评论    

# re: EclipseRCP打印表格 2008-01-23 21:34 winner

org.eclipse.nebula.widgets.grid.Grid;这个JAR哪儿下载呀,程序老报错。源码传上来看看呀
  回复  更多评论    

# re: EclipseRCP打印表格 2008-05-28 09:32 求助

我想用utf-8的老大能给我个吗

luiguangshui@163.com
  回复  更多评论    

# re: EclipseRCP打印表格 2008-07-07 17:50 huangdan

IReportPage page = ReportUtil.convert(grid, title);
convert(grid, title)这个方法出错了,怎么改?
怎么样使用这个类来进行打印呀?
谢谢!!!
  回复  更多评论    

# re: EclipseRCP打印表格 2008-07-21 23:49 何敏··

谢谢大家把你们的资源共享。
hemin108@126.com
  回复  更多评论    

# re: EclipseRCP打印表格 2010-10-11 16:36 zmh

博主你好,我在调用打印表格的方法时出现如下错误:Exception in thread "main" java.lang.IllegalStateException: Workbench has not been created yet.
at org.eclipse.ui.PlatformUI.getWorkbench(PlatformUI.java:92)
at com.ytkj.kq.print.PrintContent.printToPrinter(PrintContent.java:55)
at com.ytkj.kq.print.PrintContent.printTable(PrintContent.java:44)
at com.ytkj.kq.PersonnelData.PersonnelChange$6.widgetSelected(PersonnelChange.java:404)
at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at com.ytkj.kq.PersonnelData.PersonnelChange.open(PersonnelChange.java:139)
at com.ytkj.kq.Main$14.widgetSelected(Main.java:852)
at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at com.ytkj.kq.Main.<init>(Main.java:278)
at com.ytkj.kq.Main.main(Main.java:243)

能帮忙解决一下吗,希望能将解决方法发到小弟的邮箱:lqmh18@163.com
在此先谢过了
  回复  更多评论    

只有注册用户登录后才能发表评论。


网站导航: