请问:AU3能实现将txt文件转换成xls文件吗?
下面是VB的实现代码:
将一个文本文件导入一个单元格为左上角的工作表中
Sub Txt2Xls(strTxtFile As String, oCell As Excel.Range)
'strTxtFile 完整的txt文件路径
'oCell Excel单元格对象,将以这个单元格作为填充左上角
Dim oCn As Connection
Dim oRs As Recordset
Dim strSql As String
Dim nPos As Integer
Dim strFolder As String
Dim strTable As String
'将文件拆分为目录和文件名
nPos = InStrRev(strTxtFile, "\")
strFolder = Left(strTxtFile, nPos)
strTable = Right(strTxtFile, nPos)
'将文件后缀前的"."改为"#" 比如 try.txt->try#txt
nPos = InStrRev(strTable, ".")
Mid(strTable, nPos) = "#"
'将文本文件按照数据库打开
Set oCn = New Connection
With oCn
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _
& strFolder _
& ";Extended Properties=text;Persist Security Info=False"
.Open
End With
'打开文件
strSql = "select * from " & strTable
Set oRs = New Recordset
oRs.Open strSql, oCn
'用Recordset填充cell
oCell.CopyFromRecordset oRs
oRs.Close
oCn.Close
End Sub
用excel按照打开文本文件的方式打开,然后存盘为excel工作簿
Sub OpenTxtinExcel(ByVal strTxtFileName As String)
Dim oApp As Excel.Application
Dim oWorkbooks As Excel.Workbooks
Dim bIsOpenSuccess As Boolean
Dim nIndex As Long
Dim nPos As Integer
Dim strExEcelFilename As String
'打开excel
On Error Resume Next
Set oApp = GetObject(, "Excel.Application")
If oApp Is Nothing Then
Set oApp = CreateObject("Excel.Application")
End If
On Error GoTo 0
oApp.Visible = True
Set oWorkbooks = oApp.Workbooks
'用excel打开文本文件
oWorkbooks.OpenText strTxtFileName, StartRow:=1, DataType:= _
xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, Comma:=True, Space:=False, Other:=False
'新建文件名
nPos = InStrRev(strTxtFileName, ".")
strExEcelFilename = Left(strTxtFileName, nPos) & ".xls"
'存盘
nIndex = oWorkbooks.Count
oWorkbooks(nIndex).SaveAs strExEcelFilename, xlNormal
oWorkbooks(nIndex).Close
End Sub
[ 本帖最后由 menfan 于 2009-3-25 11:13 编辑 ] |