守望者--AIR技术交流

标题: OfficeUtil 使用示例 [打印本页]

作者: 破晓    时间: 2015-1-12 09:31
标题: OfficeUtil 使用示例
直接上代码:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:Window xmlns:fx="http://ns.adobe.com/mxml/2009"
  3.                   xmlns:s="library://ns.adobe.com/flex/spark"
  4.                   xmlns:mx="library://ns.adobe.com/flex/mx" width="600" height="600"
  5.                   title="Excel解析"
  6.                   creationComplete="init()"
  7.                   showStatusBar="true"
  8.                   initialize="statusText.setStyle('fontSize', 12)"
  9.                   status="开源地址:http://www.airmyth.com/forum-52-1.html;QQ交流群:272732356">
  10.         <fx:Declarations>
  11.                 <!-- 将非可视元素(例如服务、值对象)放在此处 -->
  12.         </fx:Declarations>
  13.        
  14.         <fx:Script>
  15.                 <![CDATA[
  16.       import com.ms.office.core.OfficeBase;
  17.       import com.ms.office.core.vo.InformationVo;
  18.       import com.ms.office.excel.Cell;
  19.       import com.ms.office.excel.Excel;
  20.       import com.ms.office.excel.Sheet;
  21.       
  22.       import mx.utils.ObjectUtil;
  23.                        
  24.                        
  25.                         private const FILE_TYPE_LIST:Array = ["xlsx", "docx", "pptx"];
  26.                        
  27.                         [Bindable]
  28.                         private var arrTree:Array = [];
  29.                         /**已读取的所有文件  */
  30.                         private var officeFileList:Dictionary = new Dictionary();
  31.                        
  32.                         /**当前选中的文件  */
  33.                         private var selectedFile:File;
  34.                         private var load:URLLoader;
  35.                        
  36.                         /**加载队列  */
  37.                         private var loadingList:Array = [];
  38.                         private var isLoading:Boolean;
  39.                        
  40.                         private function init():void
  41.                         {
  42.                                 load = new URLLoader();
  43.                                 load.addEventListener(Event.COMPLETE, onComplete);
  44.                                 load.dataFormat = URLLoaderDataFormat.BINARY;
  45.                                
  46.                                 this.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER,onDragIn);//通常做拖入文件的类型检查               
  47.                                 this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP,onDrop);//拖拽完成事件
  48.                         }
  49.                        
  50.                         private function onDragIn(event:NativeDragEvent):void
  51.                         {
  52.                                 var transferable:Clipboard = event.clipboard;
  53.                                 if(transferable.hasFormat(ClipboardFormats.FILE_LIST_FORMAT))
  54.                                 {
  55.                                         var isAccept:Boolean = true;
  56.                                         var files:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
  57.                                         for each(var fi:File in files)
  58.                                         {
  59.                                                 if(fi.isDirectory || FILE_TYPE_LIST.indexOf(fi.extension) < 0)
  60.                                                 {
  61.                                                         isAccept = false;
  62.                                                         break;
  63.                                                 }
  64.                                         }
  65.                                         if(isAccept)
  66.                                                 NativeDragManager.acceptDragDrop(this);
  67.                                 }
  68.                         }
  69.                        
  70.                         private function onDrop(event:NativeDragEvent):void{
  71.                                 loadingList = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
  72.                                
  73.                                 loadFile();
  74.                         }
  75.                        
  76.                         private function loadFile():void
  77.                         {
  78.                                 if(loadingList.length == 0) return;
  79.                                 selectedFile = loadingList[0];
  80.                                
  81.                                 isLoading = true;
  82.                                 load.load(new URLRequest(selectedFile.nativePath));
  83.                         }

  84.                         protected function onComplete(event:Event):void
  85.                         {
  86.                                 loadingList.shift();
  87.                                 var byte:ByteArray = load.data as ByteArray;
  88.                                 var office:OfficeBase = new Excel(byte);
  89.                                 var info:InformationVo = office.fileInformation;
  90.                                 if(selectedFile)
  91.                                         info.setFileData(selectedFile);
  92.                                
  93.                                 ui.visible = false;
  94.                                 txtInfo.visible = true;
  95.                                 txtInfo.text = ObjectUtil.toString(info);
  96.                                
  97.                                 var obj:Object = {name:selectedFile.name, type:"file", children:[]};
  98.                                 if(selectedFile.extension == "xlsx")
  99.                                 {
  100.                                         var sheetArr:Array = (office as Excel).getSheetNameArray();
  101.                                         for each(var si:String in sheetArr)
  102.                                         {
  103.                                                 obj.children.push({name:si, type:"sheet"});
  104.                                         }
  105.                                 }
  106.                                 else if(selectedFile.extension == "docx")
  107.                                 {
  108.                                        
  109.                                 }
  110.                                 else if(selectedFile.extension == "pptx")
  111.                                 {
  112.                                        
  113.                                 }
  114.                                
  115.                                 obj.icon = selectedFile.icon.bitmaps[0];
  116.                                 arrTree.push(obj);
  117.                                 tree.dataProvider = arrTree;
  118.                                
  119.                                 officeFileList[selectedFile.name] = office;
  120.                                
  121.                                 isLoading = false;
  122.                                
  123.                                 loadFile();
  124.                         }
  125.                        
  126.                         protected function treeItemClick(event:MouseEvent):void
  127.                         {
  128.                                 var obj:Object = tree.selectedItem;
  129.                                 if(obj.type == "file")
  130.                                 {
  131.                                         ui.visible = false;
  132.                                         txtInfo.visible = true;
  133.                                         txtInfo.text = ObjectUtil.toString(officeFileList[obj.name].fileInformation);
  134.                                 }
  135.                                 else if(obj.type == "sheet")
  136.                                 {
  137.                                         ui.visible = true;
  138.                                         txtInfo.visible = false;
  139.                                         var se:Excel = officeFileList[tree.getParentItem(obj).name] as Excel;
  140.                                         showExeclFile(se.getSheetByName(obj.name));
  141.                                 }
  142.                         }
  143.                        
  144.                         private function showExeclFile(sheet:Sheet):void
  145.                         {
  146.         sheet.sheetVo.name = "改完了";
  147. //        sheet.addRow(6, ["123", "dfsgd", null, "89", null, null, 123456]);
  148. //        sheet.addCell("H8", "dsfhsj");
  149.         sheet.addTabel([
  150.           ["123", "dfsgd", null, "89", null, null, 123456],
  151.           ["123", "dfsgd", null, "89", null, null, 123456],
  152.           ["123", "dfsgd", null, "89", null, null, 123456],
  153.           ["123", "dfsgd", null, "89", null, null, 123456],
  154.           ["123", "dfsgd", null, "89", null, null, 123456],
  155.           ["123", "dfsgd", null, "89", null, null, 123456],
  156.           ["123", "dfsgd", null, "89", null, null, 123456],
  157.           ["123", "dfsgd", null, "89", null, null, 123456]
  158.         ], 5, 5)
  159.                                 while(ui.numChildren)
  160.                                 {
  161.                                         ui.removeChildAt(0);
  162.                                 }
  163.                                 if(!sheet) return;
  164.                                
  165.                                 var tx:int = 10;
  166.                                 var ty:int = 10;
  167.                                 var item:TextField;
  168.                                 var items:Dictionary = new Dictionary();
  169.                                 for(var i:int=sheet.minRowIndex; i<=sheet.maxRowIndex; i++)
  170.                                 {
  171.                                         var row:Array = sheet.getRowAt(i);
  172.                                         for each(var cell:Cell in row)
  173.                                         {
  174.                                                
  175.                                                 item = new TextField();
  176.                                                 if(cell)
  177.                                                 {
  178.                                                         item.text = String(cell.value==null?"":cell.value);
  179.                                                         items[cell.cellPosition.rowIndex + "#" + cell.cellPosition.columnIndex] = item;
  180.                                                 }
  181.                                                 item.border = true;
  182.                                                 item.width = 50;
  183.                                                 item.height = 24;
  184.                                                 ui.addChild(item);
  185.                                                 item.x = tx;
  186.                                                 item.y = ty;
  187.                                                 tx += 50;
  188.                                         }
  189.                                        
  190.                                         tx = 10;
  191.                                         ty += 24;
  192.                                 }
  193.                                
  194.                                 ui.height = ty + 100;
  195.                                 ui.width = tx * 50 + 50;
  196.                                
  197.                                 var marr:Array = sheet.mergeCellData;
  198.                                 var isFrist:Boolean = true;
  199.                                 for each(var mi:Object in marr)
  200.                                 {
  201.                                         var txt:TextField = items[mi.leftTop.cellPosition.rowIndex + "#" + mi.leftTop.cellPosition.columnIndex] as TextField;
  202.                                         isFrist = true;
  203.                                         for(var k:int=mi.leftTop.cellPosition.rowIndex; k<=mi.rightBottom.cellPosition.rowIndex; k++)
  204.                                         {
  205.                                                 for(var l:int=mi.leftTop.cellPosition.columnIndex; l<=mi.rightBottom.cellPosition.columnIndex; l++)
  206.                                                 {
  207.                                                         if(isFrist)
  208.                                                         {
  209.                                                                 isFrist = false;
  210.                                                                 continue;
  211.                                                         }
  212.                                                         ui.removeChild(items[k + "#" + l]);
  213.                                                         delete items[k + "#" + l];
  214.                                                 }
  215.                                                 txt.width = 50 * (mi.rightBottom.cellPosition.columnIndex - mi.leftTop.cellPosition.columnIndex + 1);
  216.                                                 txt.height = 24 * (mi.rightBottom.cellPosition.rowIndex - mi.leftTop.cellPosition.rowIndex + 1);
  217.                                         }
  218.                                        
  219.                                 }
  220.                         }
  221.                        
  222.       protected function button1_clickHandler(event:MouseEvent):void
  223.       {
  224.         var f:File = File.desktopDirectory.resolvePath("test.xlsx");
  225.         var fs:FileStream = new FileStream();
  226.         fs.open(f, FileMode.WRITE);
  227.         fs.writeBytes(OfficeBase(officeFileList[selectedFile.name]).exportOffice());
  228.         fs.close();
  229.       }
  230.       
  231.       protected function button3_clickHandler(event:MouseEvent):void
  232.       {
  233.         var f:File = File.desktopDirectory.resolvePath("test.xlsx");
  234.         var fs:FileStream = new FileStream();
  235.         fs.open(f, FileMode.WRITE);
  236.         fs.writeBytes(Excel.createExcel().exportOffice());
  237.         fs.close();
  238.       }
  239.       
  240.     ]]>
  241.         </fx:Script>
  242.        
  243.         <mx:HDividedBox width="100%" height="100%" paddingTop="10">
  244.                 <mx:Tree id="tree" width="100" height="100%" labelField="name" iconField="icon" doubleClickEnabled="true" doubleClick="treeItemClick(event)"></mx:Tree>
  245.                
  246.                 <s:BorderContainer  width="100%" height="100%">
  247.                         <mx:Canvas width="100%" height="100%" horizontalScrollPolicy="on" verticalScrollPolicy="on">
  248.                                 <mx:UIComponent id="ui"/>
  249.                         </mx:Canvas>
  250.                         <s:TextArea id="txtInfo" x="10" y="10" width="90%" height="95%" editable="false"/>
  251.                 </s:BorderContainer>
  252.         </mx:HDividedBox>
  253.   <mx:Button label="导出" click="button1_clickHandler(event)"/>
  254.   <mx:Button label="导出空文档" x="200" click="button3_clickHandler(event)"/>
  255. </s:Window>
复制代码







欢迎光临 守望者--AIR技术交流 (http://www.airmyth.com/)