libreoffice for mac 将xls文件转换成xlsx文件

安装: brew cask install libreoffice libreoffice-language-pack

相关链接: https://computingforgeeks.com/install-openoffice-libreoffice-macos-sierra/

安装之后,会安装一个LibreOfficeAPP, 但是 在终端并没有 libreoffice命令的存在,因为这里使用的是 soffice命令

相关链接: https://ubuntu-mate.community/t/cant-open-libreoffice-from-terminal/16300/4

使用: soffice --convert-to xlsx 1122.xls --headless

遇到的坑:在我们服务器上进行转换的文件,下载在本地之后,windows 7 系统exls 07版不能正常的打开文件。 2011版没有问题。
并且网上普遍反应当前这个情况,目前并没有具体解决方案(我和公司小伙帮没有找到具体解决方案)

1    # 传递文件的绝对路径(xls文件),将文件转换成xlsx 文件 
2    def xls_convert_to_xlsx(path) 
3      filename = nil 
4      file_path = "#{path}x" 
5      begin 
6        # 开始转换 
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 文件转换失败 error: #{e}")
12      end
13
14      return file_path, filename
15    end

Ruby里面使用 shell 命令 两个反引号执行shell命令

image.png

image.png

可以使用 --outdir指定文件存储的目录

image.png

image.png

你可能感兴趣的:(libreoffice for mac 将xls文件转换成xlsx文件)