最近无聊,看到个要把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
分享到:
相关推荐
从PB中导出数据到WORD文件中 从PB中导出数据到WORD文件中
Java读取Word中的表格(Excel),并导出文件为Excel
用word编好格式,另存为xml文件,修改变量,修改成.ftl模版,然后赛数据导出。
C# winform,打开CAD文件,导出CAD到Word 导入Excel文件,将数据显示在表格中 导出数据到word文件中 direct3d图形显示
网络上的根据模板填充Word我都看过一些, 它们的功能在数据换行的时候用的是run对象的.addCarriageReturn()方法,或者是直接用\n实现换行。这些都不符合我的需求, 因为我要的是分段,而不是换行。换行的word导致另一...
windows下,使用python 将Excel中数据批量导出到Word模板中生成新的文件
一个使用Apache的poi使用word模板循环输出表格行并导出的例子
将系统中富文本编辑器内容导出到word文件(包含图片)
pb数据窗口 导出word文件 对刚刚学习powerbuilder的人希望有帮助。谢谢大家
使用JAVASCRIPT导出页面里的任意一个Table里的内容到Word或Excel.而且还保留了Table在页面里的样式。
由于项目需要,关于js将table转为excel,纠结了好久,在网上找了很多资料,前面也上传了两个版本,后来发现问题很多,到目前为止这个版本是兼容性最好。可以自定义文件名,下载表格有边框,有标题,欢迎下载;
Java 导出 Word。 可直接运行,有说明文档,有注释,有jar文件。
对word操作,画表格,在单元格设计特别的字体颜色等等,具体教程请搜博客,标题
delphi 导出word表格文字
JAVA根据数据库表生成word表格文档
改文件中有基于bootstrap的demo,和用于其他前端框架的js文件,文件夹中有相应的说明,其他的我也不知道有什么好说的了
根据word模板导出word、...本资源中集成了 根据word模板导出word功能 、合并多个word功能 、word转换成pdf功能 、文件删除功能能。 其中坐标以$开头为一对一查询结果替换 $t为表格模板开头标识 $p为图片模板开头标识
本工具可以根据自定义模板,自动将Excel表格中每一行的数据填充到Word文档中,每行导出一个word。
根据业务需求需要,需要将json格式的api信息【比如postman导出接口文档这类的】,导出成标准接口文档的word文件。 该平台是将一些好的第三方平台接口接入进来,供用户使用,每个用户下有可以使用的接口,可以根据...
stax-api-1.0.1.jar xmlbeans-2.4.0.jar ooxml-schemas-1.0.jar 这三个主要是java导出为word文件的jar包