shanshan3301 发表于 2013-4-3 23:31:16

use TestComplete :chang Excel to CSV

在这里介绍了一下怎么把.xlsx 转成.csv文件


/* 4.3.2013
   shanshan xu
   XLSX2CSV*/

function Xls2Csv()
{
var Excel = Sys.OleObject("Excel.Application");
Excel.Workbooks.Open("C:\\Planar_UI_ComponentIDs.xlsx");//The Excel file path
var fso = new ActiveXObject("Scripting.FileSystemObject");
var cntSheets = Excel.ActiveWorkbook.Sheets.count;
for ( si = 1; si <= cntSheets; si++)//read every sheet
{
    var sheetname = Excel.ActiveWorkbook.Sheets(si).name;//get the name from sheet
    var fname="C:\\res\\"+sheetname+".csv";//the target folder to Storing files
    var f1 = fso.createtextfile(fname);//create the same name for every sheet
    var RowCount = Excel.ActiveWorkbook.Sheets(si).usedRange.Rows.Count;
    var ColumnCount = Excel.ActiveWorkbook.Sheets(si).usedRange.Columns.Count;
    for (var i = 1; i <= RowCount; i++)
    {
      var s = "";
      for (var j = 1; j <= ColumnCount; j++)
      {
      s=VarToString(Excel.ActiveWorkbook.sheets(si).Cells(i, j));
      if (j != ColumnCount)
      {
          f1.Write("\""+s+"\"");
          f1.Write(",")
      }
      else
      {
          f1.WriteLine("\""+s+"\"");
      }
      }
    }
    f1.close();
}
//f1.close();
Excel.Quit();
}

happyrichman 发表于 2015-7-1 10:26:28

ding
页: [1]
查看完整版本: use TestComplete :chang Excel to CSV