根据列表将 .xls/.xlsx 电子表格转换为多个 .csv 的

csh*_*eth 10 command-line xls csv

我需要将单个 .xls/.xlsx 文件的所有工作表转换为 .csv。这将在所有目录和子目录(递归)中的所有 .xls 文件上完成。

第 1 步:使用以下方法将所有 .xls 的工作表名称转换为 .csv:

for file in $(find . -name '*.xls' -o -name '*.xlsx');do in2csv -n "$file" > ${file%.xls}-sheetnames-list.csv; done
Run Code Online (Sandbox Code Playgroud)

filename-sheetnames-list.csv 可以作为一个列表:

sheetname1
sheetname2
sheetname3
Run Code Online (Sandbox Code Playgroud)

第 2 步:使用 in2csv 将特定工作表转换为 .csv 的代码是:

in2csv --sheet "SHEETNAME" filename.xls > filename-SHEETNAME.csv
Run Code Online (Sandbox Code Playgroud)

如何获取 .xls/x 中的每个工作表名称并为包含 .xls/x 的所有目录分别编写每个工作表?

in2csv --write-sheets "-" filename.xls > filename-sheet1.csv filename-sheet2.csv .... 仅在 sheet1.csv 上提供输出,不确定如何从中获取所有工作表。

pLu*_*umo 10

您可以将一个循环放入另一个循环中。

为了避免错误,不使用forfind结果。

while IFS= read -r file; do
    while IFS= read -r sheet; do
        in2csv --sheet "$sheet" "$file" > "${file%.*}-${sheet}.csv"
    done < <(in2csv -n "$file")
done < <(find . -name '*.xls' -o -name '*.xlsx')
Run Code Online (Sandbox Code Playgroud)


mur*_*uru 7

跳过查找并使用 bash:

shopt -s globstar  # enable recursive globbing
for f in **/*.xls{,x}  # for files ending in .xls or .xlsx
do
    in2csv -n "$f" |   # get the sheetnames
      xargs -I {} bash -c 'in2csv --sheet "$2" "$1" > "${1%.*}"-"$2".csv' _ "$f" {} # {} will be replaced with the sheetname
done
Run Code Online (Sandbox Code Playgroud)