用EXCEL的VBA将PHPCMS的备份文件转换成HTML的一次尝试

背景

有个PHPCMS的网站停了,但是网站的历史文章又想要看看,网站停了以后,管理员发来了网站的所有数据。

分析

因为不会PHP,所有本地环境跑网站不优先考虑。

有MySQL数据库文件,但是不熟悉MySQL数据库,也就不下载数据库了。

有PHPCMS导出的SQL文件,但是不是标准的SQL文件,不能用于导入数据库。不过这个文本格式的文件已经包含了所有文章的内容。

尝试

1.将v9_news 文章主表,v9_news_data 文章从表 用 vlook 函数拼接了一个 “news full”表。

2.用邮件合并的方式,直接生成了2000页的DOC 文档。

3.但是文章的附件图片未能显示……

行动

1.将几个SQL 合并成一个文件。(导出文件是按照2MB进行了分割)

2.下载了 PHPCMS的数据字典。

3. 用到的表有: v9_news 文章主表,v9_news_data 文章从表,v9_attachment 附件表, v9_attachment_index 附件关系表

4.将这几个表的插入语句用 VSCODE处理成 csv格式,导入EXCEL。

扫描二维码关注公众号,回复: 4235224 查看本文章

5.mysql 数据库的时间转换到 excel的时间,用公式  =(Q2+8*3600)/86400+70*365+19,再用函数拼接成字符串格式。

6.用 VBA 将记录读取到txt文件中。

 1 Private Sub CommandButton1_Click()
 2 Dim fso As Object, sFile As Object
 3 Const ForReading = 1, ForWriting = 2, ForAppending = 8, TristateFalse = 0
 4 Set fso = CreateObject("Scripting.FileSystemObject")
 5 Set sFile = fso.OpenTextFile("d:\testfile.txt", ForAppending, TristateFalse)
 6 For c = 2 To 1818
 7 
 8         sFile.WriteLine "<div><h3>" + Sheet1.Cells(c, "d").Value + "</h3>"
 9         sFile.WriteLine "<h5>日期:" + Sheet1.Cells(c, "w").Value + "</h5>"
10         For ccccc = 2 To 1818
11         wenzhangID = Sheet2.Cells(ccccc, "a")
12         If wenzhangID = Sheet1.Cells(c, "a") Then
13         NewsData = Sheet2.Cells(ccccc, "b").Value
14         sFile.WriteLine "<div>" & NewsData & "</div>"
15         End If
16         Next
17         'sFile.WriteLine "<div>" + getNewsData(Sheet1.Cells(c, "a")) + "</div>"
18         
19         
20         Dim keyArray() As Single
21         Dim i As Single '数组计数
22         i = 0
23         For cc = 2 To 828
24         aid = Sheet4.Cells(cc, "c")
25         If aid = Sheet1.Cells(c, "a") Then
26             ReDim keyArray(i + 1)
27             keyArray(i) = Sheet4.Cells(cc, "d")
28              i = i + 1
29         End If
30     
31         Next
32         If i > 0 Then
33         For Each aaa In keyArray
34         
35        ' MsgBox aaa
36         
37         For ccc = 2 To 746
38     
39             ID = Sheet3.Cells(ccc, "a")
40             If ID = aaa Then
41              'MsgBox Sheet3.Cells(ccc, "e").Value
42              
43              sFile.WriteLine "<img width=""100%"" src=uploadfile/" + Sheet3.Cells(ccc, "e").Value + " />"
44             End If
45             
46             
47         Next
48         
49         
50         
51         Next
52         
53         End If
54         
55        
56 
57 sFile.WriteLine "</div>"
58         
59 Next
60 
61 sFile.Close
62 Set fso = Nothing
63 Set sFile = Nothing
64 MsgBox "OKOK!!!"
65 
66 End Sub

遇到的问题

1. VBA 不熟悉,基本上都是粘贴复制。做了几个函数调用,总是有这样那样的问题,调用不成功,后来只好来个嵌套大法,层层嵌套了。

 1 Function getNewsData(m)
 2 
 3 For ccccc = 2 To 1818
 4         wenzhangID = Sheet2.Cells(ccccc, "a")
 5         If wenzhangID = m Then
 6         getNewsData = Sheet2.Cells(ccccc, "b").Value
 7         End If
 8         
 9         
10 Next
11 
12 End Function
13 
14 Function getKey(m)
15     'm是文章id
16     Dim keyArray() As Single
17     Dim i As Single '数组计数
18     i = 0
19     For c = 2 To 828
20         aid = Sheet4.Cells(c, "c")
21         If aid = m Then
22         ReDim keyArray(i + 1)
23         keyArray(i) = Sheet4.Cells(c, "d")
24         i = i + 1
25         End If
26     
27     Next
28     ReDim keyArray(i + 1)
29     getKey = keyArray
30 End Function
31 
32 Function EmptyArr(ByRef x() As String) As Boolean  '判断是否为空数组的自定义函数
33 Dim tempStr As String
34 tempStr = Join(x, ",")
35 EmptyArr = LenB(tempStr) <= 0
36 End Function
几个不成功的函数

2.如果单元格里面有连续四个空格的话,FileSystemObject.WriteLine 会报错,不明白为什么。

3.还有两个单元格看起来没有什么特别的内容,FileSystemObject.WriteLine 也会报错,原因不明,也不知道该怎样分析,最后把单元格内容设定为“读取错误”给跳过了。

4.变量名,格式啥的非常随意,估计过几天自己也不认得了。

5.认为这样简单的任务用 VBA 搞定应该很宽,但是没想到在做循环的时候,竟然在怎样判断数组的元素个数上搜了半天。

想法:

下次这样的任务还是用 NOPI 做。

猜你喜欢

转载自www.cnblogs.com/kqw/p/10018354.html