Board logo

标题: [文本处理] 【已解决】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文件,所以可以从头开始帮忙看看脚本有没问题,感谢(文件一直上传失败~尴尬)
  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),)&""
复制代码

作者: 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

这部分改一下试呢
  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
复制代码

作者: terse    时间: 2023-11-8 19:51

本帖最后由 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
复制代码

作者: 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
  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. )
复制代码

作者: Andalye    时间: 2023-11-9 10:09

本帖最后由 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
作者: 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

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

作者: 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
  1. set csvfile="R:\old.csv"
  2. set xlsxfile="R:\new.xlsx"
  3. 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格式的文件的
  1. @echo off&pause&chcp 936 >nul
  2. rem 将bat代码文件保存为gbk编码的文件
  3. rem 生成的csv的编码为gbk,分割符为英文逗号,因此文件名中最好不要有英文逗号和特殊字符
  4. rem 循环初始值
  5. set iii=10000
  6. rem 数据列最大初始值
  7. set max=10000
  8. rem 文件数量初始值
  9. set m=10000
  10. echo,
  11. rem 递归枚举当前目录下的.lvm文件并提取数据
  12. for /R %%a in ("*.lvm") do (
  13. set n=10000
  14. set file=%%a
  15. echo 开始提取文件"%%a"数据列
  16. call :aaa
  17. echo 提取完成&echo.
  18. set /a m+=1
  19. )
  20. set /a m-=1
  21. echo 开始生成csv文件...&echo,
  22. echo 创建文件 ______a.csv
  23. cd.>______a.csv
  24. for /l %%a in (%iii%,1,%m%) do (
  25. set lll=0
  26. rem 获取变量值并分割,重定向数据到csv文件
  27. for /f "tokens=1* delims==" %%b in ('set array%%a_') do (
  28. if defined lll (
  29. set /p="%%c"<nul
  30. set lll=
  31. ) else set /p=,%%c<nul
  32. )
  33. echo,
  34. )>>______a.csv
  35. echo 已生成csv文件&echo.
  36. echo 创建文件 ______b.csv
  37. cd.>______b.csv
  38. setlocal enabledelayedexpansion
  39. rem 重定向数据到csv文件,分隔符为英文逗号,不足最大数据列的用nil填充
  40. (for /l %%y in (%iii%,1,%max%) do (
  41. set lll=0
  42. for /l %%z in (%iii%,1,%m%) do (
  43. if !lll! == 0 (
  44. if "!array%%z_%%y!" neq "" (set/p=!array%%z_%%y!<nul) else set/p=nil<nul
  45. set /a lll+=1
  46. ) else (
  47. if "!array%%z_%%y!" neq "" (set/p=,!array%%z_%%y!<nul) else set/p=,nil<nul
  48. ))
  49. echo,
  50. ))>>______b.csv
  51. echo 已生成csv文件&echo.
  52. endlocal
  53. pause&exit/b
  54. rem 提取文件数据过程
  55. :aaa
  56. set "array%m%_%n%=%file%"
  57. rem 读取文件并提取数据
  58. for /f "tokens=3" %%b in ('findstr /b "[-0-9][0-9]*\." "%file%"') do (
  59. set /a n+=1
  60. setlocal enabledelayedexpansion
  61. for %%c in (!m!_!n!) do endlocal&set array%%c=%%b
  62. )
  63. rem 重设最大数据列值
  64. if %n% gtr %max% set max=%n%
  65. goto :eof
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2