[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文本处理] 【已解决】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文件,但都不太行,看来只能借助第三方工具了

回复 1# Andalye


请把你使用的转换命令发出来看看?顺便把csv文件上传到网盘,以便测试。
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

回复 2# Batcher
是先lvm文本合并读取有效列数值,删除无效行生成的csv文件,所以可以从头开始帮忙看看脚本有没问题,感谢(文件一直上传失败~尴尬)
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. REM 读取多层目录下的.lvm文件数据并提取电压输出值
  4. for /f "delims=" %%a in ('dir /a-d/s/b *.lvm') do (
  5.     set n=0&set/a m+=1
  6.     for /f "tokens=3" %%b in ('type "%%a"') do (
  7.     set/a n+=1&set str!m!!n!=%%b
  8.     )
  9. )
  10. (for /l %%a in (1 1 !n!) do (
  11.     for /l %%b in (1 1 !m!) do (
  12.     set/p=!str%%b%%a! <nul
  13.     )
  14. echo.
  15. ))>new.lvm
  16. REM 删除前几行无效数据
  17. @for %%i in (*.lvm) do @more +7 "%%i">$&move $ "%%i"
  18. REM 读取new.lvm文件内容并按行解析
  19. for /f "tokens=*" %%a in (new.lvm) do (
  20.     set "line=%%a"
  21.     set "validLine="
  22.     set "validElements=0"
  23.    
  24.     REM 将一行数据按空格或其他空白内容分隔成数组
  25.     for %%b in (!line!) do (
  26.         set "element=%%b"
  27.         
  28.         REM 检查数组元素是否为有效数字或小数点
  29.         echo !element! | findstr /r "[0-9]*\.[0-9]*" >nul
  30.         if !errorlevel! equ 0 (
  31.             REM 找到有效元素,将其连接到validLine变量
  32.             if defined validLine (
  33.                 set "validLine=!validLine!,!element!"
  34.             ) else (
  35.                 set "validLine=!element!"
  36.             )
  37.             set /a validElements+=1
  38.         )
  39.     )
  40.     REM 将满足要求的有效行写入new.csv文件
  41.     if !validElements! geq 1 (
  42.         echo !validLine! >>new.csv
  43.         echo 数据提取中...
  44.         
  45.         ren new.csv new.xlsx
  46.     )
  47. )
  48. endlocal
  49. echo 提取数据完成,将于1s后关闭窗口!
  50. ping 127.1 -n 2 >nul
  51. REM excel一列转多列多行的公式:=OFFSET($C$1,(COLUMN(C1)-3)*81+(ROW(C1)-1),)&""
复制代码

TOP

回复 3# Andalye
网盘分享的原lvm文件
链接: https://pan.baidu.com/s/1ly-ZPqR3h0y_apfZbxsXlw?pwd=89ag 提取码: 89ag

TOP

这部分改一下试呢
  1. for /f "delims=" %%a in ('dir /a-d/s/b *.lvm') do (
  2.     set n=0&set/a m+=1
  3.     for /f "tokens=3" %%b in ('type "%%a"') do (
  4.     set/a n+=1&set str!m!_!n!=%%b
  5.     )
  6. )
  7. (for /l %%a in (1 1 !n!) do (
  8.     for /l %%b in (1 1 !m!) do (
  9.     set/p=!str%%b_%%a! <nul
  10.     )
  11. echo.
  12. ))>new.lvm
复制代码

TOP

本帖最后由 terse 于 2023-11-8 21:25 编辑

这里应该也不能改,放到后面吧  ren new.csv new.xlsx

这样生成csv文件呢
  1. @echo off&setlocal enabledelayedexpansion
  2. for /f "delims=" %%a in ('dir /a-d/s/b *.lvm') do (
  3.      set n=0
  4.      for /f "usebackq tokens=3" %%b in ("%%a") do (
  5.           set/a n+=1
  6.           for %%c in (!n!) do set _!n!=!_%%c! %%b
  7.      )
  8. )
  9. (for /l %%a in (8,1,!n!) do (
  10.       set "str="
  11.       for %%b in (!_%%a!) do (
  12.            set s=%%b
  13.            if "!s:~,1!" == "-" (set t=-&set "s=!s:~1!") else set "t="
  14.            for /f "delims=.0123456789" %%c in ("!s!") do set "s="
  15.            if defined s set str=!str!,!t!!s!
  16.       )
  17.       echo!str!
  18. ))>new.csv
  19. pause
复制代码

TOP

本帖最后由 Andalye 于 2023-11-9 09:28 编辑

回复 6# terse


    3楼有我网盘分享的源文件,可以拿来调试。我自己试了你刚刚建议的代码,没抓取成功,抓取后的csv显示截图(部分代码显示是因为我关了echo off):https://ibb.co/ynCRcVr

TOP

不知行否, 会生成两种类型的csv
  1. @echo off&pause&chcp 936 >nul
  2. set max=10000
  3. set m=10000
  4. cd.>______1.csv
  5. for /R %%a in ("*.lvm") do (
  6. set /a m+=1
  7. set file=%%a
  8. call :aaa
  9. echo,
  10. )>>______1.csv
  11. for /l %%a in (%max%,-1,10000) do set array%%a=,
  12. for /f "delims=" %%a in (______1.csv) do (
  13. set l=10000
  14. for %%b in (%%a) do (
  15. setlocal enabledelayedexpansion
  16. for %%c in (!l!) do (
  17. for %%d in ("!array%%c!") do endlocal&set array%%c=%%~d,%%~b
  18. )
  19. set /a l+=1
  20. )
  21. call :bbb
  22. )
  23. cd.>______2.csv
  24. (for /f "tokens=1* delims==," %%a in ('set array') do echo %%b)>>______2.csv
  25. pause&exit/b
  26. :aaa
  27. set n=10000
  28. set /p=""%file%""<nul
  29. for /f "tokens=3" %%u in ('findstr /b "[-0-9][0-9]*\." "%file%"') do (
  30. set /a n+=1
  31. set /p=,%%%u<nul
  32. )
  33. if %n% gtr %max% set max=%n%
  34. goto :eof
  35. :bbb
  36. set /a ll=max+1
  37. if %l% neq %ll% (
  38. for /l %%y in (%l%,1,%max%) do (
  39. setlocal enabledelayedexpansion
  40. for %%z in ("!array%%y!") do endlocal&set array%%y=%%~z,nil
  41. )
  42. )
复制代码
1

评分人数

    • Andalye: 乐于助人,附带注释就更好了技术 + 1

TOP

本帖最后由 Andalye 于 2023-11-9 11:58 编辑

回复 8# Five66
可以提取成功,生成按行和列不同方式排列的2个csv文件,然后有点问题想再请教下:
1.受中文路径或文件名影响了吗,命令行界面显示乱码的,想在数据提取过程中界面提示echo 数据读取中...
2.csv再转换成xlsx文件怎么保持列格式不变,我转换后多列变1列了
我自己尝试的转换代码是
  1. powershell -Command "& {Import-Csv '___1.csv' -Delimiter "`t" | Export-Excel -Path '___1.xlsx' -Show}"
复制代码
但是报错:Export-Excel : 无法将“Export-Excel”项识别为
cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。
所在位置 行:1 字符: 45

TOP

回复 9# Andalye


Install-Module ImportExcel   
要安装个powershell 模块  importexcel

TOP

回复 10# pd1
好吧,安装了ImportExcel这个命令也没多大作用,还是没法正确识别分割符转成xlsx

TOP

回复 11# Andalye


    你确定分割符是制表符?

TOP

不清楚里那边没成功的原因,关掉回显试呢 我这里运行正常的,图片传不上
  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
复制代码

TOP

回复 12# buyiyang


  哈哈不确定,bat 代码东拼西凑的~实在不行只能用excel 自带的数据导入来处理了

TOP

本帖最后由 Andalye 于 2023-11-9 19:50 编辑

回复 13# terse


    一开始就是没开回显的,可能哪里操作有误了吧,明天我再试试。方便的话可以你把完整的代码贴一下吗,谢谢

TOP

返回列表