#!/bin/bash
help() {
echo "
转换文本文件为xls文件
usage: file2xls [-F] [-t FS] file1 file2 ...
-F 强制所有字段为文本格式,不强制,0000会变成0
-t 指定分隔符,默认为\",\"
"
}
convert() {
awk -F "$FS" -v F_set="$F_set" '
BEGIN {
if (F_set==1) F_str=" style=mso-number-format:\"\\@\""
print ""
print "
"print ""
print ""
print "
"print "
}
{
for(i=1;i<=NF;i++) {
if (i==1) print "
"printf("
%s\n",F_str,$i)if (i==NF) printf("
\n")}
}
END {
print "
"print ""
print ""
}
' $1
}
#main
[[ $# -eq 0 || "$1"x = "-h"x || "$1"x = "--help"x || "$1"x = "-H"x ]] && help && exit
if [ "$1"x = "-F"x ];then
F_set=1
if [ "$2"x = "-t"x ];then
FS="$3"
Start_count=4
else
FS=","
Start_count=2
fi
else
F_set=0
if [ "$1"x = "-t"x ];then
FS="$2"
Start_count=3
else
FS=","
Start_count=1
fi
fi
Count=0
for File in $*
do
Count=$(expr $Count + 1)
if [ $Count -ge $Start_count ];then
printf "%s\t...\t" "$File"
if [ -e $File ];then
convert $File >${File}.xls
printf "%s\n" "[ done ]"
else
printf "%s\n" "[ not found ]"
fi
fi
done