找回密码
 加入
搜索
查看: 6980|回复: 17

[效率算法] 关于Excel读取放在数组中的问题【已解决】

 火.. [复制链接]
发表于 2016-7-1 11:01:56 | 显示全部楼层 |阅读模式
本帖最后由 blue_dvd 于 2016-7-11 20:30 编辑

以前版中EXCEL.AU3中有函数__ExcelReadSheetToArray()怎么新的版本没有了?
     而且总是出错,以前改过,忘记如何改了,高手帮忙看看!
Func _ExcelReadSheetToArray($oExcel, $iStartRow = 1, $iStartColumn = 1, $iRowCnt = 0, $iColCnt = 0, $iColShift = False)
        Local $avRET[1][2] = [[0, 0]] ; 2D return array

        ; Test inputs
        If Not IsObj($oExcel) Then Return SetError(1, 0, 0)
        If $iStartRow < 1 Then Return SetError(2, 0, 0)
        If $iStartColumn < 1 Then Return SetError(2, 1, 0)
        If $iRowCnt < 0 Then Return SetError(3, 0, 0)
        If $iColCnt < 0 Then Return SetError(3, 1, 0)

        ; Get size of current sheet as R1C1 string
        ;     Note: $xlCellTypeLastCell and $x1R1C1 are constants declared in ExcelCOM_UDF.au3
        Local $sLastCell = $oExcel.Application.Selection.SpecialCells($xlCellTypeLastCell).Address(True, True, $xlR1C1)

        ; Extract integer last row and col
        $sLastCell = StringRegExp($sLastCell, "\A[^0-9]*(\d+)[^0-9]*(\d+)\Z", 3)
        Local $iLastRow = $sLastCell[0]
        Local $iLastColumn = $sLastCell[1]

        ; Return 0's if the sheet is blank
        If $sLastCell = "R1C1" And $oExcel.Activesheet.Cells($iLastRow, $iLastColumn).Value = "" Then Return $avRET

        ; Check input range is in bounds
        If $iStartRow > $iLastRow Then Return SetError(2, 0, 0)
        If $iStartColumn > $iLastColumn Then Return SetError(2, 1, 0)
        If $iStartRow + $iRowCnt - 1 > $iLastRow Then Return SetError(3, 0, 0)
        If $iStartColumn + $iColCnt - 1 > $iLastColumn Then Return SetError(3, 1, 0)

        ; Check for defaulted counts
        If $iRowCnt = 0 Then $iRowCnt = $iLastRow - $iStartRow + 1
        If $iColCnt = 0 Then $iColCnt = $iLastColumn - $iStartColumn + 1

        ; Size the return array
        ReDim $avRET[$iRowCnt + 1][$iColCnt + 1]
        $avRET[0][0] = $iRowCnt
        $avRET[0][1] = $iColCnt

        If $iColShift Then ;Added by litlmike
                ; Read data to array
                For $r = 1 To $iRowCnt
                        For $c = 1 To $iColCnt
                                $avRET[$r][$c - 1] = $oExcel.Activesheet.Cells($iStartRow + $r - 1, $iStartColumn + $c - 1).Value
                        Next
                Next
        Else ;Default for $iColShift
                ; Read data to array
                For $r = 1 To $iRowCnt
                        For $c = 1 To $iColCnt
                                $avRET[$r][$c] = $oExcel.Activesheet.Cells($iStartRow + $r - 1, $iStartColumn + $c - 1).Value
                        Next
                Next
        EndIf
        ;Return data
        Return $avRET
EndFunc   ;==>_ExcelReadSheetToArray
出错如下:
"D:\AutoIt3\Include\Excel.au3" (791) : ==> 索引使用了不可访问的变量:
Local $iLastRow = $sLastCell[0]
Local $iLastRow = $sLastCell^ ERROR
就是第17-18行出现错误!

或者哪位高手给个从Excel表读取到数组中的函数,万分感谢!
发表于 2016-7-1 11:48:57 | 显示全部楼层
搜索我的咨询的帖子 有答案。
 楼主| 发表于 2016-7-1 17:01:02 | 显示全部楼层
回复 2# heroxianf

能否发一个你用的AU3安装版给我,79451196@qq.com  谢谢
发表于 2016-7-1 17:27:47 | 显示全部楼层
回复 3# blue_dvd


    http://www.autoitx.com/thread-19307-1-1.html

传送门在这里
发表于 2016-7-1 21:20:16 | 显示全部楼层
为什么不用老版本呢
发表于 2016-7-6 10:01:59 | 显示全部楼层
excel.au3还是新版本比较好
发表于 2016-7-6 11:48:38 | 显示全部楼层
回复 1# blue_dvd
新版照样可以直接写入数组啊!
#include <Array.au3>
#include <Excel.au3>
#include <MsgBoxConstants.au3>

