VB写了新建保存excel文件,但是为啥excel数据没写进去
OptionExplicitDimxlsAppAsExcel.ApplicationPublicxlsBookAsExcel.WorkbookPublicxlsSheet...
Option Explicit
Dim xlsApp As Excel.Application
Public xlsBook As Excel.Workbook
Public xlsSheet As New Excel.Worksheet
Dim xlsChar As New Excel.Chart
'判断是否保存,读入
Function savetoexcel()
Dim t1, t2 As Integer
Dim fname As String
CDSave1.Filter = "*.xls|*.xls"
CDSave1.ShowSave
fname = CDSave1.FileName
If fname <> "" Then
Set xlsApp = New Excel.Application
Set xlsApp = CreateObject("Excel.Application") '添加新建表格
Set xlsBook = xlsApp.Workbooks.Add
Set xlsSheet = xlsBook.Worksheets.Add
xlsApp.Sheets("sheet1").Cells(1, 1).Value = "initial Azimuth (°)"
xlsApp.Sheets("sheet1").Cells(1, 2).Value = "initial Inclination (°)"
xlsApp.Sheets("sheet1").Cells(1, 3).Value = "Measured Azimuth (°)"
xlsApp.Sheets("sheet1").Cells(1, 4).Value = "Measured Inclination (°)"
xlsApp.Sheets("sheet1").Cells(1, 5).Value = "Drilling Length (m)"
Dim i As Integer
For i = 1 To count Step 1
xlsApp.Sheets("sheet1").Cells(i + 1, 1).Value = Data1(i)
xlsApp.Sheets("sheet1").Cells(i + 1, 2).Value = Data2(i)
xlsApp.Sheets("sheet1").Cells(i + 1, 3).Value = Data4(i)
xlsApp.Sheets("sheet1").Cells(i + 1, 4).Value = Data5(i)
xlsApp.Sheets("sheet1").Cells(i + 1, 5).Value = Data6(i)
Next i
xlsBook.SaveAs (fname) '保存文件
xlsApp.Quit
Set xlsApp = Nothing
Else
t2 = MsgBox("Please enter a filename", vbInformation + vbYesNoCancel, "OK")
End If
End Function
为啥写的标头字符串不显示呢 展开
Dim xlsApp As Excel.Application
Public xlsBook As Excel.Workbook
Public xlsSheet As New Excel.Worksheet
Dim xlsChar As New Excel.Chart
'判断是否保存,读入
Function savetoexcel()
Dim t1, t2 As Integer
Dim fname As String
CDSave1.Filter = "*.xls|*.xls"
CDSave1.ShowSave
fname = CDSave1.FileName
If fname <> "" Then
Set xlsApp = New Excel.Application
Set xlsApp = CreateObject("Excel.Application") '添加新建表格
Set xlsBook = xlsApp.Workbooks.Add
Set xlsSheet = xlsBook.Worksheets.Add
xlsApp.Sheets("sheet1").Cells(1, 1).Value = "initial Azimuth (°)"
xlsApp.Sheets("sheet1").Cells(1, 2).Value = "initial Inclination (°)"
xlsApp.Sheets("sheet1").Cells(1, 3).Value = "Measured Azimuth (°)"
xlsApp.Sheets("sheet1").Cells(1, 4).Value = "Measured Inclination (°)"
xlsApp.Sheets("sheet1").Cells(1, 5).Value = "Drilling Length (m)"
Dim i As Integer
For i = 1 To count Step 1
xlsApp.Sheets("sheet1").Cells(i + 1, 1).Value = Data1(i)
xlsApp.Sheets("sheet1").Cells(i + 1, 2).Value = Data2(i)
xlsApp.Sheets("sheet1").Cells(i + 1, 3).Value = Data4(i)
xlsApp.Sheets("sheet1").Cells(i + 1, 4).Value = Data5(i)
xlsApp.Sheets("sheet1").Cells(i + 1, 5).Value = Data6(i)
Next i
xlsBook.SaveAs (fname) '保存文件
xlsApp.Quit
Set xlsApp = Nothing
Else
t2 = MsgBox("Please enter a filename", vbInformation + vbYesNoCancel, "OK")
End If
End Function
为啥写的标头字符串不显示呢 展开
1个回答
展开全部
Option Explicit
Dim xlsApp As Excel.Application
Public xlsBook As Excel.Workbook
Public xlsSheet As New Excel.Worksheet
Dim xlsChar As New Excel.Chart
'判断是否保存,读入
Function savetoexcel()
Dim t1, t2 As Integer
Dim fname As String
CDSave1.Filter = "*.xls|*.xls"
CDSave1.ShowSave
fname = CDSave1.Filename
If fname <> "" Then
Set xlsApp = New Excel.Application
Set xlsApp = CreateObject("Excel.Application") '添加新建表格
Set xlsBook = xlsApp.Workbooks.Add
Set xlsSheet = xlsBook.Worksheets.Add
With xlsSheet
.Cells(1, 1) = "initial Azimuth (°)"
.Cells(1, 2) = "initial Inclination (°)"
.Cells(1, 3) = "Measured Azimuth (°)"
.Cells(1, 4) = "Measured Inclination (°)"
.Cells(1, 5) = "Drilling Length (m)"
Dim i As Integer
For i = 1 To Count Step 1
.Cells(i + 1, 1) = Data1(i)
.Cells(i + 1, 2) = Data2(i)
.Cells(i + 1, 3)= Data4(i)
.Cells(i + 1, 4)= Data5(i)
.Cells(i + 1, 5)= Data6(i)
Next i
End With
xlsBook.SaveAs (fname) '保存文件
xlsApp.Quit
Set xlsApp = Nothing
Else
t2 = MsgBox("Please enter a filename", vbInformation + vbYesNoCancel, "OK")
End If
End Function
Dim xlsApp As Excel.Application
Public xlsBook As Excel.Workbook
Public xlsSheet As New Excel.Worksheet
Dim xlsChar As New Excel.Chart
'判断是否保存,读入
Function savetoexcel()
Dim t1, t2 As Integer
Dim fname As String
CDSave1.Filter = "*.xls|*.xls"
CDSave1.ShowSave
fname = CDSave1.Filename
If fname <> "" Then
Set xlsApp = New Excel.Application
Set xlsApp = CreateObject("Excel.Application") '添加新建表格
Set xlsBook = xlsApp.Workbooks.Add
Set xlsSheet = xlsBook.Worksheets.Add
With xlsSheet
.Cells(1, 1) = "initial Azimuth (°)"
.Cells(1, 2) = "initial Inclination (°)"
.Cells(1, 3) = "Measured Azimuth (°)"
.Cells(1, 4) = "Measured Inclination (°)"
.Cells(1, 5) = "Drilling Length (m)"
Dim i As Integer
For i = 1 To Count Step 1
.Cells(i + 1, 1) = Data1(i)
.Cells(i + 1, 2) = Data2(i)
.Cells(i + 1, 3)= Data4(i)
.Cells(i + 1, 4)= Data5(i)
.Cells(i + 1, 5)= Data6(i)
Next i
End With
xlsBook.SaveAs (fname) '保存文件
xlsApp.Quit
Set xlsApp = Nothing
Else
t2 = MsgBox("Please enter a filename", vbInformation + vbYesNoCancel, "OK")
End If
End Function
追问
楼上,搞错了吧,我是说数据全部没写进去啊,不知道为啥,表格中没有数字或者字符串啊。你这就只是帮我简化了一下,用了个with而已啊···
追答
你的程序中有很多变量就没有初始化和赋值。
也有可能是你发上来的只是程序的片断,所以我没有办法测试,我给你一段核心部分经过调试的代码。你在此基础上修改调试吧。我调试生成的结果我也一并送上。
你把代码部分放到窗口中任意一个按钮的单击事件中,试一下
由于只能发一个附件,结果不发了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询