1、form8的command1的事件直接用MSHFlexGrid1内容判断后直接输出到execl。
command1的具体代码修改如下:
Private Sub Command1_Click()
Dim i, j, t
CommonDialog1.ShowSave
t = CommonDialog1.FileName
Dim xlApp As Excel.Application '定义EXCEL类
Dim xlBook As Excel.Workbook '定义工件簿类
Dim xlsheet As Excel.Worksheet '定义工作表类
Set xlApp = CreateObject("Excel.Application") '创建EXCEL应用类
xlApp.Visible = False
Set xlBook = xlApp.Workbooks.Add
Set xlsheet = xlBook.Worksheets(1) '打开EXCEL工作表
Dim Date1, Date2, Date3
Dim Cols, Rows
Dim count1
Date1 = CDate(DTPicker1.Value)
Date3 = CDate(DTPicker2.Value)
Rows = MSHFlexGrid1.Rows
Cols = MSHFlexGrid1.Cols
count1 = 2
For i = 0 To Cols - 1: xlsheet.Cells(1, i + 1) = MSHFlexGrid1.TextMatrix(0, i): Next '表头
For i = 1 To Rows - 1
Date2 = CDate(Format(MSHFlexGrid1.TextMatrix(i, 7), "yyyy/mm/dd"))
If Date1 <= Date2 And Date2 <= Date3 Then '符合条件
For j = 0 To Cols - 1: xlsheet.Cells(count1, j + 1) = MSHFlexGrid1.TextMatrix(i, j): Next '符合条件记录
count1 = count1 + 1
End If
Next
xlBook.SaveAs (t)
xlBook.Close (True) '关闭EXCEL工作簿
xlApp.Quit '关闭EXCEL
Set xlApp = Nothing '释放EXCEL对象
MsgBox "完成导出"
End Sub
2、如果用数据库,要么是查询全部记录,在匹配;或者直接构造符合条件sql语句(日期型的比较有难度,日期型数据必须格式一致才行)。
不过以上数据已经加载完毕,没有必要在去数据库中查询了。