function myTest() { addSheet() setValue() regFind() deleteSheet() } // 新增sheet function addSheet(name) { var st = Sheets.Add() st.Name = name || "测试" } // 删除sheet function deleteSheet(name) { name = name || "测试" var st = Sheets(name) // Sheets(1) Sheets.Item(1) st.Delete() } // 正则查找整个Excel // 序号都是从1开始 // 如果当前sheet被激活可以直接使用 Cells、Columns、Rows 等 function regFind(sheetName, reg) { var res = [] reg = reg instanceof RegExp ? reg : reg && new RegExp(reg, 'g') || /(\d+)/g //for(var st = 1; st <= Sheets.Count; st++) { var sheet = Sheets(sheetName || '测试'); for(var row = 1; row <= 10; row++) { sheet.Rows(row).Hidden = true for(var col = 1; col <= 10; col++) { var cellValue = sheet.Cells.Item(row, col).Value2 + '' var m = reg.exec(cellValue) while(m) { res.push(m[1]) m = reg.exec(cellValue) } if(reg.test(cellValue)) { sheet.Rows(row).Hidden = false } } } //} alert("查询“" + reg + "”的结果:<br>" + res.join(',')) } // 设置值 // 如果当前sheet被激活可以直接使用 Cells、Columns、Rows 等 function setValue(sheetName) { var st = Sheets(sheetName || '测试'); for(var row = 1; row <= 10; row++) { for(var col = 1; col <= 10; col++) { var v = '测试' if((row + col) % 9 != 0 && row % 2 == 0) { v = row + col } st.Cells.Item(row, col).Value2 = v st.Cells.Item(row, col).Interior.ColorIndex = row + col } } }
或者