安裝: brew cask install libreoffice libreoffice-language-pack
相關(guān)鏈接: https://computingforgeeks.com/install-openoffice-libreoffice-macos-sierra/
安裝之后叹坦,會(huì)安裝一個(gè)LibreOfficeAPP刊苍, 但是 在終端并沒有 libreoffice命令的存在,因?yàn)檫@里使用的是 soffice命令
相關(guān)鏈接: https://ubuntu-mate.community/t/cant-open-libreoffice-from-terminal/16300/4
使用: soffice --convert-to xlsx 1122.xls --headless
遇到的坑:在我們服務(wù)器上進(jìn)行轉(zhuǎn)換的文件致稀,下載在本地之后,windows 7 系統(tǒng)exls 07版不能正常的打開文件熔任。 2011版沒有問題泽本。
并且網(wǎng)上普遍反應(yīng)當(dāng)前這個(gè)情況,目前并沒有具體解決方案(我和公司小伙幫沒有找到具體解決方案)
1 # 傳遞文件的絕對路徑(xls文件)捻爷,將文件轉(zhuǎn)換成xlsx 文件
2 def xls_convert_to_xlsx(path)
3 filename = nil
4 file_path = "#{path}x"
5 begin
6 # 開始轉(zhuǎn)換
7 `soffice --headless --convert-to xlsx #{path} --outdir #{File.dirname(path)}`
8
9 filename = path.split('/').last + 'x' if File.exists?(file_path)
10 rescue => e
11 raise("xls 文件轉(zhuǎn)換失敗 error: #{e}")
12 end
13
14 return file_path, filename
15 end
Ruby里面使用 shell 命令
兩個(gè)反引號(hào)執(zhí)行shell命令
<figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">image.png</figcaption>
</figure>
可以使用 --outdir
指定文件存儲(chǔ)的目錄
<figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">image.png</figcaption>
</figure>