0%

CENTOS下终端显示xlsx内容

在一些特殊情况下, 需要在终端下查看xlsx中的内容
该脚本目的在此

安装

1
2
yum install python-pip -y
pip install xlsx2csv

脚本

xlsx_to_csv.shview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/bash - 
#===============================================================================
#
# FILE: xlsx_to_csv.sh
#
# USAGE: ./xlsx_to_csv.sh
#
# DESCRIPTION: linux 命令行下查看xlsx文件
# 通过xlsx2csv
#
# OPTIONS: ---
# REQUIREMENTS: ---
# BUGS: ---
# NOTES: ---
# AUTHOR: YOUR NAME (),
# ORGANIZATION:
# CREATED: 2016/09/ 2 18:52
# REVISION: ---
#===============================================================================

set -o nounset # Treat unset variables as an error

## 生成csv文件
function xlsx_to_csv(){
## 删除所有旧版数据
for i in $(ls | grep "^xlsx2csv"); do
rm $i -f
done
file_name="$1"
xlsx2csv -s 0 -i "${file_name}" | awk '/^--------/{++i}{print > "xlsx2csv"i}'
}

## 显示
function show(){
file_name=$1
# cat $file_name | sed '2,10d' | sed '4,6d' | sed 's/,,,//g' | sed 's/,$//g' | column -t -s "," -o "|" | more
cat $file_name | sed -e '2,10d' | sed -e '4,6d' -e 's/,,,//g' -e 's/,$//g' | column -t -s "," -o "|" | more
}

## 显示subsheet
function list_file(){
for i in $(ls | grep "^xlsx2csv"); do
echo -n "$i "
grep -e "^--------" $i
done
}

## help
function help(){
echo "----------------------------"
echo "gen filenmae"
echo "list"
echo "show xlsx2csvN"
echo "----------------------------"
}

case $1 in
"gen") xlsx_to_csv "$2";;
"show") show $2;;
"list") list_file;;
*) help;;
esac

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Administrator@PC-20160616ZYHV /c/e/q/s/test# bash zm_xlsx.sh 1
----------------------------
gen filenmae
list
show xlsx2csvN
----------------------------
Administrator@PC-20160616ZYHV /c/e/q/s/test# bash zm_xlsx.sh gen "/cygdrive/e/qqjy/plan/004 配置表/039 送花配置表.xlsx"
Administrator@PC-20160616ZYHV /c/e/q/s/test# bash zm_xlsx.sh list
xlsx2csv1 -------- 1 - 送花配置表
Administrator@PC-20160616ZYHV /c/e/q/s/test# bash zm_xlsx.sh show xlsx2csv1
-------- 1 - 送花配置表
后端·数据类型 |num |str |num |num |num |num |num |num
后端·字段名 |id |name |tiem_id |intimacy |charm |sweet_target |sweet_me |message
字段名 |鲜花|名称 |对应道具ID|增加彼此亲密度|增加目标魅力值|增加目标甜蜜度|增加自身甜蜜度|系统推送消息
|1 |玫瑰*至真之恋|1290001 |2 |2 |1 |2 |69
|2 |玫瑰*白头之恋|1290002 |100 |100 |50 |100 |70
|3 |玫瑰*地久之恋|1290003 |600 |600 |300 |600 |71