excel中sheet.cells的参数都是从1开始的

用jcom获得excel cell的值
xlsApp = new IDispatch(rm, "Excel.Application");

            xlsApp.put("Visible", new Boolean(false));
            IDispatch workbooks = (IDispatch) xlsApp.get("Workbooks");

            workbooks.method("open", new Object[] { file, null, null, null,
                    password });// open
            logger.debug("1----" + file);

            IDispatch sheets = ((IDispatch) xlsApp.get("Sheets"));

            IDispatch sheet = ((IDispatch) sheets.get("item",
                    new Object[] { sheetName }));
           
            // sheet.method("Select", null);
            IDispatch range = ((IDispatch) sheet.get("Cells", new Object[] {
                    new Integer(i), new Integer(j) }));
            retval = range.get("value").toString();

i------1+;
j-----1+;
要是传个0就报错了
powerdesigner导出excel ctrl + shift +x 弹出窗口输入:'****************************************************************************** Option Explicit Dim rowsNum rowsNum = 0 '----------------------------------------------------------------------------- ' Main function '----------------------------------------------------------------------------- ' Get the current active model Dim Model Set Model = ActiveModel If (Model Is Nothing) Or (Not Model.IsKindOf(PdPDM.cls_Model)) Then MsgBox "The current model is not an PDM model." Else ' Get the tables collection '创建EXCEL APP dim beginrow DIM EXCEL, SHEET, SHEETLIST set EXCEL = CREATEOBJECT("Excel.Application") EXCEL.workbooks.add(-4167)'添加工作表 EXCEL.workbooks(1).sheets(1).name ="表结构" set SHEET = EXCEL.workbooks(1).sheets("表结构") EXCEL.workbooks(1).sheets.add EXCEL.workbooks(1).sheets(1).name ="目录" set SHEETLIST = EXCEL.workbooks(1).sheets("目录") ShowTableList Model,SHEETLIST ShowProperties Model, SHEET,SHEETLIST EXCEL.workbooks(1).Sheets(2).Select EXCEL.visible = true '设置列宽和自动换行 sheet.Columns(1).ColumnWidth = 20 sheet.Columns(2).ColumnWidth = 20 sheet.Columns(3).ColumnWidth = 20 sheet.Columns(4).ColumnWidth = 40 sheet.Columns(5).ColumnWidth = 10 sheet.Columns(6).ColumnWidth = 10 sheet.Columns(1).WrapText =true sheet.Columns(2).WrapText =true sheet.Columns(4).WrapText =true '不显示网格线 EXCEL.ActiveWindow.DisplayGridlines = False End If '----------------------------------------------------------------------------- ' Show properties of tables '----------------------------------------------------------------------------- Sub ShowProperties(mdl, sheet,SheetList) ' Show tables of the current model/package rowsNum=0 beginrow = rowsNum+1 Dim rowIndex rowIndex=3 ' For each table output "begin" Dim tab For Each tab In mdl.tables ShowTable tab,sheet,rowIndex,sheetList rowIndex = rowIndex +1 Next if mdl.tables.count > 0 then sheet.Range("A" & beginrow + 1 & ":A" & rowsNum).Rows.Group end if output "end" End Sub '----------------------------------------------------------------------------- ' Show table properties '----------------------------------------------------------------------------- Sub ShowTable(tab, sheet,rowIndex,sheetList) If IsObject(tab) Then Dim rangFlag rowsNum = rowsNum + 1 ' Show properties Output "================================" sheet.cells(rowsNum, 1) =tab.name sheet.cells(rowsNum, 1).HorizontalAlignment=3 sheet.cells(rowsNum, 2) = tab.code 'sheet.cells(rowsNum, 5).HorizontalAlignment=3 'sheet.cells(rowsNum, 6) = "" 'sheet.cells(rowsNum, 7) = "表说明" sheet.cells(rowsNum, 3) = tab.comment 'sheet.cells(rowsNum, 8).HorizontalAlignment=3 sheet.Range(sheet.cells(rowsNum, 3),sheet.cells(rowsNum, 7)).Merge '设置超链接,从目录点击表名去查看表结构 '字段中文名 字段英文名 字段类型 注释 是否主键 是否非空 默认值 sheetList.Hyperlinks.Add sheetList.cells(rowIndex,2), "","表结构"&"!B"&rowsNum rowsNum = rowsNum + 1 sheet.cells(rowsNum, 1) = "字段中文名" sheet.cells(rowsNum, 2) = "字段英文名" sheet.cells(rowsNum, 3) = "字段类型" sheet.cells(rowsNum, 4) = "注释" sheet.cells(rowsNum, 5) = "是否主键" sheet.cells(rowsNum, 6) = "是否非空" sheet.cells(rowsNum, 7) = "默认值" '设置边框 sheet.Range(sheet.cells(rowsNum-1, 1),sheet.cells(rowsNum, 7)).Borders.LineStyle = "1" 'sheet.Range(sheet.cells(rowsNum-1, 4),sheet.cells(rowsNum, 9)).Borders.LineStyle = "1" '字体为10号 sheet.Range(sheet.cells(rowsNum-1, 1),sheet.cells(rowsNum, 7)).Font.Size=10 Dim col ' running column Dim colsNum colsNum = 0 for each col in tab.columns rowsNum = rowsNum + 1 colsNum = colsNum + 1 sheet.cells(rowsNum, 1) = col.name 'sheet.cells(rowsNum, 3) = "" 'sheet.cells(rowsNum, 4) = col.name sheet.cells(rowsNum, 2) = col.code sheet.cells(rowsNum, 3) = col.datatype sheet.cells(rowsNum, 4) = col.comment If col.Primary = true Then sheet.cells(rowsNum, 5) = "Y" Else sheet.cells(rowsNum, 5) = " " End If If col.Mandatory = true Then sheet.cells(rowsNum, 6) = "Y" Else sheet.cells(rowsNum, 6) = " " End If sheet.cells(rowsNum, 7) = col.defaultvalue next sheet.Range(sheet.cells(rowsNum-colsNum+1,1),sheet.cells(rowsNum,7)).Borders.LineStyle = "3" 'sheet.Range(sheet.cells(rowsNum-colsNum+1,4),sheet.cells(rowsNum,9)).Borders.LineStyle = "3" sheet.Range(sheet.cells(rowsNum-colsNum+1,1),sheet.cells(rowsNum,7)).Font.Size = 10 rowsNum = rowsNum + 2 Output "FullDescription: " + tab.Name End If End Sub '----------------------------------------------------------------------------- ' Show List Of Table '----------------------------------------------------------------------------- Sub ShowTableList(mdl, SheetList) ' Show tables of the current model/package Dim rowsNo rowsNo=1 ' For each table output "begin" SheetList.cells(rowsNo, 1) = "主题" SheetList.cells(rowsNo, 2) = "表中文名" SheetList.cells(rowsNo, 3) = "表英文名" SheetList.cells(rowsNo, 4) = "表说明" rowsNo = rowsNo + 1 SheetList.cells(rowsNo, 1) = mdl.name Dim tab For Each tab In mdl.tables If IsObject(tab) Then rowsNo = rowsNo + 1 SheetList.cells(rowsNo, 1) = "" SheetList.cells(rowsNo, 2) = tab.name SheetList.cells(rowsNo, 3) = tab.code SheetList.cells(rowsNo, 4) = tab.comment End If Next SheetList.Columns(1).ColumnWidth = 20 SheetList.Columns(2).ColumnWidth = 20 SheetList.Columns(3).ColumnWidth = 30 SheetList.Columns(4).ColumnWidth = 60 output "end" End Sub 导出文件在哪里了
最新发布
09-10
Option Explicit Dim mdl ' 当前模型 Set mdl = ActiveModel If (mdl Is Nothing) Then MsgBox "没有活动的模型", vbExclamation, "错误" WScript.Quit End If Dim HaveExcel Dim RQ RQ = vbYes ' 假设 Excel 已安装 If RQ = vbYes Then HaveExcel = True Dim xlApp Set xlApp = CreateObject("Excel.Application") xlApp.Visible = True ' 显示 Excel 界面 xlApp.Workbooks.Open "D:\my_study\示例文件\pdm导入Excle文件.xlsx" ' 修改为实际路径 Else HaveExcel = False MsgBox "未检测到 Excel 应用程序", vbExclamation, "错误" WScript.Quit End If ' 调用主处理函数,传入当前模型 ProcessTables xlApp, mdl Sub ProcessTables(xlApp, mdl) Dim sheet Set sheet = xlApp.ActiveWorkbook.Sheets("cpms table list") ' 指定工作表名称 Dim rowIndex, tableCount, fieldCount rowIndex = 1 tableCount = 0 Do While rowIndex <= sheet.UsedRange.Rows.Count If InStr(sheet.Cells(rowIndex, 1).Value, "表名") > 0 Then Dim tableName, tableCode, tableComment tableName = sheet.Cells(rowIndex, 2).Value ' 表名在B列 tableCode = sheet.Cells(rowIndex, 3).Value ' 表代码在C列 tableComment = sheet.Cells(rowIndex, 4).Value ' 表注释在D列 ' 检查模型中是否已有同名表 On Error Resume Next Dim existingTable Set existingTable = mdl.Tables(tableName) On Error GoTo 0 If Not existingTable Is Nothing Then MsgBox "表 """ & tableName & """ 已存在,跳过。" rowIndex = rowIndex + 1 Continue Do End If ' 创建新表 Dim newTable Set newTable = mdl.Tables.CreateNew newTable.Name = tableName newTable.Code = tableCode newTable.Comment = tableComment tableCount = tableCount + 1 rowIndex = rowIndex + 1 ' 移动到字段标题行 rowIndex = rowIndex + 1 ' 移动到第一个字段行 fieldCount = 0 Do While rowIndex <= sheet.UsedRange.Rows.Count If Trim(sheet.Cells(rowIndex, 1).Value) = "" Or _ InStr(sheet.Cells(rowIndex, 1).Value, "表名") > 0 Then Exit Do End If Dim newColumn Set newColumn = newTable.Columns.CreateNew newColumn.Name = sheet.Cells(rowIndex, 1).Value ' 字段名在A列 newColumn.Code = sheet.Cells(rowIndex, 2).Value ' 字段代码在B列 newColumn.Comment = sheet.Cells(rowIndex, 3).Value ' 字段注释在C列 newColumn.DataType = sheet.Cells(rowIndex, 4).Value ' 数据类型在D列 If UCase(Trim(sheet.Cells(rowIndex, 5).Value)) = "TRUE" Or _ Trim(sheet.Cells(rowIndex, 5).Value) = "是" Then newColumn.Primary = True End If If UCase(Trim(sheet.Cells(rowIndex, 6).Value)) = "TRUE" Or _ Trim(sheet.Cells(rowIndex, 6).Value) = "是" Then newColumn.Mandatory = True End If fieldCount = fieldCount + 1 rowIndex = rowIndex + 1 Loop MsgBox "创建表: " & tableName & vbCrLf & _ "字段数: " & fieldCount, vbInformation, "表创建成功" Else rowIndex = rowIndex + 1 End If Loop xlApp.ActiveWorkbook.Close False xlApp.Quit Set xlApp = Nothing MsgBox "成功创建表数量: " & tableCount, vbInformation, "导入完成" End Sub 在这段代码中添加上述排序方法
07-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值