文本文件转成xls文件

#!/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 "<HTML>"
    print "<HEAD>"
    print "<META http-equiv=Content-Type content=\"text/html;charset=gb2312\">"
    print "</HEAD>"
    print "<BODY>"
    print "<TABLE border=\"1\">"
}

{
    for(i=1;i<=NF;i++) {
        if (i==1)  print "<TR>"
        printf("<TD nowrap%s>%s</TD>\n",F_str,$i)
        if (i==NF) printf("</TR>\n")
    }
}

END {
    print "</TABLE>"
    print "</BODY>"
    print "</HTML>"
}
' $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

你可能感兴趣的:(文本文件转成xls文件)