VB写Excel的一个问题
毕业好多年了最近接手一个VB的东西,功能是用VB把数据做成Excel报表,但是Excel要是只读的,我网上找了段测试代码如下Private Sub Command1_Click()
On Error GoTo err_handle
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Add
xlApp.Visible = False
Set xlsheet = xlBook.Worksheets("Sheet1")
xlApp.ScreenUpdating = False
With xlsheet
.Range("A1").Value = "A"
.Range("B1").Value = "B"
.Range("C1").Value = "C"
.Range("D1").Value = "D"
.Range("E1").Value = "E"
.Range("F1").Value = "F"
.Range("G1").Value = "G"
.Range("H1").Value = "H"
.Range("I1").Value = "I"
End With
'xlsheet.Range("A2").CopyFromRecordset Adodc2.Recordset
xlsheet.Cells.HorizontalAlignment = xlCenter
xlsheet.Cells.Font.Size = 12
xlsheet.Columns(1).ColumnWidth = 10
xlsheet.Columns(2).ColumnWidth = 10
xlsheet.Columns(3).ColumnWidth = 10
xlsheet.Columns(4).ColumnWidth = 16
xlsheet.Columns(5).ColumnWidth = 10
xlsheet.Columns(6).ColumnWidth = 16
xlsheet.Columns(7).ColumnWidth = 16
xlsheet.Columns(8).ColumnWidth = 10
xlsheet.Columns(9).ColumnWidth = 10
If Dir(App.Path & "\Excel", vbDirectory) = "" Then MkDir App.Path & "\Excel"
xlBook.SaveAs App.Path & "\Excel\Excel.xls"
xlBook.ChangeFileAccess Mode:=Excel.XlFileAccess.xlReadOnly
xlBook.Close
Set xlsheet = Nothing
Set xlBook = Nothing
xlApp.Quit
Set xlApp = Nothing
MsgBox "SUCCESS", , MsgTitle
err_handle:
End Sub
但是excel仍然是可写的,不是只读的,网上搜了半天也找不到办法,不知道有没有高人指点下