2011-01-11 88 views

回答

0

roo是的腦海中唯一的選項,XLSX

編輯

雖然this link證明寫XLSX文件

+0

我不知道roo會在這裏有多大用處。他們的網站表示,它目前僅限於Excel電子表格的只讀訪問。 – bta 2011-01-11 13:39:11

1

假設您有Excel安裝,你應該能夠使用WIN32OLE(http://ruby-doc.org/stdlib/libdoc/wi n32ole/rdoc/index.html)腳本Excel。最簡單的技術可能是將文件重命名爲.xlsx文件,然後讓Excel打開並保存它。這可能比試圖通過「另存爲」操作編寫腳本更容易。

這不是Ruby解決方案,但我也使用AutoIt腳本化Excel界面。

0

如果您有Excel安裝,您可以使用下面的方法將XLS文件轉換成XLSX文件:

require 'win32ole' 

def xls2xlsx(path, target = nil) 
    raise ArgumentError unless path =~ /.xls\Z/ 
    raise ArgumentError unless File.exist?(path) 

    target = path + 'x' unless target # Save the workbook./must be \ 
    puts "convert %s to %s" % [path, target] 

    # Create an instance of the Excel application object 
    xl = WIN32OLE.new('Excel.Application') 
    # Make Excel visible 1=visible 0=not visible 
    xl.Visible = 1 
    #~ xl.Interactive = false #visible, but no input allowed 
    #~ xl.ScreenUpdating = false #make it faster 
    xl.DisplayAlerts = false #No alerts like "don't overwrite 
    # Add a new Workbook object  
    wb = xl.Workbooks.Open(File.expand_path(path)) 

    wb.SaveAs(File.expand_path(target).gsub!(/\//, '\\'), 51) #excel 2007 
    # Close the workbook 
    wb.Close 
    # Quit Excel 
    xl.Quit 
end 

如果您需要TH其他方式(XLSX到XLS),您可以使用:

def xlsx2xls(path, target = nil) 
    raise ArgumentError unless path =~ /.xlsx\Z/ 
    raise ArgumentError unless File.exist?(path) 

    target = path.chop unless target # Save the workbook./must be \ 
    puts "convert %s to %s" % [path, target] 

    # Create an instance of the Excel application object 
    xl = WIN32OLE.new('Excel.Application') 
    # Make Excel visible 1=visible 0=not visible 
    xl.Visible = 1 
    #~ xl.Interactive = false #visible, but no input allowed 
    #~ xl.ScreenUpdating = false #make it faster 
    xl.DisplayAlerts = false #No alerts like "don't overwrite 
    # Add a new Workbook object  
    wb = xl.Workbooks.Open(File.expand_path(path)) 

    wb.SaveAs(File.expand_path(target).gsub!(/\//, '\\'), -4143) #excel97_2003_format 
    # Close the workbook 
    wb.Close 
    # Quit Excel 
    xl.Quit 
end 

我用第2種方法結合axlsx得到XLS文件。首先我用axslx創建一個xlsx,然後通過winole將它轉換爲xls。