用户操作
[留言]  [发消息]  [加为好友] 
订阅我的博客
XML聚合    FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
ejunnet的公告
<script type="text/javascript"><!-- google_ad_client = "pub-5804414361724728"; //160x600, 创建于 07-12-15 google_ad_slot = "0605332833"; google_ad_width = 160; google_ad_height = 600; //--></script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
文章分类
友情联接
存档

原创  GWT 调用本地js 导出excel 收藏

    刚做的用GWT调用本地js导出excel , 导出的excel有点难看, 但总比没有导出功能好!

 

    第一步: 编写本地js放到public/js/file/FileExport.js目录下:

  1.  //导入Excel
  2.  function ExportToExcel(element) 
  3.  {
  4.   var oXL = new ActiveXObject("Excel.Application"); 
  5.   var oWB = oXL.Workbooks.Add(); 
  6.   var oSheet = oWB.ActiveSheet;  
  7.   var sel=document.body.createTextRange();
  8.   sel.moveToElementText(element);
  9.   sel.select();
  10.   sel.execCommand("Copy");
  11.   oSheet.Paste();
  12.   oXL.Visible = true;
  13.  }
  14.  //导入Word
  15.  function ExportToWord(element)
  16.  {
  17.   var oWD = new ActiveXObject("Word.Application");
  18.   var oDC = oWD.Documents.Add("",0,1);
  19.   var oRange =oDC.Range(0,1);
  20.   var sel = document.body.createTextRange();
  21.   sel.moveToElementText(element);
  22.   sel.select();
  23.   sel.execCommand("Copy");
  24.   oRange.Paste();
  25.   oWD.Application.Visible = true;
  26.  }

   第二步: 编写JSNI实现类来调用js

  1. package com.gogocode.bi.client;
  2. import com.google.gwt.user.client.Element;
  3. /**
  4.  * JSNI实现类调用js 
  5.  * @author 赵昌峻
  6.  *
  7.  */
  8. public class FileExport {
  9.     public static native void ExportToExcel(Element element) /*-{
  10.       $wnd.ExportToExcel(element);
  11.     }-*/;
  12.     
  13.     public static native void ExportToWord(Element element) /*-{
  14.       $wnd.ExportToWord(element);
  15.     }-*/;
  16. }

