`
wsql
  • 浏览: 11966354 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类
最新评论

VBS 将Txt 转成Excel,并加图片列头的处理...

阅读更多

'------------------------------------------------------------
'O2 Data File Transfer
'Get File type is txt, Change Transport to file type is xls
'
'Create Date: 2008-11-11
'Author: Wei_Zhu
'Chage Log:
'Last Chage Date:
'-------------------------------------------------------------

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

set ws=createobject("wscript.shell")
set fso=createobject("scripting.filesystemobject")
set folder=fso.getfolder(ws.currentdirectory&"\From")

set files=folder.files
for each file in files
ReadFile file.name,folder
MoveFile file.name,folder
next

'-------Read Data File-----

Sub ReadFile(lstg_file,folder)
Dim fso,f
dim lstg_from_file
dim lstg_f
dim lstg_f_txt
dim lstg_f_line
lstg_f_ling = 0
lstg_from_file=folder&"\"&lstg_file
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(lstg_from_file)
Set lstg_f = f.OpenAsTextStream(ForReading, TristateUseDefault)
While Not lstg_f.atEndOfLine
lstg_f_txt=lstg_f.readall
lstg_f_line=lstg_f.line 'Get File Line Count
msgbox lstg_f_line
lstg_f_txt=Replace(lstg_f_txt,"|",Chr(9))
WEnd
TransferFile lstg_f_txt,folder,lstg_file
End Sub


'-------Transfer Data File-----
Sub TransferFile(lstg_f_txt,folder,lstg_file)
dim lstg_to_f
Dim l_f
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")

lstg_to_f = folder
lstg_to_f = replace(lstg_to_f,"From","To")
lstg_file=UCase(lstg_file)
'---Get Report Type---

if instr(lstg_file,"ATWIP")=1 then
'msgbox "1"
l_f = "1"
end if
if instr(lstg_file,"WAO")=1 then
'msgbox "2"
l_f = "2"
end if
if instr(lstg_file,"WSC")=1 then
'msgbox "3"
l_f = "3"
end if
if instr(lstg_file,"SFAS")=1 then
'msgbox "4"
l_f = "4"
end if
lstg_file = replace(lstg_file,"TXT","xls")
lstg_to_f = lstg_to_f&"\"&lstg_file

Set f = fso.OpenTextFile(lstg_to_f, ForWriting, True)
f.Write lstg_f_txt
f.Close
'msgbox lstg_to_f
'msgbox lstg_f_txt

Set objExcel = CreateObject("Excel.Application")
'Set objWorkbook=objExcel.Workbooks.Add() 'This is add new
Set objWorkbook=objExcel.Workbooks.Open(lstg_to_f)

Select Case l_f
Case "1"
Set objRange =objExcel.Range("A1","A1")
objRange.EntireRow.Insert
objRange.EntireRow.Insert
'---Set Title---
Set objRange = objExcel.Range("A1","J1")
objRange.Font.Size = 14
objrange.Font.Bold = True
objrange.Font.Name = "Times New Roman"
objrange.Cells(1).value="Auto-WIP(SHOP FLOOR)"
objrange.Interior.ColorIndex = 15'Set BackColor
objrange.MergeCells = true
'objrange.HorizontalAlignment = -4108
objrange.HorizontalAlignment = 3
objrange.Merge

'---Set Header---
'JOBPART#LOT#DEPARTMENT_CODEQUEUE_QTYRUNNING(WIP)_QTYHOLD_QTYMOVE_PASS_QTYMOVE_FAIL_QTYWAFE_PCS

Set objRange = objExcel.Range("A2","J2")
objRange.Font.Size = 10
objrange.Font.Bold = True
objrange.Font.Name = "Times New Roman"
objrange.Cells(1).Value="JOB"
objrange.Cells(2).Value="PART#"
objrange.Cells(3).Value="LOT#"
objrange.Cells(4).Value="DEPARTMENT_CODE"
objrange.Cells(5).Value="QUEUE_QTY"
objrange.Cells(6).Value="RUNNING(WIP)_QTY"
objrange.Cells(7).Value="HOLD_QTY"
objrange.Cells(8).Value="MOVE_PASS_QTY"
objrange.Cells(9).Value="MOVE_FAIL_QTY"
objrange.Cells(10).Value="WAFE_PCS"
objrange.Interior.ColorIndex = 34'Set BackColor
objRange.Borders.LineStyle = 1
Set objRange = objExcel.ActiveCell.EntireColumn

objRange.AutoFit()
'----Fill Data----

'----Auto Fill The Column Width---
Set objcol = objExcel.Columns("A:J").EntireColumn
objcol.AutoFit
Case "2"
Set objRange =objExcel.Range("A1","A1")
objRange.EntireRow.Insert
objRange.EntireRow.Insert
'---Set Title---
Set objRange = objExcel.Range("A1","F1")
objRange.Font.Size = 14
objrange.Font.Bold = True
objrange.Font.Name = "Times New Roman"
objrange.Cells(1).value="O2M_AUTOWIP_FTP_TEMP"
objrange.Interior.ColorIndex = 15'Set BackColor
objrange.MergeCells = true
'objrange.HorizontalAlignment = -4108
objrange.HorizontalAlignment = 3
objrange.Merge

'---Set Header---
'JOBPRODUCTPROCESSOUT_QTYDATE_CODEREMARK

Set objRange = objExcel.Range("A2","F2")
objRange.Font.Size = 10
objrange.Font.Bold = True
objrange.Font.Name = "Times New Roman"
objrange.Cells(1).Value="JOB"
objrange.Cells(2).Value="PRODUCT"
objrange.Cells(3).Value="PROCESS"
objrange.Cells(4).Value="OUT_QTY"
objrange.Cells(5).Value="DATE_CODE"
objrange.Cells(6).Value="REMARK"
objrange.Interior.ColorIndex = 34'Set BackColor
objRange.Borders.LineStyle = 1
Set objRange = objExcel.ActiveCell.EntireColumn

objRange.AutoFit()
'----Fill Data----

'----Auto Fill The Column Width---
Set objcol = objExcel.Columns("A:J").EntireColumn
objcol.AutoFit
Case "3"
Set objRange =objExcel.Range("A1","A1")
objRange.EntireRow.Insert
objRange.EntireRow.Insert
'---Set Title---
Set objRange = objExcel.Range("A1","F1")
objRange.Font.Size = 14
objrange.Font.Bold = True
objrange.Font.Name = "Times New Roman"
objrange.Cells(1).value="O2MO WIP SCHEDULE CONFIRMED"
objrange.Interior.ColorIndex = 15'Set BackColor
objrange.MergeCells = true
'objrange.HorizontalAlignment = -4108
objrange.HorizontalAlignment = 3
objrange.Merge

'---Set Header---
'PRODUCT JOBPROCESSJOB_QTYLOT_NUMBERDATE_CONFIRMED
Set objRange = objExcel.Range("A2","F2")
objRange.Font.Size = 10
objrange.Font.Bold = True
objrange.Font.Name = "Times New Roman"
objrange.Cells(1).Value="PRODUCT"
objrange.Cells(2).Value="JOB"
objrange.Cells(3).Value="PROCESS"
objrange.Cells(4).Value="JOB_QTY"
objrange.Cells(5).Value="LOT_NUMBER"
objrange.Cells(6).Value="DATE_CONFIRMED"
objrange.Interior.ColorIndex = 34'Set BackColor
objRange.Borders.LineStyle = 1
Set objRange = objExcel.ActiveCell.EntireColumn

objRange.AutoFit()

'----Fill Data----

'----Auto Fill The Column Width---
Set objcol = objExcel.Columns("A:F").EntireColumn
objcol.AutoFit
Case "4"
Set objRange =objExcel.Range("A1","A1")
'---Insert 6 balnk row---
objRange.EntireRow.Insert
objRange.EntireRow.Insert
objRange.EntireRow.Insert
objRange.EntireRow.Insert
objRange.EntireRow.Insert
objRange.EntireRow.Insert

'---Insert Picedure--
objExcel.ActiveSheet.Pictures.Insert(ws.currentdirectory&"\logo\O2.png").select

Set objRange =objExcel.Range("G3","G3")
objRange.Font.Size = 16
objrange.Font.Bold = True
objrange.Font.Name = "Arial"
objRange.Value="Shop floor move transactions"

Set objRange =objExcel.Range("A5","B5")
objRange.Font.Size = 9
objrange.Font.Bold = False
objrange.Font.Name = "Times New Roman"
objRange.Cells(1).Value="Organization Code:"
objRange.Cells(2).Value="OMI"

Set objRange =objExcel.Range("I5","I6")
objRange.Font.Size = 10
objrange.Font.Bold = True
objrange.Font.Name = "Times New Roman"
objRange.Cells(1).Value="SF NO"
objRange.Cells(2).Value="PL NO"

Set objRange =objExcel.Range("J5","J6")
objRange.Font.Size = 10
objrange.Font.Bold = False
objrange.Font.Name = "Times New Roman"
objRange.Cells(1).Value="ASXXXXXXX"
objExcel.ActiveSheet.Range("F7","F7").Cut
objRange.Cells(2).Select
objExcel.ActiveSheet.Paste

Set objRange=objExcel.Range("A7","A7")
objRange.Select
objRange.EntireRow.Delete

Set objRange =objExcel.Range("A7","K7")
objRange.Font.Size = 12
objrange.Font.Bold = True
objrange.Font.Name = "Times New Roman"
objRange.Borders.LineStyle = 1

Set objRange =objExcel.Range("A8","K8")
objRange.Font.Size = 12
objrange.Font.Bold = False
objrange.Font.Name = "Times New Roman"
objRange.Borders.LineStyle = 1

Set objcol = objExcel.Columns("A:K").EntireColumn
objcol.AutoFit
End Select


REM Set objRange = objExcel.Range("A1","D1")
REM objRange.Font.Size = 11
REM objrange.Font.Bold = True
REM objRange.Borders.LineStyle = 1 ' 1~13 have 13line style

REM Set objRange = objExcel.ActiveCell.EntireColumn
REM objRange.AutoFit()

objExcel.DisplayAlerts = False 'Close the Alert
'objExcel.ActiveWorkBook.Saveas lstg_to_f,-4143
'msgbox lstg_to_f
objExcel.ActiveWorkBook.Saveas lstg_to_f,-4143
objExcel.DisplayAlerts = False ''Close the Save Alert
objExcel.ActiveWorkbook.Close
objExcel.DisplayAlerts = False
objExcel.Application.Quit

End Sub

'-------Move File For Transfer Bakup Source File-----
Sub MoveFile(lstg_file,bak_folder)
Dim fso
dim lstg_from_file
dim lstg_bak_folder
dim lstg_log_folder
dim log_msg
lstg_from_file=bak_folder&"\"&lstg_file
lstg_bak_folder=replace(bak_folder,"From","Bak")
lstg_log_folder=replace(bak_folder,"From","Log")
' Set f = fso.GetFolder(lstg_bak_folder)
set fso = CreateObject("Scripting.FileSystemObject")

lstg_bak_folder_1=lstg_bak_folder&"\"
If fso.FileExists(lstg_bak_folder_1&"\"&lstg_file) Then
'msgBox lstg_file&" exists"
log_msg =Date&" "&Time &" Transfer File [ "& lstg_file& " ] is exists !"
LogFile lstg_log_folder&"\"&Date&".log",log_msg
else
fso.MoveFile lstg_from_file,lstg_bak_folder_1
log_msg=Date&" "&Time &" Move File [ "& lstg_file& " ] is Success !"
LogFile lstg_log_folder&"\"&Date&".log",log_msg
end if
End Sub

'-------File Operation Log-----
Sub LogFile(lstg_file,log_msg)
Dim fso,f, LogFile
Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FileExists(lstg_file) Then
Set f = fso.GetFile(lstg_file)
Set LogFile = f.OpenAsTextStream(ForAppending, TristateUseDefault)
LogFile.WriteLine log_msg
LogFile.Close
else
Set LogFile = fso.CreateTextFile(lstg_file, True)
LogFile.WriteLine log_msg
LogFile.Close
end if
End Sub

---Result---

分享到:
评论

相关推荐

    vbs excel转pdf

    通过命令行测试 , 参数为excel路径和pdf目标路径。 可以通过java调用,实现自动转换。

    如何在WINCC中基于VBS实现对EXCEL文件中数据的读写?.docx

    本篇将详细介绍如何在WINCC中基于Visual Basic Script (VBS)实现对Excel文件中的数据进行读写操作。 首先,我们需要了解基础的VBS语法和WINCC的编程环境。VBS是一种弱类型的语言,常用于Windows系统的脚本编写,如...

    Wincc中通过VBS关闭已打开的excel文件

    在WinCC的应用场景中,经常需要通过集成外部程序来增强其功能,例如利用VBS(Visual Basic Scripting)来操作Excel文件,以便于进行数据处理和报表生成。 #### 关键问题 当在WinCC项目中使用VBS脚本来处理Excel报表...

    (转)VBS导excel.txt

    Set oExcel= CreateObject("Excel.Application") Set oWb = oExcel.Workbooks.Open("E:\其他\新装电话表.xls") Set oSheet = oWb.Sheets("Sheet1") MsgBox oSheet.Range("B2").Value '#提取单元格B2内容 '.....

    快速将txt文件转换为excel文件(2.10版)

    1快速将txt格式的数据文件转换为excel文件 2后缀名不一定是txt只要是文本格式的就行 3可以自己指定分隔符(tab,空格,或者用户自定义的符号) 4是本人写的vb版本的升级版,使用更加方便,加量不加价 O(∩_∩)O

    wincc7.4 用VBS将变量写入EXCEL,并能读取记录文件

    本教程将详细讲解如何利用Visual Basic Script(VBS)在WinCC 7.4中操作Excel,实现数据的写入和读取,这对于记录和分析生产数据至关重要。 首先,我们需要理解VBS的基本概念。VBS是Microsoft开发的一种脚本语言,...

    将vbs文件转换为bat文件

    3. **原理&说明&彩蛋.txt**:这是一个文本文件,详细解释了VBS转BAT的原理,可能包括如何解析VBS脚本、如何将VBS命令映射到批处理命令,以及可能存在的注意事项。此外,根据文件名中的“彩蛋”一词,可能还包含了...

    vbs操作excel全集

    本文将对vbs操作excel全集进行详细的说明和介绍。 使用动态创建的方法 使用VBScript可以动态创建Excel对象,从而控制和操作Excel应用程序。例如,可以使用以下代码创建Excel对象: `Set oExcel = CreateObject(...

    VBS基础.txt

    根据提供的文件信息,我们可以整理出关于VBS(Visual Basic Script Edition)的基础知识点,涉及VBS的概念、使用场景、基本语法等内容。 ### VBS是什么? VBS全称是Visual Basic Script Edition,是由微软开发的一...

    Excel 宏示例以及VBS转EXE工具

    这些工具可以将VBS源代码编译成无法被轻易查看或修改的EXE文件,提高代码的安全性。转换过程一般包括导入VBS脚本、设置编译选项(如图标、版权信息等)然后生成EXE。但要注意,生成的EXE可能在不同计算机上运行需要...

    excel列名转换为列数的vbs脚本

    在Excel中,列通常以字母表示(例如A、B、C等),而在编程或自动化处理过程中,有时需要将这些字母表示的列名转换为数字表示的列号(例如1、2、3等)。本文档介绍的VBS脚本(Visual Basic Script)能够实现这一功能...

    VBS操作Excel实例

    下面将详细介绍VBS操作Excel的相关知识点。 1. **创建Excel对象**: 在VBS中,首先我们需要创建一个Excel应用程序对象,以便与Excel进行通信。这可以通过`CreateObject`函数实现,例如: ```vbscript Set ...

    vbs跳过宏打开EXCEL

    本文将详细讨论如何使用VBScript(Visual Basic Scripting Edition)来跳过宏安全提示,直接打开包含宏的Excel文件,并提醒大家注意避免制作和传播可能含有恶意代码的文件。 标题“vbs跳过宏打开EXCEL”指的是使用...

    VBS转EXE工具.rar

    为了解决这个问题,我们可以利用“VBS转EXE工具”将VBS脚本转换成不可读的EXE可执行文件,从而提高代码的安全性。 标题中的"VBS转EXE工具.rar"指的是一个压缩包,包含了一款能够将VBS脚本打包成可执行文件(.exe)...

    VBS_VBS操作EXCLE排序

    本文将详细介绍如何利用 VBS 对 Excel 工作表中的数据进行排序。 #### 一、基础知识简介 1. **VBS 简介**: - VBS 是一种基于 Visual Basic 的轻量级脚本语言。 - 它主要用于编写 Windows 环境下的脚本,常用于...

    Excel转Testlink测试用例.vbs

    网上找了很多转换工具,java的安装比较复杂,vbs的缺失多步骤转换,我在vbs工具的基础上增加了多步骤,经测试OK,但该vbs适合testlink1.9以上版本,因为不同版本testlink的测试用例模板可能不一样,需要处理的列字段...

Global site tag (gtag.js) - Google Analytics