1

我每天都会收到一些报告,这些报告必须以特定方式导入到数据库 MS Access 中。问题是,报告的布局方式很难做到这一点。

 Program: Name A
     Date | Description | Other | Stuff
     Date | Description | Other | Stuff 
Program: Name B
     Date | Description | Other | Stuff
     Date | Description | Other | Stuff   
Program: Name C
     Date | Description | Other | Stuff
     Date | Description | Other | Stuff           

基本上,我希望它看起来像这样。

Program: Name A | Date | Description | Other | Stuff
Program: Name A | Date | Description | Other | Stuff
Program: Name B | Date | Description | Other | Stuff
Program: Name B | Date | Description | Other | Stuff
Program: Name C | Date | Description | Other | Stuff
Program: Name C | Date | Description | Other | Stuff

我玩过几个不同的解决方案,但到目前为止还没有完成这项工作。

谢谢你的帮助!

4

1 回答 1

2

就像是:

Dim xl As New Excel.Application
Dim ws As Excel.Worksheet
Dim rng As Excel.Range
Dim rs As DAO.Recordset
Dim db As Database

Set db = CurrentDb

''Run once
''s = "create table reporttable (id counter primary key, program text, " _
'' & "pdate text, [description] text, other text, stuff text)"
''db.Execute s, dbFailOnError

Set rs = db.OpenRecordset("ReportTable")
xl.Visible = True ''dev

Set ws = xl.Workbooks.Open("z:\docs\report.xlsx").Sheets("Sheet1")
Set rng = ws.UsedRange

For i = 1 To rng.Rows.Count
    If rng.Cells(i, 1) Like "Program*" Then
        progdat = rng.Cells(i, 1)
    Else
        rs.AddNew
        rs!program = progdat
        rs!pdate = rng.Cells(i, 1) ''Text, because you cannot trust reports
        rs!Description = rng.Cells(i, 2)
        rs!Other = rng.Cells(i, 3)
        rs!Stuff = rng.Cells(i, 4)
        rs.Update
    End If
Next
Set rs = Nothing
Set rng = Nothing
ws.Parent.Close
Set ws = Nothing
xl.Quit
Set xl = Nothing
于 2013-01-11T19:45:15.720 回答