数组未通过[调试]读取 [英] Array is not read through [debug]

查看:50
本文介绍了数组未通过[调试]读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码段摘自Visual Studio 2010(试用版)演练.
请注意:
1.创建的Excel工作表并不包含[values]
中声明的所有值 2.尽管该应用程序是首次运行一个名为SampleWorkBooks.xls的文件
创建第二遍后,应用程序提示我输入Book.xlsx的默认位置,这使我认为这是一些默认行为,
该应用程序尚未覆盖.

The following code snippet was taken from the Visual Studio 2010 (trial) walk through.
It is noted that:
1. The excel sheet created does not include all the values declared within [values]
2. Although the first time the application runs a file named SampleWorkBooks.xls
is created, the second time round, the application prompts me for a default location for Book.xlsx, which leads me to think this is some default behavior which
the application has not overridden.

Imports Excel = Microsoft.Office.Interop.Excel
Module Module1
    Sub Main()
        Dim values = {4, 6, 18, 2, 1, 76, 0, 3, 11}
        CreateWorkbook(values, "C:\SampleFolder\SampleWorkbook.xls)
    End Sub
    Sub CreateWorkbook(ByVal values As Integer(), ByVal filePath As String)
        Dim excelApp As Excel.Application = Nothing
        Dim wkbk As Excel.Workbook
        Dim sheet As Excel.Worksheet
        Try
            Start Excel and create a workbook and worksheet.
            excelApp = New Excel.Application
            wkbk = excelApp.Workbooks.Add()
            sheet = CType(wkbk.Sheets.Add(), Excel.Worksheet)
            sheet.Name = "Sample Worksheet"
            ' Write a column of values.
            For i = 1 To values.Length
                sheet.Cells(Row,Column)
                sheet.Cells(i, 1) = values(i) 
               //question: shouldn't this loop iterate through the values declared
               //and write the list of values in row (i,1)?
            Next
            ' Suppress any alerts and save the file. Create the directory
            ' if it does not exist. Overwrite the file if it exists.
            excelApp.DisplayAlerts = False
            Dim folderPath = My.Computer.FileSystem.GetParentPath(filePath)
            If Not My.Computer.FileSystem.DirectoryExists(folderPath) Then
                My.Computer.FileSystem.CreateDirectory(folderPath)
            End If
            wkbk.SaveAs(filePath)
        Catch
        Finally
            sheet = Nothing
            wkbk = Nothing
            ' Close Excel.
            excelApp.Quit()
            excelApp = Nothing
        End Try
    End Sub
End Module



文章来源:http://msdn.microsoft.com/en-us/library/ee317478.aspx



Article sourced from: http://msdn.microsoft.com/en-us/library/ee317478.aspx

Microsoft Visual Studio 2010
Version 10.0.30319.1 RTMRel
Microsoft .NET Framework
Version 4.0.30319 RTMRel
Installed Version: Professional
Microsoft Office Developer Tools   01018-169-2660007-70603
Microsoft Office Developer Tools
Microsoft Visual Basic 2010   01018-169-2660007-70603
Microsoft Visual Basic 2010
Microsoft Visual C# 2010   01018-169-2660007-70603
Microsoft Visual C# 2010
Microsoft Visual C++ 2010   01018-169-2660007-70603
Microsoft Visual C++ 2010
Microsoft Visual F# 2010   01018-169-2660007-70603
Microsoft Visual F# 2010
Microsoft Visual Studio 2010 Team Explorer   01018-169-2660007-70603
Microsoft Visual Studio 2010 Team Explorer
Microsoft Visual Web Developer 2010   01018-169-2660007-70603
Microsoft Visual Web Developer 2010
Crystal Reports Templates for Microsoft Visual Studio 2010
Crystal Reports Templates for Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 SharePoint Developer Tools   10.0.30319
Microsoft Visual Studio 2010 SharePoint Developer Tools

推荐答案

我看到3个错误的原因:
1)
I see 3 reasons to bugs:
1)
Dim sheet As Excel.Worksheet


永远不要使用名为"sheet"的变量,因为它是Excel名称.
Excel具有工作表和工作表集合.在msdn网站上阅读有关差异的更多信息.


Never use variable named "sheet", becouse it is Excel name.
Excel have Worksheets and Sheets collections. Read more about differences on msdn website.

Dim oSheet As Excel.Worksheet



2)



2)

oSheet.Cells(Row,Column)


我没有看到以下声明:行"和列"
您应该始终使用Option Explicit关键字来强制声明变量.

3)


I have''nt see declarations for: "Row" and "Column"
You should always use Option Explicit keyword to enforce declaration of variables.

3)

For i=1 To values.Length
    oSheet.Cells(i, 1) = values(i)
Next i


在For ... To ...循环中使用数组时,默认下限为0(零),而不是1.使用Option Base 1关键字从1枚举数组.

还有一个建议:
使用Range("A" & i)对象.最好然后最快:sSheet.Cells(1,i).


When you use arrays in For ... To ... loop, the default lower bound is 0 (zero), not 1. Use Option Base 1 keyword to enumerate arrays from 1.

And one advise:
Use Range("A" & i) object. This is better and quickest then: sSheet.Cells(1,i).

oRng = oSheet.Range("A" & i) ''direct access
oRng.Value = "Example 1"



要在迭代中更改列,可以使用Offset对象.例如(使用工作表对象):



To change column in iteration you can use Offset object. For example (using sheet object):

With oSheet
    .Range("A1").Offset(RowOffset:=0,ColumnOffset:=i)
    //other instructions
End With


这篇关于数组未通过[调试]读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