本文首发于我的Blog:http://nice90.blogone.net http://www.blogcn.com/user2/nice90/main.asp?id=1657409 buildDoc函数在网络上很出名了,这个函数是将web页面中某个Table的内容全部导入到Word文档中进行分页打印。但是有一个缺陷是原来的buildDoc程序生成的Table在打印的时候不会打印出来Table的Border。于是我将该程序进行了修改,使得打印的时候能够打印出来黑色的Border。主要是在程序中加入了下面两句: 以下内容为程序代码:
objWordDoc.Application.ActiveDocument.Tables(1).Borders.InsideLineStyle = True objWordDoc.Application.ActiveDocument.Tables(1).Borders.OutsideLineStyle = True
完整的程序如下:toWord.htm
<html> <head> <title>Build Document by Script</TITLE> </HEAD> <body> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/html/output/F1/D4/S5AB0B.asp <br> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/html/output/F1/D4/S5AC00.asp <br> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/html/output/F1/D4/S5A856.asp <br> <Table id="myData" border="0" cellpadding="5" cellspacing="1" bgcolor="#000000"> <Tr align=center bgcolor="#ffffff"> 产品名称</ 产品描述</ 产品单价</ 产品等级</ </Tr> <Tr align=center bgcolor="#ffffff"> 产品一</ This is a test</ <Td align=right>300.50</ 一级</ </Tr> <Tr align=center bgcolor="#ffffff"> 产品二</ This is a test</ <Td align=right>300.50</ 一级</ </Tr> <Tr align=center bgcolor="#ffffff"> 产品三</ This is a test</ <Td align=right>300.50</ 一级</ </Tr> <Tr align=center bgcolor="#ffffff"> 产品一</ This is a test</ <Td align=right>300.50</ 一级</ </Tr> <Tr align=center bgcolor="#ffffff"> 产品二</ This is a test</ <Td align=right>300.50</ 一级</ </Tr> <Tr align=center bgcolor="#ffffff"> 产品三</ This is a test</ <Td align=right>300.50</ 一级</ </Tr> <Tr align=center bgcolor="#ffffff"> 产品一</ This is a test</ <Td align=right>300.50</ 一级</ </Tr> <Tr align=center bgcolor="#ffffff"> 产品二</ This is a test</ <Td align=right>300.50</ 一级</ </Tr> <Tr align=center bgcolor="#ffffff"> 产品三</ This is a test</ <Td align=right>300.50</ 一级</ </Tr> <Tr align=center bgcolor="#ffffff"> 产品一</ This is a test</ <Td align=right>300.50</ 一级</ </Tr> <Tr align=center bgcolor="#ffffff"> 产品二</ This is a test</ <Td align=right>300.50</ 一级</ </Tr> <Tr align=center bgcolor="#ffffff"> 产品三</ This is a test</ <Td align=right>300.50</ 一级</ </Tr> <Tr align=center bgcolor="#ffffff"> 产品一</ This is a test</ <Td align=right>300.50</ 一级</ </Tr> <Tr align=center bgcolor="#ffffff"> 产品二</ This is a test</ <Td align=right>300.50</ 一级</ </Tr> <Tr align=center bgcolor="#ffffff"> 产品三</ This is a test</ <Td align=right>300.50</ 一级</ </Tr> <Tr align=center bgcolor="#ffffff"> 产品一</ This is a test</ <Td align=right>300.50</ 一级</ </Tr> <Tr align=center bgcolor="#ffffff"> 产品二</ This is a test</ <Td align=right>300.50</ 一级</ </Tr> <Tr align=center bgcolor="#ffffff"> 产品三</ This is a test</ <Td align=right>300.50</ 一级</ </Tr> </Tabld> <input type=button onclick="vbscript:buildDoc 80" value="build"> </BODY> </HTML> <script language="vbscript"> '************************************************************************************************** '使用方法: ' a.使用的时候必须将IE的安全级别设置为最低,然后才能够在Client端CreateObject一个ActiveX对象 ' b.必须对需要打印的表格设置ID="myData" '函数名称:buildDoc '输入参数:表格的行数,整形 '缺 陷:对于不规则的表格,例如有在某一行有colspan="2",使用该程序在识别的时候只能够认为是一列 '************************************************************************************************** Sub buildDoc(intTableRows) Dim Table1 set Table1 = document.all.myData row = Table1.rows.length Set objWordDoc = CreateObject("Word.Document") ObjWordDoc.Application.Visible = True '这里需要设置数组的大小,最好不要小于表格的行和列数 Dim theArray(80,80)
colnum = Table1.rows(1).cells.length for i=0 to row-1 for j=0 to colnum-1 theArray(j+1,i+1) = Table1.rows(i).cells(j).innerHTML next next intNumrows = 80 objWordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore("表格的Title") objWordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore("") '输出标题后回车换行 'objWordDoc.Application.ActiveDocument.Paragraphs.Add.Range.InsertBefore("") Set rngPara = objWordDoc.Application.ActiveDocument.Paragraphs(1).Range '设置标题的属性 With rngPara .Bold = True .ParagraphFormat.Alignment = 1 '对上面的"表格的Title"对齐方式:1表示居中对齐,2表示右对齐 .Font.Name = "Arial" .Font.Size = 16 End With Set rngCurrent = objWordDoc.Application.ActiveDocument.Paragraphs(3).Range '下面这行的"4"必须和下面的第二次循环中的theArray()中的列数4相同 Set tabCurrent = ObjWordDoc.Application.ActiveDocument.Tables.Add(rngCurrent,intNumrows,4) 'objWordDoc.Application.ActiveDocument.Tables(1).Borders.InsideLineStyle = wdLineStyleNone '增加两行用来描黑Word表格里面的Border,在打印的时候才会出现边框。 objWordDoc.Application.ActiveDocument.Tables(1).Borders.InsideLineStyle = True objWordDoc.Application.ActiveDocument.Tables(1).Borders.OutsideLineStyle = True for i = 1 to colnum objWordDoc.Application.ActiveDocument.Tables(1).Rows(1).Cells(i).Range.InsertAfter theArray(i,1) objWordDoc.Application.ActiveDocument.Tables(1).Rows(1).Cells(i).Range.ParagraphFormat.alignment=1 next tabRow = 2 For j = 2 to intNumrows 'objWordDoc.Application.ActiveDocument.Tables(1).Rows(tabRow).Borders.Enable=True objWordDoc.Application.ActiveDocument.Tables(1).Rows(tabRow).Cells(1).Range.InsertAfter theArray(1,j) objWordDoc.Application.ActiveDocument.Tables(1).Rows(tabRow).Cells(1).Range.ParagraphFormat.alignment=1 objWordDoc.Application.ActiveDocument.Tables(1).Rows(tabRow).Cells(2).Range.InsertAfter theArray(2,j) objWordDoc.Application.ActiveDocument.Tables(1).Rows(tabRow).Cells(2).Range.ParagraphFormat.alignment=1 objWordDoc.Application.ActiveDocument.Tables(1).Rows(tabRow).Cells(3).Range.InsertAfter FormatCurrency(theArray(3,j)) objWordDoc.Application.ActiveDocument.Tables(1).Rows(tabRow).Cells(3).Range.ParagraphFormat.alignment=2 objWordDoc.Application.ActiveDocument.Tables(1).Rows(tabRow).Cells(4).Range.InsertAfter theArray(4,j) objWordDoc.Application.ActiveDocument.Tables(1).Rows(tabRow).Cells(4).Range.ParagraphFormat.alignment=1 tabRow = tabRow + 1 Next objWordDoc.Application.ActiveDocument.SaveAs "tempSample.doc", 0,False,"",True,"",False,False,False, False,False 'objWordDoc.Application.printout()
End Sub </script>
参考资料: a.http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/html/output/F1/D4/S5AB0B.asp b.http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/html/output/F1/D4/S5AC00.asp c.http://msdn.microsoft.com/library/default.asp?url=/library/en-us/office97/html/output/F1/D4/S5A856.asp
|