看提供的代码运行结果是按“地区”分类分解成以地区名的xlsx文件
1、在EXCEL按“地区”排序,获取“地区”的行数
2、分别按“地区”的行数复制到新建工作簿页面再保存到xlsx文件
1、在EXCEL按“地区”排序,获取“地区”的行数
2、分别按“地区”的行数复制到新建工作簿页面再保存到xlsx文件
cPath = ADDBS(JUSTPATH(SYS(16))) SET DEFAULT TO (cPath) IF DIRECTORY(cPath + "拆分文件") DELETE FILE (cPath + "拆分文件\*.*") ELSE MD (cPath + "拆分文件") ENDIF xlssl = ADIR(xlswjm, "*.xls*") wait '正在处理:' +alltrim(xlswjm(1,1)) WINDOW AT 30 ,100 NOWAIT OEXCEL = CREATEOBJECT("EXCEL.APPLICATION") OEXCEL.WORKBOOKS.OPEN(cpath+xlswjm(1,1)) OEXCEL.DisplayAlerts = .f. oShe = OEXCEL.SHEETS(1) nRow = oShe.UsedRange.Rows.Count nCol = oShe.UsedRange.Columns.Count oShe.Sort.SortFields.Add(oShe.Cells(2,3).Resize(nRow-1,1), 0, 1, 0) oShe.Sort.SetRange(oShe.Cells(2,1).Resize(nRow-1,nCol)) oShe.Sort.Header = 0 oShe.Sort.MatchCase = .f. oShe.Sort.Orientation = 1 oShe.Sort.SortMethod = 1 oShe.Sort.Apply() n = 2 DO WHILE n <= nRow cName = oShe.Cells(n,3).value ? cName m = OEXCEL.COUNTIF(oShe.UsedRange.Columns(3), cName) book = OEXCEL.WORKBOOKS.add() newShe = book.Sheets(1) oShe.Cells(1,1).Resize(1,nCol).Copy(newShe.Cells(1,1)) oShe.Cells(n,1).Resize(m,nCol).Copy(newShe.Cells(2,1)) newShe.Columns.AutoFit() book.SaveAs(cPath + "拆分文件\" + cName + ".xlsx") book.close() n = n + m ENDDO OEXCEL.ActiveWorkBook.Close(.F.) OEXCEL.QUIT