2012-09-26 58 views
1

我有我想轉換到CSV文件,並已發現這個JavaScript多個.xls文件分隔的文件:轉換多個.xls文件爲.csv用分號

/* 
jPaq - A fully customizable JavaScript/JScript library 
http://jpaq.org/ 

Copyright (c) 2011 Christopher West 
Licensed under the MIT license. 
http://jpaq.org/license/ 

Version: 1.0.6.000001 
Revised: April 6, 2011 
*/ 
(function(){jPaq={toString:function(){return"jPaq - A fully customizable JavaScript/JScript library created by Christopher West."}};var e=new ActiveXObject("WScript.Shell");alert=function(a,b,c,d){a==null&&(a="");if(!b)b=WScript.ScriptName;c==null&&(c=alert.OKOnly+alert.Exclamation);d==null&&(d=0);return e.Popup(a,d,b,c)};alert.OKOnly=0;alert.OKCancel=1;alert.AbortRetryIgnore=2;alert.YesNoCancel=3;alert.YesNo=4;alert.RetryCancel=5;alert.Critical=16;alert.Question=32;alert.Exclamation=48;alert.Information= 
64;alert.Timeout=-1;alert.OK=1;alert.Cancel=2;alert.Abort=3;alert.Retry=4;alert.Ignore=5;alert.Yes=6;alert.No=7})(); 
/***** END OF JPAQ *****/ 

try { 
    // Create an instance of Excel, but don't allow the content 
    // area to be repainted. 
    var xlCSV = 6; 
    var xlApp = new ActiveXObject("Excel.Application"); 
    xlApp.Visible = true; 
    xlApp.ScreenUpdating = false; 
    xlApp.DisplayAlerts = false; 

    // Initialize the counts. 
    var fileCount = 0, csvCount = 0; 

    // Regular expression for match Excel files to be converted. 
    var re = /([^\\\/]+)\.xlsx?$/i; 

    // Reference the containing folder. 
    var fso = new ActiveXObject("Scripting.FileSystemObject"); 
    var fldr = fso.GetFolder(WScript.ScriptFullName.replace(/[\\\/][^\\\/]+$/, "")); 

    // Determine whether or not linefeed characters should be removed. 
    var msg = "Would you like to remove linefeed characters from all cells?"; 
    var title = "Remove Linefeed Characters"; 
    var removeLf = alert.Yes == alert(msg, title, alert.YesNo + alert.Question); 

    // Loop through all of the xls and xlsx files in this folder. 
    for(var e = new Enumerator(fldr.Files); !e.atEnd(); e.moveNext()) { 
    var aFile = e.item(); 
    if(re.test(aFile.Name)) { 
     xlApp.StatusBar = "Processing " + aFile.Path + "..."; 

     // Open the workbook. 
     var wb = xlApp.Workbooks.Open(aFile.Path); 

     // Save each worksheet as a CSV file. 
     for(var e2 = new Enumerator(wb.Sheets); !e2.atEnd(); e2.moveNext()) { 
     var ws = e2.item(); 
     if(removeLf) { 
      ws.UsedRange.Replace("\n", ""); 
     } 
     var csvPath = aFile.Path.replace(re, function($0, $1) { 
      return $1 + "-" + ws.Name + ".csv"; 
     }); 
     ws.SaveAs(csvPath, xlCSV); 
     csvCount++; // Increment the number of CSV's. 
     } 

     // Close the workbook. 
     wb.Close(); 

     // Increment the number of files. 
     fileCount++; 
    } 
    } 

    // Allow alerts to be displayed, and the screen to be updated again. 
    xlApp.DisplayAlerts = true; 
    xlApp.ScreenUpdating = true; 

    // Close Excel. 
    xlApp.Quit(); 

    var msg = "The results are as follows:\nFiles converted: " 
    + fileCount + "\nCSV's created: " + csvCount; 
    var title = "Conversion Process Complete"; 
    alert(msg, title, alert.Information); 
} 
catch(e) { 
    // If the Excel workbook is open, close it. 
    try{ wb.Close(false); }catch(e2){} 

    // If Excel is open, change the settings back to normal and close it. 
    try{ 
    xlApp.DisplayAlerts = true; 
    xlApp.ScreenUpdating = true; 
    xlApp.Quit(); 
    } catch(e2){} 

    // Print the error message. 
    var msg = "The following error caused this script to fail:\n" 
    + e.message; 
    var title = "Critical Error Occurred"; 
    alert(msg, title, alert.Critical); 
} 

然而,這段代碼保存的文件與逗號分隔,而不是分號。 看來,我必須用我的本地設置保存文件(當我通過Excel手動保存時,它們以分號保存),我不知道如何...

有沒有人知道

的參數
ws.SaveAs(csvPath, xlCSV); 

方法,所以它會使用我的本地設置?

我非常感謝您的幫助!

回答

1

我很高興你發現my post有幫助。不幸的是,唯一的辦法就是通過控制面板實際改變系統的本地設置。我在多次搜索後確定了這一點。 Here is a page這說明了這一事實。如果您找到其他解決方案,請告訴我們。

+0

:D我完全忘了這個問題,沒有注意到有答案。尤其是由誰寫的初始腳本!但是,由於這是一次性的事情,我們需要它很快,所以我們使用了一些可疑的第三方軟件來轉換Excel工作表。它適用於這種情況。 @ chris-west感謝您的回答! – user1700788