第三步: 在程序中调用JSNI实现类

  1. package com.gogocode.bi.client;
  2. import com.google.gwt.core.client.EntryPoint;
  3. import com.google.gwt.user.client.DOM;
  4. import com.google.gwt.user.client.ui.RootPanel;
  5. import com.gwtext.client.core.EventObject;
  6. import com.gwtext.client.data.ArrayReader;
  7. import com.gwtext.client.data.DateFieldDef;
  8. import com.gwtext.client.data.FieldDef;
  9. import com.gwtext.client.data.FloatFieldDef;
  10. import com.gwtext.client.data.MemoryProxy;
  11. import com.gwtext.client.data.RecordDef;
  12. import com.gwtext.client.data.Store;
  13. import com.gwtext.client.data.StringFieldDef;
  14. import com.gwtext.client.widgets.Button;
  15. import com.gwtext.client.widgets.Panel;
  16. import com.gwtext.client.widgets.Toolbar;
  17. import com.gwtext.client.widgets.ToolbarButton;
  18. import com.gwtext.client.widgets.event.ButtonListenerAdapter;
  19. import com.gwtext.client.widgets.grid.ColumnConfig;
  20. import com.gwtext.client.widgets.grid.ColumnModel;
  21. import com.gwtext.client.widgets.grid.GridPanel;
  22. /**
  23.  * GWT 调用本地js 导出excel 示例程序
  24.  * @author 赵昌峻
  25.  *
  26.  */
  27. public class BI_Enter implements EntryPoint {
  28.     public void onModuleLoad() {   
  29.         Panel panel = new Panel();   
  30.         panel.setBorder(false);   
  31.         panel.setPaddings(15);   
  32.   
  33.         RecordDef recordDef = new RecordDef(   
  34.                 new FieldDef[]{   
  35.                         new StringFieldDef("company"),   
  36.                         new FloatFieldDef("price"),   
  37.                         new FloatFieldDef("change"),   
  38.                         new FloatFieldDef("pctChange"),   
  39.                         new DateFieldDef("lastChanged""n/j h:ia"),   
  40.                         new StringFieldDef("symbol"),   
  41.                         new StringFieldDef("industry")   
  42.                 }   
  43.         );   
  44.   
  45.         final GridPanel grid = new GridPanel();   
  46.         grid.setId("grid"); //给要导出的表格一个维一的ID
  47.   
  48.         Object[][] data = getCompanyData();   
  49.         MemoryProxy proxy = new MemoryProxy(data);   
  50.   
  51.         ArrayReader reader = new ArrayReader(recordDef);   
  52.         Store store = new Store(proxy, reader);   
  53.         store.load();   
  54.         grid.setStore(store);   
  55.   
  56.   
  57.         ColumnConfig[] columns = new ColumnConfig[]{   
  58.                 new ColumnConfig("股票""company"130truenull"company"),   
  59.                 new ColumnConfig("价格""price"35),   
  60.                 new ColumnConfig("涨跌""change"45),   
  61.                 new ColumnConfig("涨幅""pctChange"65),   
  62.                 new ColumnConfig("最后更新时间""lastChanged"100)
  63.         };   
  64.   
  65.         ColumnModel columnModel = new ColumnModel(columns);   
  66.         grid.setColumnModel(columnModel);   
  67.   
  68.         grid.setFrame(true);   
  69.         grid.setStripeRows(true);   
  70.         grid.setAutoExpandColumn("company");   
  71.   
  72.         grid.setHeight(350);   
  73.         grid.setWidth(600);   
  74.         grid.setTitle("今日股市情况");   
  75.   
  76.         Toolbar bottomToolbar = new Toolbar();   
  77.         bottomToolbar.addFill();   
  78.         bottomToolbar.addButton(new ToolbarButton("导出到Excel"new ButtonListenerAdapter() {   
  79.             public void onClick(Button button, EventObject e) {
  80.                 FileExport.ExportToExcel(DOM.getElementById("grid"));  //调用本地js导出excel
  81.                 grid.clearSortState(true);   
  82.             }   
  83.         }));   
  84.         grid.setBottomToolbar(bottomToolbar);   
  85.   
  86.         panel.add(grid);   
  87.   
  88.         RootPanel.get().add(panel);   
  89.     }   
  90.     
  91.     /**
  92.      * 得到测试数据
  93.      * @return
  94.      */
  95.     private Object[][] getCompanyData() {   
  96.         return new Object[][]{   
  97.                 new Object[]{"Google"new Double(71.72), new Double(0.02),   
  98.                         new Double(0.03), "9/1 12:00am""MMM""Manufacturing"},   
  99.                 new Object[]{"Alcoa Inc"new Double(29.01), new Double(0.42),   
  100.                         new Double(1.47), "9/1 12:00am""AA""Manufacturing"},   
  101.                 new Object[]{"Altria Group Inc"new Double(83.81), new Double(0.28),   
  102.                         new Double(0.34), "9/1 12:00am""MO""Manufacturing"},   
  103.                 new Object[]{"American Express Company"new Double(52.55), new Double(0.01),   
  104.                         new Double(0.02), "9/1 12:00am""AXP""Finance"},   
  105.                 new Object[]{"American International Group, Inc."new Double(64.13), new Double(0.31),   
  106.                         new Double(0.49), "9/1 12:00am""AIG""Services"},   
  107.                 new Object[]{"AT&T Inc."new Double(31.61), new Double(-0.48),   
  108.                         new Double(-1.54), "9/1 12:00am""T""Services"},   
  109.                 new Object[]{"Boeing Co."new Double(75.43), new Double(0.53),   
  110.                         new Double(0.71), "9/1 12:00am""BA""Manufacturing"},   
  111.                 new Object[]{"Caterpillar Inc."new Double(67.27), new Double(0.92),   
  112.                         new Double(1.39), "9/1 12:00am""CAT""Services"},   
  113.                 new Object[]{"Yahoo"new Double(49.37), new Double(0.02),   
  114.                         new Double(0.04), "9/1 12:00am""C""Finance"},   
  115.                 new Object[]{"E.I. du Pont de Nemours and Company"new Double(40.48), new Double(0.51),   
  116.                         new Double(1.28), "9/1 12:00am""DD""Manufacturing"}   
  117.         };   
  118.     }   
  119. }

最终效果如下图所示:

 

说明: 代码中用到了GWT-EXT,项目主页:http://gwt-ext.com/

 

转载请注明出处和作者信息

 

发表于 @ 2008年08月27日 15:23:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:简简单单实现Ajax | 新一篇:Ejunnet博客搬家公告

  • 发表评论
  • 评论内容:
  •  
Copyright © ejunnet
Powered by CSDN Blog