On Error GoTo errhandle
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Screen.MousePointer = 11
CommonDialog1.Filter = "ExcelÎļþ£¨*.xls£©|*.xls"
CommonDialog1.ShowSave
If Trim(CommonDialog1.filename) = "" Then
Screen.MousePointer = 0
Exit Sub
End If
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
xlApp.Visible = True
Dim i, j As Integer '¸ù¾ÝÍø¸ñÄÚÈÝÿÁÐÒÀ´Îдµ½Excel
For i = 0 To MSHFGrid.cols - 1 'i´ú±íÁÐ
If MSHFGrid.ColWidth(i) <> 0 Then
For j = 0 To MSHFGrid.Rows - 1 'j´ú±íÐÐ
xlSheet.Cells(j + 1, i + 1).Value = MSHFGrid.TextMatrix(j, i)
Next j
End If
Next i
xlSheet.SaveAs Trim(CommonDialog1.filename)
xlApp.Quit
Set xlApp = Nothing
Set xlBook = Nothing
Set xlSheet = Nothing
Screen.MousePointer = 0
Exit Sub
errhandle:
MsgBox Err.Description, vbInformation + vbOKOnly
Screen.MousePointer = 0
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Screen.MousePointer = 11
CommonDialog1.Filter = "ExcelÎļþ£¨*.xls£©|*.xls"
CommonDialog1.ShowSave
If Trim(CommonDialog1.filename) = "" Then
Screen.MousePointer = 0
Exit Sub
End If
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets(1)
xlApp.Visible = True
Dim i, j As Integer '¸ù¾ÝÍø¸ñÄÚÈÝÿÁÐÒÀ´Îдµ½Excel
For i = 0 To MSHFGrid.cols - 1 'i´ú±íÁÐ
If MSHFGrid.ColWidth(i) <> 0 Then
For j = 0 To MSHFGrid.Rows - 1 'j´ú±íÐÐ
xlSheet.Cells(j + 1, i + 1).Value = MSHFGrid.TextMatrix(j, i)
Next j
End If
Next i
xlSheet.SaveAs Trim(CommonDialog1.filename)
xlApp.Quit
Set xlApp = Nothing
Set xlBook = Nothing
Set xlSheet = Nothing
Screen.MousePointer = 0
Exit Sub
errhandle:
MsgBox Err.Description, vbInformation + vbOKOnly
Screen.MousePointer = 0
各位用的都是调用Excel进行写入操作的方法但是我想,如果只是另存为Excel文件的话,应该可以直接将网格数据存为文件的比如说: CSV 文件(将网格数据以行列方式写入,列与列间以逗号隔开)是Excel直接支持的! 呵呵,拙见,希望能对你有用!另外,有空的话请看看本人的拙作: Http://YanJieBing.3322.Net里面有工资报表的示例及打印控件使用说明。