取消EXCEL表中的单引号
经常需要把单引号去了,我写了宏,但不知怎么放进去使用auto去完成,给到其他人方便使用,因为怕他们不用使用宏。取消B和D列中数字有单引号,并要求单元格为文本格式,不然,数字前面的零为没了,还有大长的数字会变成科学数字了
------以下的是EXCEL中的宏Sub a()
Columns("B:D").Select
Selection.NumberFormatLocal = "@"
'无把B:D列单元格设为文本型
srcnum = Range("B2").CurrentRegion.Rows.Count
Dim i As Integer
For i = 2 To srcnum
For j = 0 To 2
If Left(Cells(i, 2 + j), 1) = "'" Then
Cells(i, 2 + j) = Right(Cells(i, 2 + j), Len(Cells(i, 2 + j)) - 1) '取消单引号
Else
j = j + 1 '否则查下一个
End If
Next
Next
End Sub
------想应到到autoit中做成浏览打开自动保存为别的
先感谢大家了
附件是个文本文件?! 不是文本来的,是xls来的 回复 2# kevinch
不是文本来的,是xls来的 回复 4# lzlcn
是文本文件来着,只是改了扩展名,如果是文本文件的话,直接stringreplace一下就行了,如果真的是excel文件的话,你的代码是无效的 是文本文件,你这个应该是软件中导出的吧,我就碰到一个软件是这样的,导出的是xls,其实就是txt文件
试试这个代码
#Include <File.au3>
$file1=@ScriptDir&"\11.xls"
$file2=@ScriptDir&"\11_0.xls"
build($file1,$file2)
MsgBox(0,"","生成")
Func build($file_read,$file_build)
Dim $count_str
$lines=_FileCountLines($file_read)
For $i=2 To $lines
$read=FileReadLine($file_read,$i)
$count_str_1=mid($read)
$count_str=$count_str&$count_str_1
Next
$all_str=top($lines)&$count_str&end()
$f=FileOpen($file_build,138)
FileWrite($f,$all_str)
FileClose($f)
EndFunc
Func top($line)
$str='<?xml version="1.0"?>'&@CRLF
$str=$str&'<?mso-application progid="Excel.Sheet"?>'&@CRLF
$str=$str&'<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"'&@CRLF
$str=$str&' xmlns:o="urn:schemas-microsoft-com:office:office"'&@CRLF
$str=$str&' xmlns:x="urn:schemas-microsoft-com:office:excel"'&@CRLF
$str=$str&' xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"'&@CRLF
$str=$str&' xmlns:html="http://www.w3.org/TR/REC-html40">'&@CRLF
$str=$str&' <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">'&@CRLF
$str=$str&'<Created>1996-12-17T01:32:42Z</Created>'&@CRLF
$str=$str&'<LastSaved>2014-04-16T15:37:49Z</LastSaved>'&@CRLF
$str=$str&'<Version>11.5606</Version>'&@CRLF
$str=$str&' </DocumentProperties>'&@CRLF
$str=$str&' <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">'&@CRLF
$str=$str&'<RemovePersonalInformation/>'&@CRLF
$str=$str&' </OfficeDocumentSettings>'&@CRLF
$str=$str&' <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">'&@CRLF
$str=$str&'<WindowHeight>4530</WindowHeight>'&@CRLF
$str=$str&'<WindowWidth>8505</WindowWidth>'&@CRLF
$str=$str&'<WindowTopX>480</WindowTopX>'&@CRLF
$str=$str&'<WindowTopY>120</WindowTopY>'&@CRLF
$str=$str&'<AcceptLabelsInFormulas/>'&@CRLF
$str=$str&'<ProtectStructure>False</ProtectStructure>'&@CRLF
$str=$str&'<ProtectWindows>False</ProtectWindows>'&@CRLF
$str=$str&' </ExcelWorkbook>'&@CRLF
$str=$str&' <Styles>'&@CRLF
$str=$str&'<Style ss:ID="Default" ss:Name="Normal">'&@CRLF
$str=$str&' <Alignment ss:Vertical="Bottom"/>'&@CRLF
$str=$str&' <Borders/>'&@CRLF
$str=$str&' <Font ss:FontName="宋体" x:CharSet="134" ss:Size="12"/>'&@CRLF
$str=$str&' <Interior/>'&@CRLF
$str=$str&' <NumberFormat/>'&@CRLF
$str=$str&' <Protection/>'&@CRLF
$str=$str&'</Style>'&@CRLF
$str=$str&'<Style ss:ID="s21">'&@CRLF
$str=$str&' <NumberFormat ss:Format="@"/>'&@CRLF
$str=$str&'</Style>'&@CRLF
$str=$str&'<Style ss:ID="s22">'&@CRLF
$str=$str&' <NumberFormat ss:Format="Short Date"/>'&@CRLF
$str=$str&'</Style>'&@CRLF
$str=$str&'<Style ss:ID="s23">'&@CRLF
$str=$str&' <NumberFormat ss:Format="General Date"/>'&@CRLF
$str=$str&'</Style>'&@CRLF
$str=$str&' </Styles>'&@CRLF
$str=$str&' <Worksheet ss:Name="Sheet1">'&@CRLF
$str=$str&'<Table ss:ExpandedColumnCount="10" ss:ExpandedRowCount="35" x:FullColumns="1"'&@CRLF
$str=$str&' x:FullRows="1" ss:DefaultColumnWidth="54" ss:DefaultRowHeight="14.25">'&@CRLF
$str=$str&' <Row>'&@CRLF
$str=$str&' <Cell><Data ss:Type="String">生产单位</Data></Cell>'&@CRLF
$str=$str&' <Cell><Data ss:Type="String">生产单位编码</Data></Cell>'&@CRLF
$str=$str&' <Cell><Data ss:Type="String">物料条码</Data></Cell>'&@CRLF
$str=$str&' <Cell><Data ss:Type="String">物料代码</Data></Cell>'&@CRLF
$str=$str&' <Cell><Data ss:Type="String">物料名称</Data></Cell>'&@CRLF
$str=$str&' <Cell><Data ss:Type="String">版本</Data></Cell>'&@CRLF
$str=$str&' <Cell><Data ss:Type="String">版本号</Data></Cell>'&@CRLF
$str=$str&' <Cell><Data ss:Type="String">生产日期</Data></Cell>'&@CRLF
$str=$str&' <Cell><Data ss:Type="String">注册时间</Data></Cell>'&@CRLF
$str=$str&' <Cell><Data ss:Type="String">注册人</Data></Cell>'&@CRLF
$str=$str&' </Row>'&@CRLF
Return $str
EndFunc
Func mid($strings)
$arr=StringSplit($strings,@TAB)
$str=' <Row>'&@CRLF
$str=$str&' <Cell><Data ss:Type="String">'&$arr&'</Data></Cell>'&@CRLF
$str=$str&' <Cell ss:StyleID="s21"><Data ss:Type="String">'&StringTrimLeft($arr,1)&'</Data></Cell>'&@CRLF
$str=$str&' <Cell ss:StyleID="s21"><Data ss:Type="String">'&StringTrimLeft($arr,1)&'</Data></Cell>'&@CRLF
$str=$str&' <Cell ss:StyleID="s21"><Data ss:Type="String">'&StringTrimLeft($arr,1)&'</Data></Cell>'&@CRLF
$str=$str&' <Cell><Data ss:Type="String">'&$arr&'</Data></Cell>'&@CRLF
$str=$str&' <Cell><Data ss:Type="String">'&$arr&'</Data></Cell>'&@CRLF
$str=$str&' <Cell><Data ss:Type="Number">'&$arr&'</Data></Cell>'&@CRLF
$str=$str&' <Cell><Data ss:Type="String">'&$arr&'</Data></Cell>'&@CRLF
$str=$str&' <Cell><Data ss:Type="String">'&$arr&'</Data></Cell>'&@CRLF
$str=$str&' <Cell><Data ss:Type="Number">'&$arr&'</Data></Cell>'&@CRLF
$str=$str&' </Row>'&@CRLF
Return $str
EndFunc
Func end()
$str='</Table>'&@CRLF
$str=$str&'<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">'&@CRLF
$str=$str&' <Selected/>'&@CRLF
$str=$str&' <ProtectObjects>False</ProtectObjects>'&@CRLF
$str=$str&' <ProtectScenarios>False</ProtectScenarios>'&@CRLF
$str=$str&'</WorksheetOptions>'&@CRLF
$str=$str&' </Worksheet>'&@CRLF
$str=$str&'</Workbook>'&@CRLF
Return $str
EndFunc 回复 6# gto250
是从客户系统中导出来的文挡,确实如你们说的假XSL文挡来的,由txt组成
谢谢你写的代码,我试了,也不行!
使用autoit实现确实有点难,我后面使用VBA去实现了,并可以多个文件处理,还是微软原本软件的去弄好弄些。
有想了解的,可以附件下来看看 回复 7# lzlcn
代码不行,还是不成功?我这里测试是成功的,你可以详细说明一下吗? 我看过你excel中的代码了,其实au3也可以实现操作excel,而且也不需要再用一个excel文件
页:
[1]