本帖最后由 blue_dvd 于 2012-11-22 20:33 编辑 #include <File.au3>
#include <Excel.au3>
#include <Array.au3>
#include <GuiListView.au3>
#include <ListViewConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
;打开一个选文件对话框;
Local $sFilePath1=FileOpenDialog("选择导入电子表格文件", @ScriptDir&"", "Microsoft Excel 工作薄 (*.xls)", 1+2+4)
;MsgBox(0,"","执行到这里打开对话")
;把选中的文件作为Excel文件进行读取,读取成功就激活,失败警告并退出;
Local $oExcel = ObjGet($sFilePath1)
If @error Then
MsgBox(0, "警告", "Error Getting an active Excel Object. Error code: " & Hex(@error, 8))
Exit
EndIf
;读取Excel表格到数组
Local $aArray =_ExcelReadSheetToArray($oExcel)
If @error Then
MsgBox(0, "警告", "Error Getting an active Excel Object. Error code: " & Hex(@error, 8))
Exit
EndIf
Local $oExcel2 = _ExcelBookOpen($sFilePath1)
_ExcelSheetActivate($oExcel2,2)
_ExcelWriteSheetFromArray($oExcel2,$aArray)
_ExcelBookSave($oExcel2)
_ExcelBookClose($oExcel2)
运行后,在打开Excel文件发现,没有保存表2的信息!
解决方法:7楼kevinch的解法#include <Excel.au3>
Local $sFilePath1 = FileOpenDialog("选择导入电子表格文件", @ScriptDir & "", "Microsoft Excel 工作薄 (*.xls)", 1 + 2 + 4)
$oExcel = _ExcelBookOpen($sFilePath1, 1)
With $oExcel.sheets(1)
For $row=.usedrange.row To .usedrange.row+.usedrange.rows.count-1
$col=1
If Mod($row,2)=0 Then $col=2
$off=1
If $oExcel.sheets(2).cells($oExcel.sheets(2).rows.count,$col).end(3).address=$oExcel.sheets(2).cells(1,$col).address And $oExcel.sheets(2).cells(1,$col).value="" Then $off=0
$oExcel.sheets(2).cells($oExcel.sheets(2).rows.count,$col).end(3).offset($off).resize($oExcel.application.intersect(.usedrange,.rows($row)).cells.count).value=$oExcel.application.transpose($oExcel.application.intersect(.usedrange,.rows($row)))
Next
EndWith
_ExcelBookSave($oExcel)
_ExcelBookClose($oExcel)
|