On Error GoTo ERR_HANDLE Dim ExcelWk As Object Dim mfrm As New frmProgress
Dim X As Long Dim Y As Long Dim yy As String Dim xx As String Dim XXX As String
X = gridcol \ 26 Y = gridcol Mod 26 yy = ChrW(Asc("A") + Y) If X > 0 Then xx = ChrW(Asc("A") + X - 1) yy = xx & yy End If xx = CStr(gridrow + 1) XXX = CStr(gridrow + 2) mfrm.Show UpdateStatus mfrm.picProgress, 0, False mfrm.lblPrompt = "正在启动Excel实例..." DoEvents If ExcelApp Is Nothing Then Set ExcelApp = CreateObject("Excel.Application") End If UpdateStatus mfrm.picProgress, 0.1, False On Error GoTo ERR_EXCEL Set ExcelWk = ExcelApp.Workbooks.Add
.Range("A2:" & yy & 2).Select With .Selection.Interior .ColorIndex = 48 .Pattern = xlSolid End With .Range("A2:" & yy & XXX).Select With .Selection .HorizontalAlignment = xlGeneral .VerticalAlignment = xlCenter .WrapText = True .Orientation = 0 .AddIndent = False .ShrinkToFit = False .MergeCells = False End With
.Range("A2:" & yy & 2).Select .Selection.Borders(xlDiagonalDown).LineStyle = xlNone .Selection.Borders(xlDiagonalUp).LineStyle = xlNone With .Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With .Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With .Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With .Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With
.Selection.Borders(xlInsideVertical).LineStyle = xlNone .Range("A3:" & yy & xx).Select .Selection.Borders(xlDiagonalDown).LineStyle = xlNone .Selection.Borders(xlDiagonalUp).LineStyle = xlNone With .Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With .Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With .Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With With .Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With If xx <> 3 Then With .Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With
With .Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With End If .Range("A" & XXX & ":" & yy & XXX).Select .Selection.Borders(xlDiagonalDown).LineStyle = xlNone .Selection.Borders(xlDiagonalUp).LineStyle = xlNone With .Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With .Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With .Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With .Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With .Selection.Borders(xlInsideVertical).LineStyle = xlNone .Range("A3:" & yy & xx).Select .Selection.Borders(xlDiagonalDown).LineStyle = xlNone .Selection.Borders(xlDiagonalUp).LineStyle = xlNone With .Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With .Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With .Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With With .Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .Weight = xlMedium .ColorIndex = xlAutomatic End With If xx <> 3 Then With .Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With
With .Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .Weight = xlThin .ColorIndex = xlAutomatic End With End If
For i = 0 To gridrow For j = 0 To gridcol X = Fix(j \ 26) Y = j Mod 26 yy = ChrW(Asc("A") + Y) xx = "" If X > 0 Then xx = ChrW(Asc("A") + X - 1) yy = xx & yy End If xx = CStr(i + 2) .Range(yy & xx).Select .ActiveCell.FormulaR1C1 = grdQuery.TextMatrix(i, j) Next UpdateStatus mfrm.picProgress, 0.1 + (i + 1) / (grdQuery.Rows - 1) * 0.8, False DoEvents Next '自动调整列宽 .Cells.Select
.Selection.Columns.AutoFit .ActiveWindow.SmallScroll Down:=-22 .ActiveWindow.SmallScroll ToRight:=-32 .Range("A1").Select ' ExcelWk.Save Dim xlsName As String xlsName = CStr(Now) xlsName = Replace(xlsName, ":", "-") ' ChDir "D:\Documents and Settings\java\桌面" .ActiveWorkbook.SaveAs FileName:=App.Path & "\" & xlsName & ".xls" _ , FileFormat:=xlNormal, Password:="", WriteResPassword:="", _ ReadOnlyRecommended:=False, CreateBackup:=False .Visible = True End With Unload mfrm Exit Sub ERR_EXCEL: Unload mfrm ExcelApp.Quit ERR_HANDLE: ShowError
Dim ExcelWk As Object
Dim mfrm As New frmProgress
Dim X As Long
Dim Y As Long
Dim yy As String
Dim xx As String
Dim XXX As String
X = gridcol \ 26
Y = gridcol Mod 26
yy = ChrW(Asc("A") + Y)
If X > 0 Then
xx = ChrW(Asc("A") + X - 1)
yy = xx & yy
End If
xx = CStr(gridrow + 1)
XXX = CStr(gridrow + 2)
mfrm.Show
UpdateStatus mfrm.picProgress, 0, False
mfrm.lblPrompt = "正在启动Excel实例..."
DoEvents
If ExcelApp Is Nothing Then
Set ExcelApp = CreateObject("Excel.Application")
End If
UpdateStatus mfrm.picProgress, 0.1, False
On Error GoTo ERR_EXCEL
Set ExcelWk = ExcelApp.Workbooks.Add
ExcelWk.Activate
mfrm.lblPrompt = "正在粘贴数据..."
DoEvents
With ExcelApp
.Visible = False
'设置字体为宋体9号
.Cells.Select
With .Selection.Font
.name = "宋体"
.Size = 9
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
End With
.Range("E1,E1").Select
.ActiveCell.FormulaR1C1 = cmbQueryType.Text
.Range("A2:" & yy & 2).Select
With .Selection.Interior
.ColorIndex = 48
.Pattern = xlSolid
End With
.Range("A2:" & yy & XXX).Select
With .Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.ShrinkToFit = False
.MergeCells = False
End With
.Range("A2:" & yy & 2).Select
.Selection.Borders(xlDiagonalDown).LineStyle = xlNone
.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With .Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With .Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With .Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With .Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
.Selection.Borders(xlInsideVertical).LineStyle = xlNone
.Range("A3:" & yy & xx).Select
.Selection.Borders(xlDiagonalDown).LineStyle = xlNone
.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With .Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With .Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With .Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With .Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
If xx <> 3 Then
With .Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With .Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End If
.Range("A" & XXX & ":" & yy & XXX).Select
.Selection.Borders(xlDiagonalDown).LineStyle = xlNone
.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With .Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With .Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With .Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With .Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
.Selection.Borders(xlInsideVertical).LineStyle = xlNone
.Range("A3:" & yy & xx).Select
.Selection.Borders(xlDiagonalDown).LineStyle = xlNone
.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With .Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With .Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With .Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
With .Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlMedium
.ColorIndex = xlAutomatic
End With
If xx <> 3 Then
With .Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With .Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End If
For i = 0 To gridrow
For j = 0 To gridcol
X = Fix(j \ 26)
Y = j Mod 26
yy = ChrW(Asc("A") + Y)
xx = ""
If X > 0 Then
xx = ChrW(Asc("A") + X - 1)
yy = xx & yy
End If
xx = CStr(i + 2)
.Range(yy & xx).Select
.ActiveCell.FormulaR1C1 = grdQuery.TextMatrix(i, j)
Next
UpdateStatus mfrm.picProgress, 0.1 + (i + 1) / (grdQuery.Rows - 1) * 0.8, False
DoEvents
Next
'自动调整列宽
.Cells.Select
.Selection.Columns.AutoFit
.ActiveWindow.SmallScroll Down:=-22
.ActiveWindow.SmallScroll ToRight:=-32
.Range("A1").Select
' ExcelWk.Save
Dim xlsName As String
xlsName = CStr(Now)
xlsName = Replace(xlsName, ":", "-")
' ChDir "D:\Documents and Settings\java\桌面"
.ActiveWorkbook.SaveAs FileName:=App.Path & "\" & xlsName & ".xls" _
, FileFormat:=xlNormal, Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, CreateBackup:=False
.Visible = True
End With
Unload mfrm
Exit Sub
ERR_EXCEL:
Unload mfrm
ExcelApp.Quit
ERR_HANDLE:
ShowError