您的位置:首页 > 移动开发 > Objective-C

ActiveXObject之 Excel.Application练习

2013-07-03 23:12 330 查看
<script type="text/javascript">
	
  
  	/**
		1.dataTransferAppoint 数据转换约定
		2.filePath 路径
		3.hasTitle EXCEL是否拥有标题行,默认为true
		4.lineCount 列总数
		5.stableLineCount 固定的列数
		6.hasSerialNumer EXCEL中是否拥有序号
	*/
	function dataTransferAppoint( filePath, hasTitle, rowsCount, lineCount, stableLineCount, hasSerialNumber) {
		this.filePath = filePath;
		this.hasTitle = hasTitle == undefined ? true : hasTitle;
		this.hasSerialNumber = hasSerialNumber;
		this.rowsCount = rowsCount;
		this.lineCount = lineCount;
		this.stableLineCount = stableLineCount;
		this.fxLineCount = 0;
		this.lineStartLine = 4;
		this.rowStartLine = 2;
		this.serialNumber = 0;
		
		/** 从已存在HTML的CONTEXT获取序列号 或者 自行产生序列号 */
		this.getSerialNumber = function () {
			var numberSize =  $('.serialNumer').size();
			if( numberSize > 0 ) {
			   this.serialNumber = numberSize == 1 ? $('.serialNumer').get( 0 ).value : $('.serialNumer').get( numberSize - 1 ).value 
			}
		   return ++this.serialNumber;
		};
		
		/** .............略*/

		/** 检测导入的EXCEL路径 及 类型 */
		this.isXlsTpye = function( filePath ) {
			this.filePath = filePath;
			if( !(this.filePath && (this.filePath.length > 4) &&( this.filePath.substr( this.filePath.length - 4, this.filePath.length - 1 ) == '.xls')) ) {
				alert( '选择导入的EXCEL文件路径不正确' );
				return false;
			}   
			return true;
		};
	}
	
	function Mine(){
		this.dataTransferAppoint = new dataTransferAppoint();
		
		/** 根据参数FILEPATH 初始化, 根据初始化的结果 判断可执行性*/
		this.initialize = function( filePath ) {
			this.filePath = filePath;
			if( !this.dataTransferAppoint.isXlsTpye( filePath ) ) {
				return false;
			}
var activeXObj = new ActiveXObject( "Excel.application" );
			var excelWorkbook= activeXObj.Workbooks.open( this.filePath );
	
			excelWorkbook.worksheets(1).select();
			var eSheet = excelWorkbook.ActiveSheet;
			
			
			var rowsCount = eSheet.usedrange.rows.count;
			var lineCount = eSheet.usedrange.cells.count / rowsCount;
			
			this.dataTransferAppoint.rowsCount = rowsCount;
			this.dataTransferAppoint.lineCount = lineCount;
			this.dataTransferAppoint.stableLineCount = 4;
			this.dataTransferAppoint.fxLineCount = lineCount - this.dataTransferAppoint.stableLineCount ;
			this.activeXObj = activeXObj;
			this.eSheet = eSheet;
			
			return true;
		};
		/** 从EXCEL读取数据,并转换为String对象发回 */
		this.readExcelFileInfo = function( eSheet ) {
		  /** .............略*/
		};
		
		/** .............略*/
	  /** 添加到指定的TABLE中 */
	  this.convertExcelInfoToHTMLMeta = function( appendHTML ) {
		$( '#fxTable' ).append( appendHTML );
	  }
	  
	  /** 根据导入的EXCEL列数对应DOM TABLE中的TD个数*/
	  this.transformColspans = function() {
		$( '#colspanBulk' ).attr( 'colSpan', this.dataTransferAppoint.fxLineCount );
		
	  };
	  
	  /** 释放对应的资源 */
	  this.release = function() {
		try{
			this.activeXObj.Quit();
			this.dataTransferAppoint = null;
		} catch( e ) {
			alert( '释放activeXObj资源异常,'+ e )
		}
	  }
	}


部分代码已略去 ,根据不同的业务需求,编写相应的代码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: