标题: [文本处理] 【已解决】bat命令将csv转成.xlsx而格式不变 [打印本页]
作者: Andalye 时间: 2023-11-7 13:24 标题: 【已解决】bat命令将csv转成.xlsx而格式不变
本帖最后由 Andalye 于 2023-11-10 13:19 编辑
求助,使用bat命令将.csv文件转成.xlsx后内容格式乱了,原本3列的内容集中到1列去了
网盘分享的原lvm文件
链接: https://pan.baidu.com/s/1ly-ZPqR3h0y_apfZbxsXlw?pwd=89ag 提取码: 89ag
18楼的大佬代码已解决99%。感谢各位热心的大佬
自己尝试直接用bat命令将csv转成xlsx文件,但都不太行,看来只能借助第三方工具了
作者: Batcher 时间: 2023-11-7 13:52
回复 1# Andalye
请把你使用的转换命令发出来看看?顺便把csv文件上传到网盘,以便测试。
作者: Andalye 时间: 2023-11-8 17:06
回复 2# Batcher
是先lvm文本合并读取有效列数值,删除无效行生成的csv文件,所以可以从头开始帮忙看看脚本有没问题,感谢(文件一直上传失败~尴尬)- @echo off
- setlocal enabledelayedexpansion
-
- REM 读取多层目录下的.lvm文件数据并提取电压输出值
- for /f "delims=" %%a in ('dir /a-d/s/b *.lvm') do (
- set n=0&set/a m+=1
- for /f "tokens=3" %%b in ('type "%%a"') do (
- set/a n+=1&set str!m!!n!=%%b
- )
- )
- (for /l %%a in (1 1 !n!) do (
- for /l %%b in (1 1 !m!) do (
- set/p=!str%%b%%a! <nul
- )
- echo.
- ))>new.lvm
-
- REM 删除前几行无效数据
- @for %%i in (*.lvm) do @more +7 "%%i">$&move $ "%%i"
-
- REM 读取new.lvm文件内容并按行解析
- for /f "tokens=*" %%a in (new.lvm) do (
- set "line=%%a"
- set "validLine="
- set "validElements=0"
-
- REM 将一行数据按空格或其他空白内容分隔成数组
- for %%b in (!line!) do (
- set "element=%%b"
-
- REM 检查数组元素是否为有效数字或小数点
- echo !element! | findstr /r "[0-9]*\.[0-9]*" >nul
- if !errorlevel! equ 0 (
- REM 找到有效元素,将其连接到validLine变量
- if defined validLine (
- set "validLine=!validLine!,!element!"
- ) else (
- set "validLine=!element!"
- )
- set /a validElements+=1
- )
- )
- REM 将满足要求的有效行写入new.csv文件
- if !validElements! geq 1 (
- echo !validLine! >>new.csv
- echo 数据提取中...
-
- ren new.csv new.xlsx
- )
- )
- endlocal
-
- echo 提取数据完成,将于1s后关闭窗口!
- ping 127.1 -n 2 >nul
-
- REM excel一列转多列多行的公式:=OFFSET($C$1,(COLUMN(C1)-3)*81+(ROW(C1)-1),)&""
复制代码
作者: Andalye 时间: 2023-11-8 17:18
回复 3# Andalye
网盘分享的原lvm文件
链接: https://pan.baidu.com/s/1ly-ZPqR3h0y_apfZbxsXlw?pwd=89ag 提取码: 89ag
作者: terse 时间: 2023-11-8 19:47
这部分改一下试呢- for /f "delims=" %%a in ('dir /a-d/s/b *.lvm') do (
- set n=0&set/a m+=1
- for /f "tokens=3" %%b in ('type "%%a"') do (
- set/a n+=1&set str!m!_!n!=%%b
- )
- )
- (for /l %%a in (1 1 !n!) do (
- for /l %%b in (1 1 !m!) do (
- set/p=!str%%b_%%a! <nul
- )
- echo.
- ))>new.lvm
复制代码
作者: terse 时间: 2023-11-8 19:51
本帖最后由 terse 于 2023-11-8 21:25 编辑
这里应该也不能改,放到后面吧 ren new.csv new.xlsx
这样生成csv文件呢- @echo off&setlocal enabledelayedexpansion
- for /f "delims=" %%a in ('dir /a-d/s/b *.lvm') do (
- set n=0
- for /f "usebackq tokens=3" %%b in ("%%a") do (
- set/a n+=1
- for %%c in (!n!) do set _!n!=!_%%c! %%b
- )
- )
-
- (for /l %%a in (8,1,!n!) do (
- set "str="
- for %%b in (!_%%a!) do (
- set s=%%b
- if "!s:~,1!" == "-" (set t=-&set "s=!s:~1!") else set "t="
- for /f "delims=.0123456789" %%c in ("!s!") do set "s="
- if defined s set str=!str!,!t!!s!
- )
- echo!str!
- ))>new.csv
-
- pause
复制代码
作者: Andalye 时间: 2023-11-9 09:16
本帖最后由 Andalye 于 2023-11-9 09:28 编辑
回复 6# terse
3楼有我网盘分享的源文件,可以拿来调试。我自己试了你刚刚建议的代码,没抓取成功,抓取后的csv显示截图(部分代码显示是因为我关了echo off):https://ibb.co/ynCRcVr
作者: Five66 时间: 2023-11-9 09:54
不知行否, 会生成两种类型的csv- @echo off&pause&chcp 936 >nul
-
- set max=10000
- set m=10000
-
- cd.>______1.csv
- for /R %%a in ("*.lvm") do (
- set /a m+=1
- set file=%%a
- call :aaa
- echo,
- )>>______1.csv
-
- for /l %%a in (%max%,-1,10000) do set array%%a=,
- for /f "delims=" %%a in (______1.csv) do (
- set l=10000
- for %%b in (%%a) do (
- setlocal enabledelayedexpansion
- for %%c in (!l!) do (
- for %%d in ("!array%%c!") do endlocal&set array%%c=%%~d,%%~b
- )
- set /a l+=1
- )
- call :bbb
- )
- cd.>______2.csv
- (for /f "tokens=1* delims==," %%a in ('set array') do echo %%b)>>______2.csv
-
- pause&exit/b
-
- :aaa
- set n=10000
- set /p=""%file%""<nul
- for /f "tokens=3" %%u in ('findstr /b "[-0-9][0-9]*\." "%file%"') do (
- set /a n+=1
- set /p=,%%%u<nul
- )
- if %n% gtr %max% set max=%n%
- goto :eof
-
- :bbb
- set /a ll=max+1
- if %l% neq %ll% (
- for /l %%y in (%l%,1,%max%) do (
- setlocal enabledelayedexpansion
- for %%z in ("!array%%y!") do endlocal&set array%%y=%%~z,nil
- )
- )
复制代码
作者: Andalye 时间: 2023-11-9 10:09
本帖最后由 Andalye 于 2023-11-9 11:58 编辑
回复 8# Five66
可以提取成功,生成按行和列不同方式排列的2个csv文件,然后有点问题想再请教下:
1.受中文路径或文件名影响了吗,命令行界面显示乱码的,想在数据提取过程中界面提示echo 数据读取中...
2.csv再转换成xlsx文件怎么保持列格式不变,我转换后多列变1列了
我自己尝试的转换代码是- powershell -Command "& {Import-Csv '___1.csv' -Delimiter "`t" | Export-Excel -Path '___1.xlsx' -Show}"
复制代码
但是报错:Export-Excel : 无法将“Export-Excel”项识别为
cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
所在位置 行:1 字符: 45
作者: pd1 时间: 2023-11-9 14:05
回复 9# Andalye
Install-Module ImportExcel
要安装个powershell 模块 importexcel
作者: Andalye 时间: 2023-11-9 14:34
回复 10# pd1
好吧,安装了ImportExcel这个命令也没多大作用,还是没法正确识别分割符转成xlsx
作者: buyiyang 时间: 2023-11-9 15:30
回复 11# Andalye
你确定分割符是制表符?
作者: terse 时间: 2023-11-9 16:30
不清楚里那边没成功的原因,关掉回显试呢 我这里运行正常的,图片传不上- 0.021841,0.034476,0.057524,0.021841,0.034476,0.037996,0.026896,0.017917,0.016473,0.046873,0.021037,0.066235,0.021384,0.041630,0.070698
- 0.019911,0.032562,0.055642,0.019911,0.032562,0.036256,0.025116,0.016234,0.014831,0.044821,0.019360,0.064212,0.019452,0.039908,0.069087
- 0.017951,0.030625,0.053731,0.017951,0.030625,0.034484,0.023288,0.014520,0.013140,0.042733,0.017666,0.062174,0.017484,0.038172,0.067416
- 0.016004,0.028708,0.051819,0.016004,0.028708,0.032702,0.021456,0.012811,0.011469,0.040664,0.015969,0.060117,0.015515,0.036426,0.065790
- 0.014071,0.026792,0.049911,0.014071,0.026792,0.030952,0.019674,0.011120,0.009805,0.038591,0.014306,0.058073,0.013550,0.034686,0.064145
- 0.012078,0.024837,0.047972,0.012078,0.024837,0.029164,0.017823,0.009389,0.008116,0.036497,0.012603,0.056005,0.011563,0.032951,0.062466
复制代码
作者: Andalye 时间: 2023-11-9 19:45
回复 12# buyiyang
哈哈不确定,bat 代码东拼西凑的~实在不行只能用excel 自带的数据导入来处理了
作者: Andalye 时间: 2023-11-9 19:46
本帖最后由 Andalye 于 2023-11-9 19:50 编辑
回复 13# terse
一开始就是没开回显的,可能哪里操作有误了吧,明天我再试试。方便的话可以你把完整的代码贴一下吗,谢谢
作者: buyiyang 时间: 2023-11-9 20:26
回复 14# Andalye
csv转xlsx.bat- set csvfile="R:\old.csv"
- set xlsxfile="R:\new.xlsx"
- mshta vbscript:execute("With CreateObject(""Excel.Application"").Workbooks.Open("%csvfile%"):.SaveAs "%xlsxfile%",51:.Parent.Quit:End With")(close)
复制代码
作者: Batcher 时间: 2023-11-9 20:38
回复 4# Andalye
建议更新顶楼帖子,把网盘文件地址放上去,方便他人查看。
作者: Five66 时间: 2023-11-10 09:30
回复 9# Andalye
试试换成下面的吧,感觉之前的容易超过长度限制
bat代码文件保存为ansi或gbk编码的文件
生成的csv的编码为gbk,分割符为英文逗号,因此文件名中最好不要有英文逗号 和特殊字符
还有应该可以用excel直接打开csv文件,然后另存为xlsx格式的文件的- @echo off&pause&chcp 936 >nul
-
- rem 将bat代码文件保存为gbk编码的文件
- rem 生成的csv的编码为gbk,分割符为英文逗号,因此文件名中最好不要有英文逗号和特殊字符
-
- rem 循环初始值
- set iii=10000
-
- rem 数据列最大初始值
- set max=10000
-
- rem 文件数量初始值
- set m=10000
-
- echo,
- rem 递归枚举当前目录下的.lvm文件并提取数据
- for /R %%a in ("*.lvm") do (
- set n=10000
- set file=%%a
- echo 开始提取文件"%%a"数据列
- call :aaa
- echo 提取完成&echo.
- set /a m+=1
- )
-
- set /a m-=1
- echo 开始生成csv文件...&echo,
-
- echo 创建文件 ______a.csv
- cd.>______a.csv
- for /l %%a in (%iii%,1,%m%) do (
- set lll=0
- rem 获取变量值并分割,重定向数据到csv文件
- for /f "tokens=1* delims==" %%b in ('set array%%a_') do (
- if defined lll (
- set /p="%%c"<nul
- set lll=
- ) else set /p=,%%c<nul
- )
- echo,
- )>>______a.csv
- echo 已生成csv文件&echo.
-
- echo 创建文件 ______b.csv
- cd.>______b.csv
- setlocal enabledelayedexpansion
- rem 重定向数据到csv文件,分隔符为英文逗号,不足最大数据列的用nil填充
- (for /l %%y in (%iii%,1,%max%) do (
- set lll=0
- for /l %%z in (%iii%,1,%m%) do (
- if !lll! == 0 (
- if "!array%%z_%%y!" neq "" (set/p=!array%%z_%%y!<nul) else set/p=nil<nul
- set /a lll+=1
- ) else (
- if "!array%%z_%%y!" neq "" (set/p=,!array%%z_%%y!<nul) else set/p=,nil<nul
- ))
- echo,
- ))>>______b.csv
- echo 已生成csv文件&echo.
- endlocal
-
- pause&exit/b
-
- rem 提取文件数据过程
- :aaa
-
- set "array%m%_%n%=%file%"
-
- rem 读取文件并提取数据
- for /f "tokens=3" %%b in ('findstr /b "[-0-9][0-9]*\." "%file%"') do (
- set /a n+=1
- setlocal enabledelayedexpansion
- for %%c in (!m!_!n!) do endlocal&set array%%c=%%b
- )
-
- rem 重设最大数据列值
- if %n% gtr %max% set max=%n%
- goto :eof
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |