以下函数是Excel生成的vb宏,用来给表格的边框划线
Sub Macro1() Range("A2:M8").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
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 Sub-------------------------------------我在vb中调用先导出Excel在进行格式设置
程序如下:
Dim i As Integer
Dim j As IntegerDim xlApp As Excel.Application
'Set xlApp = New Excel.ApplicationDim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim r As Excel.Range
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
On Error Resume Next
Set xlBook = xlApp.Workbooks.Open("d:\text2.xls")
Set xlSheet = xlBook.Worksheets(1)
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).Select
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).RowHeight = 40
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).Merge (missing)
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).Value = "档案资料报表"
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).HorizontalAlignment = xlCenter
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).VerticalAlignment = xlCenter
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).Font.Name = "黑体"
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).Font.Size = 16For j = 0 To DataGrid1.Columns.Count - 1
xlSheet.Cells(2, j + 1) = DataGrid1.Columns.Item(j).Caption
xlSheet.Cells(2, j + 1).HorizontalAlignment = xlCenter
Next j'xlSheet.Cells(10, 1) = "i"
Adodc1.Recordset.MoveFirst
For i = 1 To adoPrimaryRS.RecordCount - 1
DataGrid1.Row = i
For j = 0 To DataGrid1.Columns.Count - 1
DataGrid1.Col = j
'MsgBox DataGrid1.Text
If IsNull(DataGrid1.Text) = False Then
xlSheet.Cells(i + 2, j + 1) = "'" + DataGrid1.Text
End If
Next j
Next ixlApp.Range(xlApp.Cells(2, 1), xlApp.Cells(adoPrimaryRS.RecordCount + 1, DataGrid1.Columns.Count)).Select
xlApp.Range(xlApp.Cells(2, 1), xlApp.Cells(adoPrimaryRS.RecordCount + 1, DataGrid1.Columns.Count)).HorizontalAlignment = xlHAlignLeft
xlApp.Range(xlApp.Cells(2, 1), xlApp.Cells(adoPrimaryRS.RecordCount + 1, DataGrid1.Columns.Count)).VerticalAlignment = xlCenter
xlApp.Range(xlApp.Cells(2, 1), xlApp.Cells(adoPrimaryRS.RecordCount + 1, DataGrid1.Columns.Count)).WrapText = True
xlApp.Range(xlApp.Cells(2, 1), xlApp.Cells(adoPrimaryRS.RecordCount + 1, DataGrid1.Columns.Count)).Font.Name = "宋体"
xlApp.Range(xlApp.Cells(2, 1), xlApp.Cells(adoPrimaryRS.RecordCount + 1, DataGrid1.Columns.Count)).Font.Size = 9 xlApp.Selection.Borders(xlDiagonalDown).LineStyle = xlNone
xlApp.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With xlApp.Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlApp.Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlApp.Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlApp.Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlApp.Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlApp.Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End WithWith xlApp.ActiveSheet.PageSetup
.CenterFooter = "第 &P 页,共 &N 页"
.Orientation = xlLandscape
.PrintGridlines = False
End With
结果别的都没问题,就是设置表格线的代码不起作用,请高手帮忙看看~
Sub Macro1() Range("A2:M8").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
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 Sub-------------------------------------我在vb中调用先导出Excel在进行格式设置
程序如下:
Dim i As Integer
Dim j As IntegerDim xlApp As Excel.Application
'Set xlApp = New Excel.ApplicationDim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim r As Excel.Range
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
Set xlBook = xlApp.Workbooks.Add
On Error Resume Next
Set xlBook = xlApp.Workbooks.Open("d:\text2.xls")
Set xlSheet = xlBook.Worksheets(1)
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).Select
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).RowHeight = 40
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).Merge (missing)
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).Value = "档案资料报表"
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).HorizontalAlignment = xlCenter
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).VerticalAlignment = xlCenter
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).Font.Name = "黑体"
xlApp.Range(xlApp.Cells(1, 1), xlApp.Cells(1, DataGrid1.Columns.Count)).Font.Size = 16For j = 0 To DataGrid1.Columns.Count - 1
xlSheet.Cells(2, j + 1) = DataGrid1.Columns.Item(j).Caption
xlSheet.Cells(2, j + 1).HorizontalAlignment = xlCenter
Next j'xlSheet.Cells(10, 1) = "i"
Adodc1.Recordset.MoveFirst
For i = 1 To adoPrimaryRS.RecordCount - 1
DataGrid1.Row = i
For j = 0 To DataGrid1.Columns.Count - 1
DataGrid1.Col = j
'MsgBox DataGrid1.Text
If IsNull(DataGrid1.Text) = False Then
xlSheet.Cells(i + 2, j + 1) = "'" + DataGrid1.Text
End If
Next j
Next ixlApp.Range(xlApp.Cells(2, 1), xlApp.Cells(adoPrimaryRS.RecordCount + 1, DataGrid1.Columns.Count)).Select
xlApp.Range(xlApp.Cells(2, 1), xlApp.Cells(adoPrimaryRS.RecordCount + 1, DataGrid1.Columns.Count)).HorizontalAlignment = xlHAlignLeft
xlApp.Range(xlApp.Cells(2, 1), xlApp.Cells(adoPrimaryRS.RecordCount + 1, DataGrid1.Columns.Count)).VerticalAlignment = xlCenter
xlApp.Range(xlApp.Cells(2, 1), xlApp.Cells(adoPrimaryRS.RecordCount + 1, DataGrid1.Columns.Count)).WrapText = True
xlApp.Range(xlApp.Cells(2, 1), xlApp.Cells(adoPrimaryRS.RecordCount + 1, DataGrid1.Columns.Count)).Font.Name = "宋体"
xlApp.Range(xlApp.Cells(2, 1), xlApp.Cells(adoPrimaryRS.RecordCount + 1, DataGrid1.Columns.Count)).Font.Size = 9 xlApp.Selection.Borders(xlDiagonalDown).LineStyle = xlNone
xlApp.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With xlApp.Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlApp.Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlApp.Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlApp.Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlApp.Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
With xlApp.Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End WithWith xlApp.ActiveSheet.PageSetup
.CenterFooter = "第 &P 页,共 &N 页"
.Orientation = xlLandscape
.PrintGridlines = False
End With
结果别的都没问题,就是设置表格线的代码不起作用,请高手帮忙看看~
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货