论坛首页 Web前端技术论坛

从txt文件中导出内容到word表格中

浏览 4279 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-05-20   最后修改:2010-03-18
最近无聊,看到个要把txt中的内容导入到word中的需求,当然了,word里是带表格的,要把txt中的内容解析后再分别放在word表格的指定位置,用java来做?想都没想,VBS正好可以干这个活啊!
test.html
<html>
	<head>
		<title>test</title>
	</head>
	<body>
		<table align="center" border="1">
			<tr>
				<td colspan="4" align="center">导出txt内容到word文件<br></td>
			</tr>
			<tr>
				<td>选择要导出<br>的txt文件</td>
				<td width="80%"><input type="file" id="txtFile"></td>
			</tr>
			<tr>
				<td>选择要导出<br>的word文件</td>
				<td width="80%"><input type="file" id="wordFile"></td>
			</tr>
			<tr>
					<td colspan="4" align="center"><input type="button" value="开始" onclick="chick()"></td>
			</tr>
		</table>
	</body>
</html>

<SCRIPT LANGUAGE="vbscript">
		Dim txtFile
		Dim wordFile
		function chick()
			txtFile = document.getElementById("txtFile").value
			wordFile = document.getElementById("wordFile").value
			
			If Len(trim(txtFile)) = 0 Or Len(trim(wordFile)) = 0 Then
				MsgBox "请选择文件!"
			Else
				dowrite()
			End If
			
		end function
		
		function dowrite()
			Set fso = CreateObject("Scripting.FileSystemObject")
			Dim strAry()
			Dim linCount
			Dim strLine
			ReDim strAry(1000)
			
			Set TxtFile = fso.OpenTextFile(txtFile,1, False)
  		
  		While Not TxtFile.AtEndOfStream
  			strLine = TxtFile.ReadLine
  			If Len(strLine) > 0 Then
  				strAry(linCount) = strLine
  				linCount = linCount + 1
  			End If
  		Wend
		  
		  Set myDocApp = CreateObject("Word.Application")
		  myDocApp.Visible = True
		  myDocApp.Activate
		  myDocApp.Application.ScreenUpdating = False
		  set myDoc = myDocApp.Documents.Open(wordFile)
		  
		  Set objSelection = myDocApp.Selection
			For i = 0 To linCount-1
				objSelection.Font.Name = "黑体"
				objSelection.Font.Size = 22
				objSelection.ParagraphFormat.Alignment = 1
				objSelection.ParagraphFormat.LineSpacingRule = 0
				objSelection.Font.Bold = true
			
				objSelection.TypeText "检测报告单"&vbCrLf
				
				objSelection.Font.Size = 12
				objSelection.Font.Bold = false
				
				Set table1 = objSelection.Tables.Add(objSelection.Range, 14, 6)
				Set Table1 = myDoc.Tables(i+1)
				With Table1
					.PreferredWidthType = 2
					.PreferredWidth = 100
					.Columns.PreferredWidthType = 2
					With .Borders(-2)
            .LineStyle = 1
            .LineWidth = 4
            .Color = -16777216
	        End With
	        With .Borders(-4)
	            .LineStyle = 1
            .LineWidth = 4
            .Color = -16777216
	        End With
	        With .Borders(-1)
	            .LineStyle = 1
            .LineWidth = 4
            .Color = -16777216
	        End With
	        With .Borders(-3)
	            .LineStyle = 1
            .LineWidth = 4
            .Color = -16777216
	        End With
	        With .Borders(-5)
	            .LineStyle = 1
            .LineWidth = 4
            .Color = -16777216
	        End With
	        .Borders(-7).LineStyle = 0
        	.Borders(-8).LineStyle = 0
        	.Borders.Shadow = False
	    	End With
	    	With myDocApp.Options
        	.DefaultBorderLineStyle = 1
        	.DefaultBorderLineWidth = 4
        	.DefaultBorderColor = -16777216
    		End With		
				'合并单元格 开始
				
				'第一行合并单元格
				objSelection.MoveRight 1, 6, 1
				objSelection.Cells.Merge
				
				'第五行合并单元格
				objSelection.MoveRight 1,2
				objSelection.MoveDown 5,3
				objSelection.MoveRight 1, 6, 1
				objSelection.Cells.Merge
				
				'第六行的第一二列合并单元格
				objSelection.MoveRight 1,2
				objSelection.MoveRight 1, 2, 1
				objSelection.Cells.Merge
				'第六行的第三四列合并单元格
				objSelection.MoveRight 1,1
				objSelection.MoveRight 1, 2, 1
				objSelection.Cells.Merge
				
				'第六行的第五六列合并单元格
				objSelection.MoveRight 1,1
				objSelection.MoveRight 1, 2, 1
				objSelection.Cells.Merge
				
				'第七行的第一二列合并单元格
				objSelection.MoveRight 1,2
				objSelection.MoveRight 1, 2, 1
				objSelection.Cells.Merge
				'第七行的第三四列合并单元格
				objSelection.MoveRight 1,1
				objSelection.MoveRight 1, 2, 1
				objSelection.Cells.Merge
				'第七、八、九行的第五六列合并单元格
				objSelection.MoveRight 1,1
				objSelection.MoveRight 1, 2, 1
				objSelection.MoveDown 5,2,1
				objSelection.Cells.Merge
				
				'第八行的第一二列合并单元格
				objSelection.MoveRight 1,2
				objSelection.MoveRight 1, 2, 1
				objSelection.Cells.Merge
				'第八行的第三四列合并单元格
				objSelection.MoveRight 1,1
				objSelection.MoveRight 1, 2, 1
				objSelection.Cells.Merge
				
				'第九行的第一二列合并单元格
				objSelection.MoveRight 1,3
				objSelection.MoveRight 1, 2, 1
				objSelection.Cells.Merge
				'第九行的第三四列合并单元格
				objSelection.MoveRight 1,1
				objSelection.MoveRight 1, 2, 1
				objSelection.Cells.Merge
				
				'第十行合并单元格
				objSelection.MoveRight 1,3
				objSelection.MoveRight 1, 6, 1
				objSelection.Cells.Merge
				
				'第十一、十二、十三、十四行合并单元格
				objSelection.MoveRight 1,2
				objSelection.MoveRight 1, 6, 1
				objSelection.MoveDown 5,3,1
				objSelection.Cells.Merge
				
				'合并单元格 结束

				'填写内容 开始
				Table1.Cell(1,1).Range.Text = "基本信息"
				
				Table1.Cell(2,1).Range.Text = "样品编号"
				Table1.Cell(2,2).Range.Text = Mid(strAry(i),21,15)
				Table1.Cell(2,3).Range.Text = "姓名"
				Table1.Cell(2,4).Range.Text = ""
				Table1.Cell(2,5).Range.Text = "性别"
				Table1.Cell(2,6).Range.Text = ""
				
				Table1.Cell(3,1).Range.Text = "年    龄"
				Table1.Cell(3,2).Range.Text = ""
				Table1.Cell(3,3).Range.Text = "病历号"
				Table1.Cell(3,4).Range.Text = ""
				Table1.Cell(3,5).Range.Text = "床位号"
				Table1.Cell(3,6).Range.Text = ""
				
				Table1.Cell(4,1).Range.Text = "送检日期"
				Table1.Cell(4,2).Range.Text = Mid(strAry(i),6,15)
				Table1.Cell(4,3).Range.Text = "临床诊断"
				Table1.Cell(4,4).Range.Text = ""
				Table1.Cell(4,5).Range.Text = ""
				Table1.Cell(4,6).Range.Text = ""
				
				Table1.Cell(5,1).Range.Text = "检测结果"
				
				Table1.Cell(6,1).Range.Text = "指标"
				Table1.Cell(6,2).Range.Text = "检测值"
				Table1.Cell(6,3).Range.Text = "阴阳性"
				
				Table1.Cell(7,1).Range.Text = "0分钟(T0)"
				Table1.Cell(8,1).Range.Text = "20分钟(T1)"
				Table1.Cell(9,1).Range.Text = "差值"
				
				Table1.Cell(9,2).Range.Text = Right(Left(strAry(i),5),4)
				
				Table1.Cell(7,3).Range.Text = "阴性(<4.0)"&vbCrLf&"阳性(≥4.0)"
				
				Table1.Cell(10,1).Range.Text = "结果评价"
				objSelection.ParagraphFormat.Alignment = 3
				Table1.Cell(11,1).Range.Text = ""&vbCrLf&"13C-UREA呼气试验Hp结果为:"
				
				'填写内容 结束
				
				objSelection.EndKey(6)
				objSelection.ParagraphFormat.Alignment = 3		
				objSelection.TypeText "检验人:                                        检验日期:"&vbCrLf
				If (i+1) Mod 2 = 1 Then
					objSelection.TypeText ""&vbCrLf&"-------------------剪------------- 切---------------线---------------"&vbCrLf	
				End If
			Next
			myDoc.close()
			myDocApp.quit()
		end function
</SCRIPT>



附:
txt文件内容格式
test.txt
D   32009-05-1513:2009051503MJ       0.5NEGATIVE 2.1 2.3 4.000
D   42009-05-1513:2309051504DYD      1.0NEGATIVE 2.1 2.6 4.000
D   12009-05-1514:520136            12.3POSITIVE 2.2 2.5 4.000
D   22009-05-1514:550078            -0.2NEGATIVE 1.4 1.7 4.000
D   32009-05-1514:580001            -0.7NEGATIVE 2.3 2.6 4.000
   发表时间:2009-05-22   最后修改:2009-05-22

lz没有说明的几个问题。
1、必须是windows+office2000-2007
2、降低ie的安全性
3、如果提示创建对象错误,请在运行输入regsvr32 scrrun.dll

---------

ps:以上代码可以容易的转成 JavaScript
0 请登录后投票
   发表时间:2009-06-01  
onecainiao 写道

lz没有说明的几个问题。
1、必须是windows+office2000-2007
2、降低ie的安全性
3、如果提示创建对象错误,请在运行输入regsvr32 scrrun.dll

---------

ps:以上代码可以容易的转成 JavaScript

多谢楼上补充
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics