找回密码
 立即注册

QQ登录

只需一步,快速开始

randomcic

注册会员

19

主题

38

帖子

161

积分

注册会员

积分
161

活字格认证

randomcic
注册会员   /  发表于:2015-1-25 15:50  /   查看:5915  /  回复:3
VB6下:正确得到结果

CommonDialog1.Flags = cdlOFNHideReadOnly
CommonDialog1.Filter = "All Files (*.*)|*.*|Excel文件" & "(*.xls)|*.xls|"
CommonDialog1.FilterIndex = 2
CommonDialog1.ShowOpen
    Dim y As Boolean
    Dim listcount As Integer, handle As Integer
    Dim List() As String
        y = fpSpread1.GetExcelSheetList(CommonDialog1.FileName, List, listcount, App.Path & "\log.txt", handle, True)
            ReDim List(listcount - 1)
        y = fpSpread1.GetExcelSheetList(CommonDialog1.FileName, List, listcount, App.Path & "\log.txt", handle, False)
            For i = 0 To listcount - 1
                Combo1.AddItem (List(i))
            Next i
在Combo1控件中得到正确的工作薄列表
--------------------------------------------------------------------------------------------------------------

VB2013下:在向TSComboBox1添加内容时错误

        OpenFileDialog1.Filter = "Excel 文件 (*.xls)|*.xls|所有文件 (*.*)|*.*"
        OpenFileDialog1.FilterIndex = 1
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            Dim y, z
            Dim listcount As Integer, handle As Integer
            Dim List()
            Dim i
            ReDim List(1)
            y = AxfpSpread1.GetExcelSheetList(OpenFileDialog1.FileName, List, listcount, Application.StartupPath & "\log.txt", handle, True)
            ReDim List(listcount - 1)
            y = AxfpSpread1.GetExcelSheetList(OpenFileDialog1.FileName, List, listcount, Application.StartupPath & "\log.txt", handle, False)

            For i = 0 To listcount - 1
                TSComboBox1.Items.Add(List(i))             '执行到这一步时,显示错误:值不能为 null。
            Next i
--------------------------------------------------------------------------------------------------------------------
为什么呢?同样的段子,我拿到VB6下执行正确,但在VB2013下就是不行。

3 个回复

倒序浏览
Alice
社区贡献组   /  发表于:2015-1-29 13:53:00
沙发
回复 1楼randomcic的帖子

你在代码里打开了一个excel文件,使用GetExcelSheetList方法的时候,对excel文件的格式是有要求的。
如果文件是 BIFF8 格式的excel文件,那么可以使用GetExcelSheetList方法导入。
如果文件是xlsx文件,那么要使用OpenExcel2007File方法,导入xlsx格式文件。
如果文件不是之前提到的这两种类型,那么spread不能导入文件。

所以List添加的项目会是null。
VisualStudio里,语法规定,ComboBox的Items不能添加null的项目,因此会报错。

在使用这个方法的时候,请参考文档:
http://helpcentral.componentone. ... eHelp/add_da22.html
请点击评分,对我的服务做出评价!  5分为非常满意!

葡萄城控件服务团队

官方网站: http://www.gcpowertools.com.cn
回复 使用道具 举报
randomcic
注册会员   /  发表于:2015-2-3 21:57:00
板凳
---------以下的问题是一个初学者的提问,有问得让人发笑的地方,还望不吝赐教-----------

  我使用的是 spread 8.0  for ActiveX ,为什么两个不同的环境下(VB6和VB2013)下打开同样的文件,VB6下打开正常,VB2013下却总是返回"null“,取不到工作薄的名呢。

  楼上您所说的excel格式我不懂,我目前可以使用的格式是excel2003的.xls和2007的.xlsx这两种格式所属的是您所说的哪一种类型呢?

  如果必须使用您所说的BIFF8 这种格式的excel文件,我使用的excel,应该使用哪个版本的来保存成这种格式的xls文件,请详细说明。

  另外在for activeX下的spread 是没有openexcel这个方法的。
回复 使用道具 举报
Alice
社区贡献组   /  发表于:2015-2-4 11:26:00
地板
回复 3楼randomcic的帖子

不太清楚你的excel文件具体是什么格式。仅从你的描述来看,你的两个excel文件格式是不一致的。一个是.xls文件,另一个是.xlsx文件。
如果你自己不能判断,那么使用GetExcelSheetList方法之前,可以添加判断IsExcelFile方法,如果是True,就可以使用GetExcelSheetList方法。否则使用IsExcel2007File方法判断是不是.xlsx后缀名的文件,然后使用 OpenExcel2007File方法。
有关 OpenExcel2007File方法的文档请参考:
http://helpcentral.componentone. ... nct278.html#1410199

我也已经在VisualStudio VB和VB6.0下测试过,没有重现你提到的方法不存在的问题。

特别注意的是:在VisualStudio VB下使用GetExcelSheetList方法,获取到的List(0)为null。
这个问题我已经提交到产品组进行确认。

目前来说,给你提供的解决方法,只有先通过导入读取excel文件,然后通过fpspread.SheetName获取文件名。
代码参考:
  1. Dim y As Boolean, z As Boolean
  2.         Dim Var As Object
  3.         Dim x As Integer, listcount As Integer, handle As Integer
  4.         Dim List(10) As String
  5.         If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
  6.             ' Check if file is an Excel file and set result to x
  7.             x = fpSpread1.IsExcelFile(OpenFileDialog1.FileName)

  8.             ' If file is Excel file, tell user, import sheet
  9.             ' list, and set result to y
  10.             If x = 1 Then
  11.                 MsgBox("File is an Excel file.", , "File Type")
  12.                 y = fpSpread1.GetExcelSheetList(OpenFileDialog1.FileName, List, listcount, "C:\ILOGFILE.TXT", handle, True)
  13.                 ReDim List(listcount)

  14.                 ' If received sheet list, tell user, import file,
  15.                 ' and set result to z
  16.                 If y = True Then
  17.                     MsgBox("Got sheet list.", , "Status")

  18.                     z = fpSpread1.ImportExcelSheet(handle, 0)
  19.                     MsgBox(fpSpread1.SheetName, , "Result")

  20.                     ' Tell user result based on T/F value of z
  21.                     If z = True Then
  22.                         MsgBox("Import complete.", , "Result")
  23.                     Else
  24.                         MsgBox("Import did not succeed.", , "Result")
  25.                     End If
  26.                 Else
  27.                     ' Tell user cannot obtain sheet list
  28.                     MsgBox("Cannot return information for Excel file.", , "Result")
  29.                 End If
  30.             Else
  31.                 ' Tell user file is not Excel file or is locked
  32.                 MsgBox("File is not an Excel file or is locked and cannot be imported.", , "Invalid File Type or Locked")
  33.             End If
  34.         End If
复制代码
请点击评分,对我的服务做出评价!  5分为非常满意!

葡萄城控件服务团队

官方网站: http://www.gcpowertools.com.cn
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部