China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-03 13:02
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to directly convert a TXT file to an XLS file using batch processing View 4,919 Replies 16
Original Poster Posted 2007-02-24 00:29 ·  中国 安徽 马鞍山 电信
中级用户
★★
Credits 493
Posts 228
Joined 2007-02-16 00:38
19-year member
UID 79596
Gender Male
From 安徽
Status Offline
How to directly convert a TXT file to an XLS file using batch processing

report.txt

1 13 45 0 4500 15615
2 13 45 0 4500 15600
3 26 255 0 25500 15601
4 18 81 0 8100 15602
5 29 188 0 18800 15605
6 25 150 0 15000 15606
7 84 333 12 33900 15609
8 20 105 1 10550 15610
9 26 128 0 12800 15611
10 14 149 0 14900 15612
11 5 33 0 3300 15613
12 0 0 0 0 15616
13 48 190 16 19800 15617
14 321 1702 29 171650


[ Last edited by xycoordinate on 2007-2-23 at 11:34 AM ]
Floor 2 Posted 2007-02-24 00:47 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
xls
What format do you see when you open it with Notepad?
Floor 3 Posted 2007-02-24 01:32 ·  中国 吉林 四平 联通
高级用户
★★★
Credits 859
Posts 413
Joined 2006-08-14 21:55
19-year member
UID 60532
Status Offline
Can the numbers in the txt be accurately given to the xls file?
Floor 4 Posted 2007-02-24 01:34 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Of course, if the format is regular.
Floor 5 Posted 2007-02-24 01:34 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
Now there's no time to write code. Here's an idea:

First, replace double spaces with single spaces, then replace single spaces with {TAB} + single space ({TAB}: tab character)

Then directly open the TXT file with Excel
Floor 6 Posted 2007-02-24 01:53 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
After looking at the original existing xls document, it seems not in plain text form...

Then it's the simulated input on the 5th floor...
Floor 7 Posted 2007-02-24 02:40 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
The script can reference this object Excel.Application
Floor 8 Posted 2007-02-24 02:50 ·  中国 宁夏 银川 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
This shouldn't be possible. An XLS file is garbled when opened with Notepad.
Floor 9 Posted 2007-02-24 03:20 ·  美国 缅因州
中级用户
★★
Credits 316
Posts 152
Joined 2006-06-18 13:01
20-year member
UID 57204
Gender Male
Status Offline
sed "s/^*//;s/\+/\t/g" report.txt> result.xls

For downloading and using sed, please search the forum with "sed"

[ Last edited by amao on 2007-2-24 at 03:22 AM ]
Floor 10 Posted 2007-02-24 03:27 ·  中国 江苏 苏州 联通
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
If separated by spaces, tabs, or commas, you can directly open the TXT file with EXCEL, and EXCEL will ask you how to separate.
PS: Tabs are well separated. Copy to the clipboard, then open EXCEL and paste. If so, you need to use VBS.
Floor 11 Posted 2007-02-24 03:56 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
The generated "test.xls" can be directly opened with Excel

@echo off
setlocal EnableDelayedExpansion
cd.>test.xls
for /f "delims=" %%n in (test.txt) do (
set "str=%%n"
set "str=!str: = !"
set "str=!str: ={TAB} !"
echo !str!
>>test.xls echo !str!
)
pause


{TAB}: Keyboard TAB tab

PS: Is there a problem with the last line of your data "14 321 1702 29 171650 ", missing the third data?

[ Last edited by zh159 on 2007-2-23 at 06:16 PM ]
Floor 12 Posted 2007-02-24 09:38 ·  中国 安徽 马鞍山 电信
中级用户
★★
Credits 493
Posts 228
Joined 2007-02-16 00:38
19-year member
UID 79596
Gender Male
From 安徽
Status Offline
Originally posted by zh159 at 2007-2-23 02:56 PM:
The generated "test.xls" can be directly opened with Excel

@echo off
setlocal EnableDelayedExpansion
cd.>test.xls
for /f "delims=" %%n in (test.txt) do (
set "str=%%n"
set "str=!str: = !"
set "str=!str: ={TAB} !"

echo !str!
>>test.xls echo !str!
)
pause

First replace double spaces with single spaces, then replace single spaces with {TAB} + single space ({TAB}: tab character)


Thank you!!!
NNNNNN!!!!!!!!!!!!!

Also:

cd.>test.xls

set "str=!str: = !"
set "str=!str: ={TAB} !"


The meaning is not very clear???
Is it this?
set /?
Environment variable substitution has been enhanced as follows:

%PATH:str1=str2%

This will expand the PATH environment variable, replacing each "str1" with "str2" in the expanded result.
To effectively remove all "str1" from the expanded result, "str2" can be empty.
"str1" can start with an asterisk; in this case, "str1" will match from the start of the expanded result to the first occurrence of the remaining part of "str1".



I can directly use tab to separate columns and it's OK!!!!


The 20070131.txt has the following format, directly changing the extension to xls will be as I wish!!!
1 13 45 0 4500 15615
2 13 45 0 4500 15600
3 26 255 0 25500 15601
4 18 81 0 8100 15602
5 29 188 0 18800 15605
6 25 150 0 15000 15606
7 84 333 12 33900 15609
8 20 105 1 10550 15610
9 26 128 0 12800 15611
10 14 149 0 14900 15612
11 5 33 0 3300 15613
12 0 0 0 0 15616
13 48 190 16 19800 15617
14 321 1702 29 171650

[ Last edited by xycoordinate on 2007-2-23 at 10:53 PM ]
Floor 13 Posted 2007-02-24 09:39 ·  中国 宁夏 银川 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Floor 14 Posted 2007-02-24 10:46 ·  中国 广西 贵港 电信
高级用户
★★
学无尽止
Credits 635
Posts 244
Joined 2006-04-15 05:07
20-year member
UID 53857
Gender Male
From 广西贵港
Status Offline
Hehe, this is also simple`````

@echo off
type report.txt echo >>report.xls
学无尽止
Floor 15 Posted 2007-02-24 11:43 ·  中国 安徽 马鞍山 电信
中级用户
★★
Credits 493
Posts 228
Joined 2007-02-16 00:38
19-year member
UID 79596
Gender Male
From 安徽
Status Offline
Originally posted by chainliq at 2007-2-23 09:46 PM:
Hehe, this is also simple.`````

@echo off
type report.txt echo >>report.xls





F:\>type report.txt echo >>report.xls

report.txt


The system cannot find the specified file.
Error occurred while processing: echo


Why does it report an error?????

The report.xls is indeed generated, but the format is incorrect!!!
Forum Jump: