Unix bash:获取 Excel 文件中正确的行数

问题描述 投票:0回答:1

有没有正确的方法来查找unix中xlsx文件的行数?假设每个 Excel 文件只有 1 页。

wc -l
在这里不起作用,因为它给出了编码文件的行数,而不是实际行数。

我找到了一种 Perl 方法来使用 电子表格模块 来做到这一点。 或者将 Excel 文件转换为 CSV,然后计算行数

但是是一种简单的方法,无需安装额外的模块吗?

bash unix spreadsheet xlsx
1个回答
0
投票

这个问题很时髦,我喜欢!

任何以“x”结尾的新型办公文件(例如 .xlsx 和 .docx)实际上都是充满大量 .xml 和其他内容的压缩文件夹。如果您想查看 Unix 中其中一张表的 xml,只需将其解压缩,然后开始浏览即可。

为了进一步深入,我使用一台没有 Office 软件包的 Linux 机器从我的家庭服务器上抓取了一个电子表格,并验证了我上面刚刚建议的内容。

scott@chromebook-edgar:~/Downloads$ ls
'Monte Carlo Simulation.xlsx'
scott@chromebook-edgar:~/Downloads$ file 'Monte Carlo Simulation.xlsx' 
Monte Carlo Simulation.xlsx: Microsoft Excel 2007+
scott@chromebook-edgar:~/Downloads$ unzip 'Monte Carlo Simulation.xlsx' 
Archive:  Monte Carlo Simulation.xlsx
  inflating: [Content_Types].xml     
  inflating: _rels/.rels             
  inflating: xl/workbook.xml         
  inflating: xl/_rels/workbook.xml.rels  
  inflating: xl/worksheets/sheet1.xml  
  inflating: xl/worksheets/sheet2.xml  
  inflating: xl/theme/theme1.xml     
  inflating: xl/styles.xml           
  inflating: xl/sharedStrings.xml    
  inflating: xl/drawings/drawing1.xml  
  inflating: xl/charts/chart1.xml    
  inflating: xl/worksheets/_rels/sheet2.xml.rels  
  inflating: xl/drawings/_rels/drawing1.xml.rels  
  inflating: xl/calcChain.xml        
  inflating: docProps/core.xml       
  inflating: docProps/app.xml        
scott@chromebook-edgar:~/Downloads$ ls
'[Content_Types].xml'   docProps  'Monte Carlo Simulation.xlsx'   _rels   xl
scott@chromebook-edgar:~/Downloads$ cd xl
scott@chromebook-edgar:~/Downloads/xl$ ls
calcChain.xml  charts  drawings  _rels  sharedStrings.xml  styles.xml  theme  workbook.xml  worksheets
scott@chromebook-edgar:~/Downloads/xl$ vim workbook.xml 
scott@chromebook-edgar:~/Downloads/xl$ cd worksheets/
scott@chromebook-edgar:~/Downloads/xl/worksheets$ ls
_rels  sheet1.xml  sheet2.xml

在 xl/worksheets/ 文件夹中,我找到了每个工作表的 xml 文档,其中包含未真正格式化的 xml(全部在一行中)。我使用广泛使用的 xmllint 工具对其进行了格式化,然后通读了一下。在其中,我在前面附近找到了一个“维度”字段,以及每行的单独格式字段

scott@chromebook-edgar:~/Downloads/xl/worksheets$ xmllint --format sheet1.xml | grep dimension
  <dimension ref="A1:G60"/>
scott@chromebook-edgar:~/Downloads/xl/worksheets$ xmllint --format sheet1.xml | grep 60
  <dimension ref="A1:G60"/>
        <v>0.94903607351434327</v>
        <v>60</v>
        <v>1.029702686080076</v>
        <v>460</v>
    <row r="60" spans="1:7" ht="13.2" thickBot="1" x14ac:dyDescent="0.25">

所以你可能可以做这样的事情来找出我假设的实际有数据的范围中的最低行是什么

scott@chromebook-edgar:~/Downloads/xl/worksheets$ xmllint --format sheet1.xml | grep dimension | awk -F\" '{print $2}' | awk -F: '{print $2}' | grep -o '[0-9]\+'
60
scott@chromebook-edgar:~/Downloads/xl/worksheets$ xmllint --format sheet2.xml | grep dimension | awk -F\" '{print $2}' | awk -F: '{print $2}' | grep -o '[0-9]\+'
204
© www.soinside.com 2019 - 2024. All rights reserved.