有时候我们需要把txt文档或者word文档中的资料导入到excel中去,但是采用copy/paste方式将是非常麻烦的。那么有没有办法采用 自动化方式来导入资料到excle中呢?AutoIt提供了一种非常方便的解决方案。
AutoIt提供了一个写excel的函 数:_ExcelWriteCell($oExcel, $sValue, $sRangeOrRow, $iColumn = 1)
第一 个参数为$oExcel = _ExcelBookNew()或者$oExcel = _ExcelBookOpen()返回的对象。第二个参数是填入excel的数值,第三个和第四个参数是定义填入的位置。 所以我们只要用一个fileopen()函数读入资料数据,再根据要求填入相应的位置就可以了。 用一个循环语句就可以非常轻松的完成文档的copy了哦。下面这个是我下的一个脚本,用于从txt文档中导出数据到excel中。非常的方便。
#include <Excel.au3>
$path = $Cmdline[1]
$saveas = $Cmdline[2]
$nHtmlFile = FileOpen($path, 0) ; temp.txt 为你缩写的txt格式的case
$oExcel = _ExcelBookNew()
$low = 1
$string = ""
$temp = "[Preconditions]" & @CRLF & @CRLF & "[Steps]" & @CRLF & @CRLF & "[Expected Results]" & @CRLF & @CRLF
$flag = 1
While 1
$line = FileReadLine($nHtmlFile)
If @error = -1 Then
FileClose($nHtmlFile)
_ExcelWriteCell($oExcel, $string, $low, 2)
_ExcelBookSaveAs($oExcel, $saveas, "xls", 0, 1) ; Now we save it into the temp directory; overwrite existing file if necessary
_ExcelBookClose($oExcel, 1, 0) ; And finally we close out
Exit
EndIf
$stringleft1 = StringLeft($line, 5)
If StringInStr($stringleft1, '>') Then
If $flag = 1 Then
_ExcelWriteCell($oExcel, $line, $low + 1, 1) ;Write to the Cell
Else
_ExcelWriteCell($oExcel, $temp, $low, 2)
$flag = 1
_ExcelWriteCell($oExcel, $line, $low + 1, 1)
EndIf
Else
Do
$string &= $line & @CRLF
$line = FileReadLine($nHtmlFile)
If @error = -1 Then
FileClose($nHtmlFile)
_ExcelWriteCell($oExcel, $string, $low, 2)
_ExcelBookSaveAs($oExcel, $saveas, "xls", 0, 1) ; Now we save it into the temp directory; overwrite existing file if necessary
_ExcelBookClose($oExcel, 1, 0) ; And finally we close out
Exit
EndIf
$stringleft1 = StringLeft($line, 10)
_ReduceMemory(@AutoItPID)
Until StringInStr($stringleft1, '>')
_ExcelWriteCell($oExcel, $string, $low, 2) ;Write to the Cell
$string = ""
_ExcelWriteCell($oExcel, $line, $low + 1, 1) ;Write to the Cell
$flag = 2
EndIf
$low = $low + 1
WEnd