标题: [文件操作] 求Bat:删除excel中的某些行 [打印本页]
作者: 19219962414 时间: 2023-10-8 19:13 标题: 求Bat:删除excel中的某些行
各位大佬:想删除某个文件的某些行,具体规则为:文件(.xlsx)共2n行,想删除45至n+16行,求大神们指导,感谢!
作者: Nsqs 时间: 2023-10-8 20:06
bat做不了- #PowerShell
- $e=New-Object -ComObject Excel.Application
- $e.DisplayAlerts=0
- $wb=$e.Workbooks.Open("$PSScriptRoot\1.xlsx")
- $ws=$wb.Sheets('Sheet1')
- $n=100
- [void]$ws.Range("45:$($n+16)").Delete()
- $wb.Save()
- $e.Quit()
复制代码
作者: Five66 时间: 2023-10-9 00:38
不知是不是这样子
将代码保存为ansi编码的bat
先用excel打开想要删除的xls文件(只允许有一个xls文件被打开)
然后运行bat
运行完成后回到excel界面自行保存或另存或关闭文件- @if (0)/*==(0)/* echo off
-
- set n=100
- set s=45
- set /a e=n+16
-
- cscript //nologo //e:jscript "%~f0" %n% %s% %e%
-
- echo 完成,请回到excel界面自行保存或另存或关闭&echo.&pause&exit/b
- rem */@end
-
- var ess=new ActiveXObject("excel.sheet");
- ess.Application.Visible=false;
- ess.Application.ScreenUpdating=false;
- var nil=null,wb=nil;
- for(var e=new Enumerator(ess.Application.Workbooks);!e.atEnd();e.moveNext()){
- wb=e.item();break;
- }
- wb.ActiveSheet.Range(WScript.Arguments(1)+":"+WScript.Arguments(2)).Delete(-4162);
- wb=nil;
- ess.Application.Visible=true;
- ess.Application.ScreenUpdating=true;
- ess.Close(0);ess=nil;
复制代码
作者: 19219962414 时间: 2023-10-9 10:26
回复 2# Nsqs 好的谢谢大佬
作者: 19219962414 时间: 2023-10-9 10:26
回复 3# Five66 谢谢大佬,我先看看
作者: Batcher 时间: 2023-10-9 16:35
回复 1# 19219962414
test-1.bat- # & cls & @cd /d "%~dp0" & powershell "Get-Content '%~0' | Out-String | Invoke-Expression " & exit /b
- $Excel = New-Object -ComObject Excel.Application
- $Excel.Visible = $false
- $Workbook = $Excel.Workbooks.Open("$PWD\1.xlsx")
- $Sheet = $Workbook.WorkSheets.Item(1)
- $n = 100
- [void]$Sheet.Range("45:$($n+16)").Delete()
- $Workbook.Save()
- $Workbook.Close()
- $Excel.Quit()
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |