将数据从一个Excel工作表写入另一个

时间:2015-01-07 10:57:54

标签: batch-file phpexcel merge-file

我的文件夹中有一些.csv文件,其名称为1filename.csv2filename.csv3filename.csv,然后是1abc.csv2abc.csv等等。

我需要合并具有相同帖子名称的文件,每个文件都有相同的号码。具有相同标题的列。因此,最终文件应该只有一行具有列标题并附加数据。

示例:1。1filename.csv2filename.csv3filename.csv应合并到filename.csv(数据一个接一个地附加)

2. 1abc.csv2abc.csv将合并并形成abc.csv

我可以在批处理文件中或在phpexcel的帮助下实现此目的吗?我想在批处理文件中做,但我不确定它是否可行。

由于

这些是需要合并的文件我将使用不同的域分割1000个文件: enter image description here

这就是csv文件的样子,一个文件最多可以包含800行,并且具有相同标题的相同列数: enter image description here

2 个答案:

答案 0 :(得分:1)

我可以使用:

copy / b 1filename.csv + 2filename.csv filename.csv

copy / b 1abc.csv + 2abc.csv abc.csv

答案 1 :(得分:0)

编辑修复了小错误

原始代码没有考虑文件名将按字母顺序处理,而不是在名字后的组中处理。

@echo off
setlocal EnableDelayedExpansion

rem I have some .csv files in my folder 
rem with names like 1filename.csv, 2filename.csv, 3filename.csv then 1abc.csv, 2abc.csv and so on.
set "postName="
for /F %%a in ('dir /B *.csv ^| sort /+2') do (
   rem I need to merge files with same post name
   set "name=%%a"
   if "!name:~1!" neq "!postName!" (
      set "postName=!name:~1!"
      rem and each file has same no. of columns with same heading.
      rem So final file should have only one row with titles for columns 
      echo Merging data into: !postName!
      copy "%%a" "!postName!" > NUL
   ) else (
      rem and appended with data.
      (for /F "skip=1 delims=" %%b in (%%a) do echo %%b) >> "!postName!"
   )
)

你总是应该发布一段文件或至少描述它的内容,否则我们只能猜测它......

如果文件包含特殊的批处理字符,例如< > | !

,则此批处理解决方案可能会失败

编辑回复评论

请注意,图片中的信息无法作为文本进行复制粘贴。您应该将数据作为包含在代码标记中的文本发布,如下所示:

C:\ dir /b
1www.ais.csv
1www.futureshade.com.au.csv
2www.ais.csv
2www.futureshade.com.au.csv
3www.ais.csv
test.bat

C:\ test
Merging data into: www.ais.csv
Merging data into: www.futureshade.com.au.csv

C:\ dir /b
1www.ais.csv
1www.futureshade.com.au.csv
2www.ais.csv
2www.futureshade.com.au.csv
3www.ais.csv
test.bat
www.ais.csv
www.futureshade.com.au.csv

请注意此解决方案适用于第一个字符中只有一位的文件名,因此如果同一帖子名称中有超过9个文件,则会失败(这是你应该从一开始就发布的细节类型。

2ND EDIT 添加了输出测试

下面的测试表明此解决方案的工作原理如下:

C:\ dir /b
1www.ais.csv
1www.futureshade.com.au.csv
2www.ais.csv
2www.futureshade.com.au.csv
3www.ais.csv
test.bat

C:\ type 1www.ais.csv
Ad group,Keyword,Currency,Avg. Monthly Search,Competition,Suggested bic,Impr. sh
ar,In account?,In plan?,Extracted from
1AIS,ais,AUD,14800,0.01,3.39,--,N,N
1AIS,medicare,AUD,201000,0.04,2.97,--,N,N

C:\ type 2www.ais.csv
Ad group,Keyword,Currency,Avg. Monthly Search,Competition,Suggested bic,Impr. sh
ar,In account?,In plan?,Extracted from
2AIS,ais,AUD,14800,0.01,3.39,--,N,N
2AIS,medicare,AUD,201000,0.04,2.97,--,N,N

C:\ type 3www.ais.csv
Ad group,Keyword,Currency,Avg. Monthly Search,Competition,Suggested bic,Impr. sh
ar,In account?,In plan?,Extracted from
3AIS,ais,AUD,14800,0.01,3.39,--,N,N
3AIS,medicare,AUD,201000,0.04,2.97,--,N,N

C:\ type 1www.futureshade.com.au.csv
Ad group,Keyword,Currency,Avg. Monthly Search,Competition,Suggested bic,Impr. sh
ar,In account?,In plan?,Extracted from
1FUTURESHADE,ais,AUD,14800,0.01,3.39,--,N,N
1FUTURESHADE,medicare,AUD,201000,0.04,2.97,--,N,N

C:\ type 2www.futureshade.com.au.csv
Ad group,Keyword,Currency,Avg. Monthly Search,Competition,Suggested bic,Impr. sh
ar,In account?,In plan?,Extracted from
2FUTURESHADE,ais,AUD,14800,0.01,3.39,--,N,N
2FUTURESHADE,medicare,AUD,201000,0.04,2.97,--,N,N

C:\ test
Merging data into: www.ais.csv
Merging data into: www.futureshade.com.au.csv

C:\ dir /B
1www.ais.csv
1www.futureshade.com.au.csv
2www.ais.csv
2www.futureshade.com.au.csv
3www.ais.csv
test.bat
www.ais.csv
www.futureshade.com.au.csv

C:\ type www.ais.csv
Ad group,Keyword,Currency,Avg. Monthly Search,Competition,Suggested bic,Impr. sh
ar,In account?,In plan?,Extracted from
1AIS,ais,AUD,14800,0.01,3.39,--,N,N
1AIS,medicare,AUD,201000,0.04,2.97,--,N,N
2AIS,ais,AUD,14800,0.01,3.39,--,N,N
2AIS,medicare,AUD,201000,0.04,2.97,--,N,N
3AIS,ais,AUD,14800,0.01,3.39,--,N,N
3AIS,medicare,AUD,201000,0.04,2.97,--,N,N

C:\ type www.futureshade.com.au.csv
Ad group,Keyword,Currency,Avg. Monthly Search,Competition,Suggested bic,Impr. sh
ar,In account?,In plan?,Extracted from
1FUTURESHADE,ais,AUD,14800,0.01,3.39,--,N,N
1FUTURESHADE,medicare,AUD,201000,0.04,2.97,--,N,N
2FUTURESHADE,ais,AUD,14800,0.01,3.39,--,N,N
2FUTURESHADE,medicare,AUD,201000,0.04,2.97,--,N,N