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 ) } } }
部分代码已略去 ,根据不同的业务需求,编写相应的代码
相关文章推荐
- js无法执行 var oXL = new ActiveXObject("Excel.Application");
- excel导出插件失败解决(原标题JS new ActiveXObject("Excel.Application")产生的问题)
- ActiveXObject("Excel.application")不能创建
- js无法执行 var oXL = new ActiveXObject("Excel.Application");
- AutomationFactory.CreateObject("Excel.Application")) // Exception: 指定的 ProgID 未注册任何对象
- Javascript调用 ActiveXObject导出excel文档。
- SMW0报错:For selected object type excel no application has been defined Maintain the settings
- ActiveXObject创建连接访问cache(Ensemble)服务器,并将数据导出到excel
- 关于asp使用CreateObject("Excel.Application")出现无法创建ActiveX对象的错误
- 在JavaScript中利用ActiveXObject控制Excel的方法
- ActiveXObject做excel导出
- javascript调用activeXObject 导出页面表格数据到excel
- AutomationFactory.CreateObject("Excel.Application")无法为指定的ProgID创建对象实例
- 在JavaScript中利用ActiveXObject控制Excel的方法
- JS用ActiveXObject导出Excel
- new ActiveXObject("Excel.Application")创建时报错
- Unable to cast COM object of type Microsoft.Office.Interop.Excel.ApplicationClass' ...
- Qt中用QAxObject操作Excel
- Apache POI练习生成EXCEL(1)
- javascript ActiveXObject