Test.js- var srcDir = 'E:\\Test'; //存放xls文件的源目录名
- var dstFile = 'E:\\result.xlsx'; //保存的文件名
- var exclude = [1,3,8,11,26,33,42,46,47,51,54,58,62,66,67,68,69,70,71,72,73]; //sheet1 要排除的行号
-
- var objExcel = new ActiveXObject('Excel.Application');
- var objNewBook = objExcel.WorkBooks.Add();
- objExcel.Visible = false;
- var fso = new ActiveXObject('Scripting.FileSystemObject');
- var e = new Enumerator( fso.GetFolder(srcDir).Files );
-
- Array.prototype.isInArray = function(a){
- for(var i=0; i<this.length; i++)if(this[i]==a)return true;
- return false;
- }
-
- var y = 0;
- for(; !e.atEnd(); e.moveNext()){
- var strFile = e.item().Path;
- var m = e.item().Name.match(/^[^-]+-[^-]+-[^-]+-_(\d+).*\.xlsx?$/i);
- if( !m ) continue;
- var x = 0; y++;
- objNewBook.WorkSheets(1).Cells(++x,y).Value = m[1]; //文件名中的编号
- var objBook = objExcel.WorkBooks.Open(strFile);
- var objSheet1 = objBook.WorkSheets(1);
- var objSheet2 = objBook.WorkSheets(2);
- var rows1 = objSheet1.UsedRange.Rows.Count + 1; //sheet1 行数
- var rows2 = objSheet2.UsedRange.Rows.Count + 1; //sheet2 行数
- var colm2 = objSheet2.UsedRange.Columns.Count + 1; //sheet2 列数
- for(var i=1; i<=rows1; i++){
- if( exclude.isInArray(i) ) continue;
- objNewBook.WorkSheets(1).Cells(++x,y).Value = objSheet1.Cells(i,2).Value;
- }
- for(var i=2; i<=colm2; i++){
- for(var j=2; j<=rows2; j++){
- objNewBook.WorkSheets(1).Cells(++x,y).Value = objSheet2.Cells(j,i).Value;
- }
- }
- objBook.Close();
- }
-
- objNewBook.SaveAs(dstFile, 51); //保存文件
- objNewBook.Close();
- objExcel.Quit();
-
- WSH.Echo('Done')
复制代码
|