简体   繁体   English

从Excel复制单元格范围

[英]Copy range of cells from Excel

I'm trying to copy some cells from Microsoft Excel to another program using AutoIt, but I can just copy one cell at a time. 我正在尝试使用AutoIt将某些单元格从Microsoft Excel复制到另一个程序,但是我一次只能复制一个单元格。 Is there a way to copy a group or range of cells? 有没有办法复制一组或一组单元格?

My code: 我的代码:

Local $Resultcpf = Excel_RangeRead($oWorkbook, default, "L4")

ClipPut($Resultcpf)
$Data = ClipGet()

" Is there a another way to copy a group of range? " 还有另一种复制一组范围的方法吗?

As per Documentation - User Defined Function Reference - _Excel_RangeRead() : 根据文档-用户定义函数参考_Excel_RangeRead()

Reads the value, formula or displayed text from a cell or range of cells of the specified workbook and worksheet 从指定工作簿和工作表的一个单元格或单元格区域中读取值,公式或显示的文本

Example : 范例:

#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <Excel.au3>

Global Const $g_sFileDoc = 'C:\document.xlsx', _
             $g_sRange   = 'L:L', _; 'L1:L4'
             $g_sPrompt  = 'Press [CTRL] + [V] to paste contents of %s.'

Global       $g_oExcel   = _Excel_Open(), _
             $g_oBook    = _Excel_BookOpen($g_oExcel, $g_sFileDoc)
Global       $g_aData    = _Excel_RangeRead($g_oBook, Default, $g_oBook.ActiveSheet.Usedrange.Columns($g_sRange))
Global       $g_sData    = _ArrayToString($g_aData)

_ArrayDisplay($g_aData)
ClipPut($g_sData)
MsgBox($MB_SYSTEMMODAL, @ScriptName, StringFormat($g_sPrompt, $g_sRange))

You have to define the range by B3:C5 for two columns or A1:A17 for one column and so on. 您必须通过两列的B3:C5或一列的A1:A17来定义范围,依此类推。 The range has to be divided by colon ":" sign. 范围必须用冒号 “:”符号分隔。

#include-once
#include <Array.au3>
#include <Excel.au3>

Global $sFileExcel  = @DesktopDir & '\MyExcelSheet.xlsx'
Global $sExcelRange = 'B3:C5'

Func _readExcelContent()
    Local $oExcel    = _Excel_Open()
    Local $oWorkbook = _Excel_BookOpen($oExcel, $sFileExcel)
    Local $aData     = _Excel_RangeRead($oWorkbook, Default, $sExcelRange, 2)
    _Excel_Close($oExcel, Default, True) ; excel will be closed
    Return $aData
EndFunc

Global $aData = _readExcelContent()
_ArrayDisplay($aData) ; this is just a preview of the read data

ClipPut(_ArrayToString($aData))

The result/output could be : 结果/输出可能是:

text1|
text2|text4
text3|

Depending on Excel content of course. 当然取决于Excel的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM