TEXT TO EXCEL(100分)

  • 主题发起人 主题发起人 饿饱鹅
  • 开始时间 开始时间
饿

饿饱鹅

Unregistered / Unconfirmed
GUEST, unregistred user!
大家好,请教个问题:
如何把原DOS下汉字符的规范表格转换成EXCEL格式?
谢谢!
 
什么意思?:(
 
不太明白什么是“原DOS下汉字符的规范表格”,
请给个实例。
 
如:原DOS下CCED,或EDIT,或WPS FOR DOS,等软件做的纯文本格式的表格文档。
 
如果是纯文本,你只要用Excel打开这些文件在另存即可。
如果其中包含表格线
┌┬┬┬┬┬┐
├├├├├├┤
└┴┴┴┴┴┘
象这些东西,没什么好办法,只能导入到Excel之后在手工删除表格线。
 
对的,就是要自动处理这种表格线。
如果用手工的话,至少有种方法比你的好:
1、用WPS2000打开文件(他认识这种表格线)
2、另存为*.rtf格式
3、用word打开
4、copy to excel 中。
如果象你一样直接导入的话,横线还可以删。
竖线的话,你的工作量..... :(
现在我想请一位懂的excel文件格式的高手来指点。
谢谢!!
 
可以通过其他程序做中间转换,如过可以转换成FoxPro的数据库文件或其他便可以转换成
Excel了。
 
我记得 CCED 好象可以把这类规范的表格转存为DBF文件,
好久没用,具体操作记不得了,呵呵
 
liuly,gBlueMan:
 其实在前面我已经讲了用中间程序转换的方法,两位说的也都行。
我向诸位高手请教的是--如何编程来解决此类问题。
 谢谢!
 
先用一个Table调用TXT文件再BATCHMOVE到一个DBF中去,再在EXCEL中打开DBF,再另存为EXCEL
文件格式。
 
1.
程序控制打开txt,读取每一行的文本(因为是有格式的所以可以根据分割符划分字段)
2。
ado +excel
filedName('a').asstring:='your value';
post;
 
cnaoszh:
思路是这样的。问题是怎样用具体的例程来实现!能给一个简单的例程吗?
谢谢!
 
能给出你的文本内容吗?
 
GbluMan:
就是用DOS下CCED,或EDIT,或WPS FOR DOS,等软件做的纯文本格式的表格文档。
并包含汉字符的表格线,如:
┏━━━━━┳━━━━┳━━━━━┳━━━━━━┳━━━━━┳━━━━━━┓
┃ ┃ ┃ ┃ ┃ ┃ ┃
┃ ┃本月 ┃ 累计 ┃ 计划┃完成计划% ┃ 增长% ┃
┃ ┃ ┃ ┃ ┃ ┃ ┃
┣━━━━━╋━━━━╋━━━━━╋━━━━━━╋━━━━━╋━━━━━━┫
┃ ┃ 3835 ┃ 3835 ┃ 6411 ┃ 59.82 ┃ 135.28 ┃
┣━━━━━╋━━━━╋━━━━━╋━━━━━━╋━━━━━╋━━━━━━┫
┃ ┃ 795 ┃ 795 ┃ 1356 ┃ 58.63 ┃ 258.11 ┃
┗━━━━━┻━━━━┻━━━━━┻━━━━━━┻━━━━━┻━━━━━━┛
谢谢
 
在编程实现时
if 汉字= ┏ 或 ━ 或 ┳ 或 ┓ 或 ╋ 或 ┫或才 ┣ 或 ┗ 或┻ 或┛则 替换为空串;
if 汉字= ┃ 则替换为 Tab 字符;
你没有公布email, 所有贴出来了,若有email,可以给你另发个exporte控件过来
下面你自己看:
调用方法:
uses Xls
procedure TfrmMain.btnMakeXlsClick(Sender: TObject);
Var
FileHandle : Integer;
FileNameStr : String;
begin
FileNameStr := ExtractFilePath(Application.ExeName) + 'Delphi.xls';
FileHandle := Xls_Create(FileNameStr);
Xls_SetFormat(FileHandle,'yyyy-mm-dd hh:mm:ss');
// user defined additional format 21
// Xls_SetProtection(FileHandle, True);
// protect entire worksheet
// Xls_SetAttributeByte1(True, True);
// Hidden, Locked -> only works if sheet is protected
// some titles in the first column
Xls_SetString(FileHandle, 0, 0, 'A random string :)');
Xls_SetString(FileHandle, 1, 0, 'A lucky integer');
Xls_SetString(FileHandle, 2, 0, 'Pi (in a box) - yuk :)');
Xls_SetString(FileHandle, 3, 0, 'Today :');
Xls_SetString(FileHandle, 0, 1, 'Hello World');
// couldn't resist the temptation
Xls_SetInteger(FileHandle, 1, 1, 13);
// integer : lucky 13
Xls_SetAttributeByte3(False, True, True, True, True, 0);
// show all borders
Xls_SetDouble(FileHandle, 2, 1, Pi);
// pi =do
uble = 3.141572...
Xls_SetAttributeByte3(False, False, False, False, False, 0);
// reset
Xls_SetAttributeByte2(0, 21);
// use user defined format 21
Xls_SetDouble(FileHandle, 3, 1, Now);
// datetime
Xls_SetAttributeByte2(0, 0);
// reset
Xls_SetColWidth(FileHandle, 0, 17);
// set column width
Xls_SetColWidth(FileHandle, 1, 25);
// set column width
Xls_Close (FileHandle);
// closing stuff
ShowMessage('done');
end;

下面是xls单元
unit Xls;
{Purpose : Create binary Micro$oft Excel files}
{Author : Eddy Sterckx - eddysterckx@hotmail.com
Last revision date : 25/02/2000
Status : Freeware
Delphi rewrite from original C-code by Jason Morrill
and the xls-fileformat description from www.wotsit.org}
{Tested with Delphi 1 and 5 ;
Excel 5.0 and Office 97}
{Should work with other Delphi/Excel versions}
{Excel uses a file format called BIFF (Binary File Format).
There are many types of BIFF records. Each has a 4 byte header.
The first two bytes are an opcode that specifies the record type.
The second two bytes specify record length.
The rest of the record is the data itself
Word-values are stored in byte-reversed form (lsbf - least significant byte first).
Double-values (8 byte floats) are stored in IEEE format}
{
Usage example :
(Calling form Uses : add Xls)
Var
FileHandle : Integer;
begin
FileHandle := Xls_Create('c:/test/delphi.xls');
Xls_SetFormat(FileHandle,'yyyy-mm-dd hh:mm:ss');
// user defined additional format 21
// Xls_SetProtection(FileHandle, True);
// protect entire worksheet
// Xls_SetAttributeByte1(True, True);
// Hidden, Locked -> only works if sheet is protected
// some titles in the first column
Xls_SetString(FileHandle, 0, 0, 'A random string :)');
Xls_SetString(FileHandle, 1, 0, 'A lucky integer');
Xls_SetString(FileHandle, 2, 0, 'Pi (in a box) - yuk :)');
Xls_SetString(FileHandle, 3, 0, 'Today :');
Xls_SetString(FileHandle, 0, 1, 'Hello World');
// couldn't resist the temptation
Xls_SetInteger(FileHandle, 1, 1, 13);
// integer : lucky 13
Xls_SetAttributeByte3(False, True, True, True, True, 0);
// show all borders
Xls_SetDouble(FileHandle, 2, 1, Pi);
// pi =do
uble = 3.141572...
Xls_SetAttributeByte3(False, False, False, False, False, 0);
// reset
Xls_SetAttributeByte2(0, 21);
// use user defined format 21
Xls_SetDouble(FileHandle, 3, 1, Now);
// datetime
Xls_SetAttributeByte2(0, 0);
// reset
Xls_SetColWidth(FileHandle, 0, 17);
// set column width
Xls_SetColWidth(FileHandle, 1, 25);
// set column width
Xls_Close (FileHandle);
// closing stuff
end;
}
interface
uses
SysUtils;
Function Xls_Create (FileName : String) : Integer;
Procedure Xls_Close (FileHandle : Integer);
Procedure Xls_SetAttributeByte1(Hidden, Locked : Boolean);
Procedure Xls_SetAttributeByte2(FontNumber, FormatCode : Byte);
Procedure Xls_SetAttributeByte3(Shaded, BottomBorder, TopBorder, RightBorder, LeftBorder : Boolean ;
AlignMent : Byte);
Procedure Xls_SetBof(FileHandle : Integer ;
Version : Word ;
DocType : Word);
Procedure Xls_SetEof(FileHandle : Integer);
Procedure BiffRecordHeader(FileHandle : Integer ;
OpCode : Word ;
Length : Word);
Procedure BiffRecordValueWord(FileHandle : Integer ;
Value : Word);
Procedure BiffRecordValueByte(FileHandle : Integer ;
Value : Byte);
Procedure BiffRecordValueString(FileHandle : Integer ;
Value : String);
Procedure BiffRecordValueDouble(FileHandle : Integer ;
Value :do
uble);
Procedure Xls_SetInteger(FileHandle : Integer ;
Row, Column : Word ;
Value : Word);
Procedure Xls_SetString(FileHandle : Integer ;
Row, Column : Word ;
Value : String);
Procedure Xls_SetDouble(FileHandle : Integer ;
Row, Column : Word ;
Value :do
uble);
Procedure Xls_SetFormatCount(FileHandle : Integer ;
Count : Integer);
Procedure Xls_SetFormat(FileHandle : Integer ;
FormatString : String);
Procedure Xls_SetColWidth(FileHandle : Integer ;
Column: Integer ;
Width : Word);
Procedure Xls_SetProtection(FileHandle : Integer ;
Protect : Boolean);
Implementation
Var
AttributeByte1 : Byte;
AttributeByte2 : Byte;
AttributeByte3 : Byte;
{**************************************************}
Function Xls_Create (FileName : String) : Integer;
{**************************************************}
Var
FileHandle : Integer;
begin
FileHandle := FileCreate(FileName);
{result = filehandle}
{Create bof record}
Xls_SetBof(FileHandle, 5, 16);
{version = 5 (doesn't seem to matter) ;
doctype 16 = worksheet}
{Set initial attributebyte-values}
Xls_SetAttributeByte1(False, False);
{Hidden, Locked -> only works if sheet is protected too}
Xls_SetAttributeByte2(0, 0);
{fontnumber 0 = default, formatcode 0 = general}
{Shaded, BottomBorder, TopBorder, RightBorder, LeftBorder, Alignment
Alignment 3 = Right ;
Alignment 2 = Center ;
Alignment 1 = Left ;
Alignment 0 = general}
Xls_SetAttributeByte3(False, False, False, False, False, 0);
{set initial standard Excel formats}
Xls_SetFormatCount(FileHandle,21);
{21 standard formats (0-based)}
Xls_SetFormat(FileHandle,'General');
Xls_SetFormat(FileHandle,'0');
Xls_SetFormat(FileHandle,'0.00');
Xls_SetFormat(FileHandle,'#,##0');
Xls_SetFormat(FileHandle,'#,##0.00');
Xls_SetFormat(FileHandle,'/"$/"#,##0_);//(/"$/"#,##0//)');
Xls_SetFormat(FileHandle,'/"$/"#,##0_);[Red]//(/"$/"#,##0//)');
Xls_SetFormat(FileHandle,'/"$/"#,##0.00_);//(/"$/"#,##0.00//)');
Xls_SetFormat(FileHandle,'/"$/"#,##0.00_);[Red]//(/"$/"#,##0.00//)');
Xls_SetFormat(FileHandle,'0%');
Xls_SetFormat(FileHandle,'0.00%');
Xls_SetFormat(FileHandle,'0.00E+00');
Xls_SetFormat(FileHandle,'m/d/yy');
Xls_SetFormat(FileHandle,'d//-mmm//-yy');
Xls_SetFormat(FileHandle,'d//-mmm');
Xls_SetFormat(FileHandle,'mmm//-yy');
Xls_SetFormat(FileHandle,'h:mm// AM/PM');
Xls_SetFormat(FileHandle,'h:mm:ss// AM/PM');
Xls_SetFormat(FileHandle,'h:mm');
Xls_SetFormat(FileHandle,'h:mm:ss');
Xls_SetFormat(FileHandle,'m/d/yy// h:mm');
Result := FileHandle;
{return filehandle}
end;

{*******************************************}
Procedure Xls_Close (FileHandle : Integer);
{*******************************************}
begin
Xls_SetEof(FileHandle);
{create eof-record}
FileClose(FileHandle);
{free filehandle}
end;

{*************************************************************}
Procedure Xls_SetAttributeByte1(Hidden, Locked : Boolean);
{*************************************************************}
begin
AttributeByte1 := 0;
If Hidden = True then
AttributeByte1 := AttributeByte1 + 128;
{set bit 7}
If Locked = True then
AttributeByte1 := AttributeByte1 + 64;
{set bit 6}
{Bits 5->0 are reserved}
end;

{*****************************************************************}
Procedure Xls_SetAttributeByte2(FontNumber, FormatCode : Byte);
{*****************************************************************}
begin
AttributeByte2 := (FontNumber SHL 6) + FormatCode;
end;

{**************************************************************************************************************************}
Procedure Xls_SetAttributeByte3(Shaded, BottomBorder, TopBorder, RightBorder, LeftBorder : Boolean ;
AlignMent : Byte);
{**************************************************************************************************************************}
begin
AttributeByte3 := Alignment;
{set bits 1-0}
If Shaded = True then
{set bit 7}
AttributeByte3 := AttributeByte3 + 128;
If BottomBorder = True then
{set bit 6}
AttributeByte3 := AttributeByte3 + 64;
If TopBorder = True then
{set bit 5}
AttributeByte3 := AttributeByte3 + 32;
If RightBorder = True then
{set bit 4}
AttributeByte3 := AttributeByte3 + 16;
If LeftBorder = True then
{set bit 3}
AttributeByte3 := AttributeByte3 + 8;
end;

{**************************************************************************}
Procedure BiffRecordHeader(FileHandle : Integer ;
OpCode : Word ;
Length : Word);
{**************************************************************************}
begin
{Write the standard recordheader to the file}
BiffRecordValueWord(FileHandle, OpCode);
BiffRecordValueWord(FileHandle, Length);
end;

{**************************************************************************}
Procedure BiffRecordValueWord(FileHandle : Integer ;
Value : Word);
{**************************************************************************}
Var
Buffer : Array[0..1] of Char;
begin
{Write a word value to the file}
Buffer[0] := Chr(Lo(Value));
{lsbf}
Buffer[1] := Chr(Hi(Value));
FileWrite(FileHandle, Buffer, 2);
end;

{**************************************************************************}
Procedure BiffRecordValueString(FileHandle : Integer ;
Value : String);
{**************************************************************************}
Var
Buffer : Array[0..255] of Char;
i : Integer;
begin
{Write a string to the file}
For i := 1 To Length(Value)do
Buffer[i-1] := Value;
FileWrite(FileHandle, Buffer, Length(Value));
end;

{**************************************************************************}
Procedure BiffRecordValueDouble(FileHandle : Integer ;
Value :do
uble);
{**************************************************************************}
Var
WorkDouble :do
uble;
ByteArray : Array[0..7] of Byte ABSOLUTE WorkDouble;
{point to same memory address as workdouble}
Buffer : Array[0..7] of Char;
i : Integer;
begin
{Write ado
uble to the file}
WorkDouble := Value;
For i := 0 to 7do
{Delphi uses IEEEdo
uble format internally -> simple memory copy is ok}
Buffer := Chr(ByteArray);
FileWrite(FileHandle, Buffer, 8);
end;

{**************************************************************************}
Procedure BiffRecordValueByte(FileHandle : Integer ;
Value : Byte);
{**************************************************************************}
Var
Buffer : Array[0..0] of Char;
{weird code requirement fulfilled :) }
begin
{Write a byte to the file}
Buffer[0] := Chr(Value);
FileWrite(FileHandle, Buffer, 1);
end;

{**************************************************************************}
Procedure Xls_SetBof(FileHandle : Integer ;
Version : Word ;
DocType : Word);
{**************************************************************************}
begin
{Write a begin
ning of file (bof) record}
BiffRecordHeader(FileHandle, 9, 4);
{9 = opcode bof, length of data}
BiffRecordValueWord(FileHandle, Version);
BiffRecordValueWord(FileHandle,do
cType);
end;

{**************************************************************************}
Procedure Xls_SetEof(FileHandle : Integer);
{**************************************************************************}
begin
{Write an end of file (eof) record}
BiffRecordHeader(FileHandle, 10, 0);
{10 = opcode eof}
end;

{************************************************************************************}
Procedure Xls_SetInteger(FileHandle : Integer ;
Row, Column : Word ;
Value : Word);
{************************************************************************************}
begin
{Write an integer value to row, column (0-based)
Caveat : only for unsigned, possitive integers that fit in a word : 0-65535 (or whatever)}
BiffRecordHeader(FileHandle,2,9);
{2 = Opcode Integer}
BiffRecordValueWord(FileHandle,Row);
BiffRecordValueWord(FileHandle,Column);
BiffRecordValueByte(FileHandle,AttributeByte1);
{attributes define the look of the cell}
BiffRecordValueByte(FileHandle,AttributeByte2);
BiffRecordValueByte(FileHandle,AttributeByte3);
BiffRecordValueWord(FileHandle,Value);
end;

{*************************************************************************************}
Procedure Xls_SetString(FileHandle : Integer ;
Row, Column : Word ;
Value : String);
{*************************************************************************************}
begin
{Write a string to row, column}
BiffRecordHeader(FileHandle,4,8 + Length(Value));
{4 = Opcode String/Label}
BiffRecordValueWord(FileHandle,Row);
BiffRecordValueWord(FileHandle,Column);
BiffRecordValueByte(FileHandle,AttributeByte1);
{attributes define the look of the cell}
BiffRecordValueByte(FileHandle,AttributeByte2);
BiffRecordValueByte(FileHandle,AttributeByte3);
BiffRecordValueByte(FileHandle,Length(Value));
BiffRecordValueString(FileHandle,Value);
end;

{************************************************************************************}
Procedure Xls_SetDouble(FileHandle : Integer ;
Row, Column : Word ;
Value :do
uble);
{************************************************************************************}
begin
{Write ado
uble value to row, column}
{Caveat : Dates are justdo
ubles with formatting applied}
BiffRecordHeader(FileHandle, 3, 15);
{3 = Opcodedo
uble}
BiffRecordValueWord(FileHandle, Row);
BiffRecordValueWord(FileHandle, Column);
BiffRecordValueByte(FileHandle, AttributeByte1);
{attributes define the look of the cell}
BiffRecordValueByte(FileHandle, AttributeByte2);
BiffRecordValueByte(FileHandle, AttributeByte3);
BiffRecordValueDouble(FileHandle, Value);
end;

{*************************************************************************}
Procedure Xls_SetFormatCount(FileHandle : Integer ;
Count : Integer);
{*************************************************************************}
begin
{Define the number of standard formats : (21)}
BiffRecordHeader(FileHandle, 31, 2);
{31 = Opcode Formatcount}
BiffRecordValueWord(FileHandle, Count);
end;

{*************************************************************************}
Procedure Xls_SetFormat(FileHandle : Integer ;
FormatString : String);
{*************************************************************************}
begin
{Add an additional format-string to the internal formatlist}
BiffRecordHeader(FileHandle, 30, Length(FormatString) + 1);
{30 = Opcode Format}
BiffRecordValueByte(FileHandle, Length(FormatString));
BiffRecordValueString(FileHandle, FormatString);
end;

{*********************************************************************************}
Procedure Xls_SetColWidth(FileHandle : Integer ;
Column: Integer ;
Width : Word);
{*********************************************************************************}
begin
BiffRecordHeader(FileHandle, 36, 4);
{36 = Opcode colwidth}
BiffRecordValueByte(FileHandle, Column);
{starting column}
BiffRecordValueByte(FileHandle, Column);
{ending column}
BiffRecordValueWord(FileHandle, Width * 256);
{width is defined in 1/256 of a character}
end;

{*********************************************************************}
Procedure Xls_SetProtection(FileHandle : Integer ;
Protect : Boolean);
{*********************************************************************}
begin
{Set general protection setting of worksheet}
BiffRecordHeader(FileHandle, 18, 2);
{18 = opcode protected worksheet}
If Protect = True then
BiffRecordValueWord(FileHandle, 1)
else
BiffRecordValueWord(FileHandle, 0);
end;


end.



 
多人接受答案了。
 

Similar threads

回复
0
查看
1K
不得闲
D
回复
0
查看
909
DelphiTeacher的专栏
D
D
回复
0
查看
767
DelphiTeacher的专栏
D
D
回复
0
查看
704
DelphiTeacher的专栏
D
D
回复
0
查看
825
DelphiTeacher的专栏
D
后退
顶部