; 创建应用程序对象并创建一个新工作簿
Local $oAppl = _Excel_Open()
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeWrite 示例", "创建 Excel 应用对象发生错误." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
Local $oWorkbook = _Excel_BookNew($oAppl)
If @error Then
    MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeWrite 示例", "创建新工作簿失败." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    _Excel_Close($oAppl)
    Exit
EndIf

; *****************************************************************************
; 2D 数组元素写入活动工作簿的活动工作表
; *****************************************************************************
Local $aArray2D[3][5] = [[11, 12, 13, 14, 15],[21, 22, 23, 24, 25],[31, 32, 33, 34, 35]]
_Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $aArray2D, "B1")
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeWrite 示例 3", "写入工作表失败." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeWrite 示例 3", "2D 数组成功写入.")
发表于 2016-7-6 16:06:25 | 显示全部楼层
其实个人建议用OBJ对像,使用COM接口
这样对EXCEL版本没有限制
发表于 2016-7-7 16:14:05 | 显示全部楼层
本帖最后由 kingdsq 于 2016-7-7 16:16 编辑

一个函数轻松搞定,

#FUNCTION# ============================================================
; 名字...........: _operexcel()
; 用途 ...: 操作EXCEL的函数,可以读或写EXCEL函数的某单元格的值或者读取整个表格值
;                        注意:用数组取全表格数据引用数组值时,先列后行,而且从0下标开始,也就是原来的数组的下标各减1
;
; 示例.........: _operexcel($excelfile, row, col, string, type)
;                          当type=0时,表示读操作
;                                如果row=-1,或col=-1,则谈读整个表的数组做为二维数组;
;                                 如果row=0,则读最后一行。
;                                  如果col为0,则谈row整行的字符串作为一维数组,其余正常读COL列的数值
;                          当type=1时,表示写操作
;                                如果row=0,表示的行为最后一行,若r0w=-1,表示操作行为新添一行,其余正常表示
;                                  若col为0,则表示顺序写入row行的含“|”;分割的字符串;其余正常表示
;
; Return values .: 返回读的值或整个表格形成的数组,或为写操作,则成功返TRUE,否则返FALSE

详情:http://www.autoitx.com/thread-52544-1-1.html
 楼主| 发表于 2016-7-7 19:57:51 | 显示全部楼层
回复 9# kingdsq
新手都这么厉害!不过有个问题:如果Excel多个sheet表,函数怎样读取多个二位数组?
 楼主| 发表于 2016-7-7 20:00:24 | 显示全部楼层
回复 9# kingdsq
我自己也重新修改了函数成功了
;读表格中的第一个表格到数组中
Func _ExcelReadSheet1ToArray($oExcel1)

    Local $aArray2 = $oExcel1.worksheets(1).usedrange.value
                $aArray2=Array2DColsToRows($aArray2)

        Local $aLen[UBound($aArray2)][2]
        For $i=1 To UBound($aArray2)-1
                $aLen[$i][0]=StringStripWS($aArray2[$i][1],8) ;去掉所有的空格
                $aLen[$i][1]=StringLen($aArray2[$i][1])
                        Next

                Return $aArray2
EndFunc


;进行行列对调
Func Array2DColsToRows(ByRef $avArray)
        Local $avArray2[UBound($avArray, 2)][UBound($avArray, 1)]
        For $i = 0 To UBound($avArray, 2) - 1
                For $j = 0 To UBound($avArray, 1) - 1
                        $avArray2[$i][$j] = $avArray[$j][$i]
                Next
        Next
        Return $avArray2
EndFunc   ;==>Array2DColsToRows
 楼主| 发表于 2016-7-9 00:28:39 | 显示全部楼层
回复 8# callwww
OBJ对像,使用COM接口,怎样操作?新手不好意思!
发表于 2016-7-9 02:41:28 | 显示全部楼层
回复 12# blue_dvd

$oExcelApp=ObjCreat("Excel.Application")
$oExcelWorkBook=$oExcelApp.WorkBook.Open("...........")

其他操作和VBA类似

如有需要,我后面可以把写的多个EXCEL文件合并的源代码发给你参考
 楼主| 发表于 2016-7-10 09:23:13 | 显示全部楼层
回复 13# callwww

那再好不过了,发来看下,多谢了!
 楼主| 发表于 2016-7-10 09:34:58 | 显示全部楼层
回复 7# 水木子

大师,我问的是读取Excel表格到数组中,不是写入Excel,不过多谢指点新版本一些函数特性。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-4-28 14:48 , Processed in 0.083629 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表