免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1200 | 回复: 1
打印 上一主题 下一主题

[学习共享] 如何把文本文件中的内容转换成Excel格式化打开? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2017-02-05 19:10 |只看该作者 |倒序浏览
有一个文本文件(.txt),希望把其中的数据转化成可以在Excel打开方便浏览;
其中文本文件的分隔符可以是分号、逗号,空格或其它字符;

处理过程

#!/bin/bash
#create a xml

fgf=$3

function format()
{
          rm tan.txt
          cat $1 | while read line
          do
              echo "<Row ss:AutoFitHeight=\"0\" ss:StyleID=\"s62\">" >> tan.txt
               echo $line | awk -F$fgf '{for(i=1;i<=NF;i++){print "<Cell><Data ss:Type=\"String\">" $i "</Data></Cell>"}}' >> tan.txt
             echo "</Row>" >> tan.txt
         done
}

#judgement the argument number of input
if [ ! $# -eq 3 ]
then
echo bash srcfile desfile separator
exit
fi

if [ -f $2 ]
then
    echo $2 is exist Fail to xml
    else
    #get the file of row and col
    row=`wc -l $1 | cut -d" " -f1`
    col=`awk -F$fgf '{print NF}' $1 | sort -rn | head -1`

    #format the file with xml
    format $1   

    argv1=head.xml
    argv3=styleend.xml
    argv4=tan.txt

    cat $argv1 > $2
    cat $argv4 >> $2
    cat $argv3 >> $2
   
    #change the col and row
    sed -i "/ExpandedColumnCount=/ {s/3/$col/; s/2/$row/}" $2

    echo $2 Success
fi


head.xml文件内容:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-comffice:spreadsheet"
xmlns="urn:schemas-microsoft-comfficeffice"
xmlns="urn:schemas-microsoft-comffice:excel"
xmlns:ss="urn:schemas-microsoft-comffice:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-comfficeffice">
  <Author>Administrator</Author>
  <LastAuthor>Administrator</LastAuthor>
  <Created>2017-02-05T02:58:29Z</Created>
  <Company>Microsoft</Company>
  <Version>14.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-comfficeffice">
  <AllowPNG/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  <WindowHeight>7605</WindowHeight>
  <WindowWidth>19395</WindowWidth>
  <WindowTopX>600</WindowTopX>
  <WindowTopY>135</WindowTopY>
  <rotectStructure>False</ProtectStructure>
  <rotectWindows>False</ProtectWindows>
</ExcelWorkbook><Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Center"/>
   <Borders/>
   <Font ss:FontName="Arial" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
   <Interior/>
   <NumberFormat/>
   <rotection/>
  </Style>
  <Style ss:ID="s62">
   <Font ss:FontName="Microsoft JhengHei" x:CharSet="136" x:Family="Swiss"
    ss:Size="11" ss:Color="#000000"/>
  </Style>
</Styles>
<Worksheet ss:Name="Sheet1">
  <Table ss:ExpandedColumnCount="3" ss:ExpandedRowCount="2" x:FullColumns="1"
   x:FullRows="1" ssefaultColumnWidth="54" ssefaultRowHeight="13.5">

styleend.xml内容:
</Table>
  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
   <ageSetup>
    <Header x:Margin="0.3"/>
    <Footer x:Margin="0.3"/>
    <ageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
   </PageSetup>
   <Unsynced/>
   <rint>
    <ValidPrinterInfo/>
    <aperSizeIndex>9</PaperSizeIndex>
    <HorizontalResolution>600</HorizontalResolution>
    <VerticalResolution>600</VerticalResolution>
   </Print>
   <Selected/>
   <anes>
    <ane>
     <Number>3</Number>
     <ActiveRow>2</ActiveRow>
     <ActiveCol>1</ActiveCol>
    </Pane>
   </Panes>
   <rotectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions>
</Worksheet>
</Workbook>





2017-02-05_190151.png (36.23 KB, 下载次数: 28)

处理前及过程

处理前及过程

2017-02-05_190219.png (28.73 KB, 下载次数: 25)

处理后打开效果

处理后打开效果

论坛徽章:
0
2 [报告]
发表于 2017-02-07 13:29 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP