找回密码
 加入
搜索
查看: 15219|回复: 19

[效率算法] Excel中“Sheet1”整理数据后写入“Sheet2”,结果Excel文件保存不了[已解决]

  [复制链接]
发表于 2012-11-19 18:02:30 | 显示全部楼层 |阅读模式
本帖最后由 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)

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2012-11-20 19:59:13 | 显示全部楼层
难道没人懂?
发表于 2012-11-20 21:43:16 | 显示全部楼层

#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)
$oExcel = _ExcelBookOpen($sFilePath1, 1)
_ExcelSheetActivate($oExcel, 1) ;激活表
$xx = $oExcel.activesheet.usedrange.rows.count
$oExcel.Sheets(2).range("a" & 1).resize($xx, 1).Formula = $oExcel.Sheets(1).range("a" & 1).resize($xx, 1).Formula ;一句话复制表1的A列到表2的A列
_ExcelBookSave($oExcel)
_ExcelBookClose($oExcel)
发表于 2012-11-20 21:49:56 | 显示全部楼层
$oExcel.Sheets(2).range($oExcel.Sheets(1).usedrange.address).Formula = $oExcel.Sheets(1).usedrange.Formula ;一句话复制表1的内容到表2
 楼主| 发表于 2012-11-21 00:25:14 | 显示全部楼层
回复 3# 骗子
太感谢了!
如果不是整个表复制过去而是把第一个表的1列n行,分成2列n/2行,写入第二个表格保存呢?
请问要用什么语句?
 楼主| 发表于 2012-11-21 00:30:34 | 显示全部楼层
本帖最后由 blue_dvd 于 2012-11-21 00:32 编辑

回复 4# kevinch
把第一个表格的单数行复制到第二个表格的第一列,把第一个表格的双数行复制到第二个表格的第2列,要如何做保存操作?
如下图

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
发表于 2012-11-21 09:42:04 | 显示全部楼层
#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)
这个试下
 楼主| 发表于 2012-11-21 13:19:58 | 显示全部楼层
回复 7# kevinch
哪里有关于
For $row=.usedrange.row To .usedrange.row+.usedrange.rows.count-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)))
这些语句的学习资料,感谢kevinch !
发表于 2012-11-21 20:47:06 | 显示全部楼层
其实就是直接改写的vba语句,你可以搜索相关的vba内容学习,一般输关键字"excel vba"就会有很多了
 楼主| 发表于 2012-11-22 00:00:53 | 显示全部楼层
这个试下
kevinch 发表于 2012-11-21 09:42


测试没有通过!“Sheet2”没动静!
发表于 2012-11-22 00:57:41 | 显示全部楼层
回复 5# blue_dvd
笨办法:
1打开excel表,
2,激活表1,读取一行,激活表2,写入
不停的重复下去就可以了
发表于 2012-11-22 07:31:46 | 显示全部楼层
回复 10# blue_dvd
发你的测试文件看下
 楼主| 发表于 2012-11-22 12:58:52 | 显示全部楼层
回复 12# kevinch
测试文件在1楼!
发表于 2012-11-22 19:05:32 | 显示全部楼层
回复 13# blue_dvd
刚用一楼的附件测试了,正常的啊!
 楼主| 发表于 2012-11-22 20:30:59 | 显示全部楼层
回复 14# kevinch
不好意思,我用了你的程序测试比较大的文件,弹出Excel后,感觉好像没动,我还以为不行就直接把Excel关了!刚才测试了短的表格后等了一会运行完毕才发现没问题!多谢了!可以看出运行Excel写还是比较慢的!谢谢kevinch
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-6-11 14:37 , Processed in 0.083131 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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