html export to excel,Delphi, Export HTML table to Excel

All I want to do is to implement "Export to excel" option of a classical webbrowser, to Delphi2007 commands...... When I am using this option from a webbrowser to export a 12000 rows table it takes less than a minute to export the table from any web browser from windows. Trying to implement this in Delphi using 2D Array it takes 10 minutes... Trying to implement the export with parsing technique (Stringlists, strings, Pos(tr), pos (td) & some other string functions) it takes a long... Hence, which are the commands of a webbrowser to export an html table to excel that I have to convert them to Delphi? Should I use javascript inside Delphi? Should I use pointers? Should I use HTML entities? xml?...Any ideas? Thank you in advance.

2D ARRAY

Excel:= CreateOleObject('Excel.Application');

ovTable := WebBrowser1.OleObject.Document.all.tags('TABLE').item(0);

arrayn:=VarArrayCreate([1, ovTable.Rows.Length, 1, ovTable.Rows.Item(1).Cells.Length], varvariant);

for i:=0 to (ovTable.Rows.Length - 1) do

begin

for j := 0 to (ovTable.Rows.Item(i).Cells.Length - 1) do

Begin

arrayn[i+1, j+1]:=ovTable.Rows.Item(i).Cells.Item(j).InnerText;

Application.ProcessMessages;

end;end;

WS.range[ws.cells[1, 1], ws.cells[ovTable.Rows.Length, ovTable.Rows.Item(1).Cells.Length]].value:=arrayn;

Excel.WorkBooks[1].SaveAs(directorylistbox1.Directory+'\'+'test.xlsx');

WS := Excel.WorkBooks.close;

Excel.quit;

Excel:=unassigned;

HTML PARSING

function HTMLCleanUp(L : string) : string;

const

CSVTempSeparator = #255; //replaced by a comma

CRLF = #13#10;

var

P1,P2 : integer;

begin

P1 := Pos('

while (P1>0) do //WHILE1

begin

P2 := Pos('>',L);

if (P2>0)

then Begin Delete(L,P1,P2-P1+1); end;

P1 := Pos('

end; //WHILE1

L:=StringReplace(L,' ','-',[rfReplaceAll]);

L:=StringReplace(L,'-01','',[rfReplaceAll]);

L:=StringReplace(L,'-02','',[rfReplaceAll]);

L:=StringReplace(L,'-03','',[rfReplaceAll]);

Result := Trim(L);

end;

function HTMLTableToCSV(HTML,CSV : TStringList) : boolean;

const

CRLF = #13#10;

CSVTempSeparator = #9;

var

P1,P2,P3,P4, p5, P6, p11, p22 : integer;

S,TmpStr,CSVStr : string;

begin

Result := True;

S := Trim(StringReplace(HTML.Text,CRLF,'',[rfReplaceAll]));

P1 := PosEx('

FIRST ROW

CSVStr := '';

while (P1>0) do //while1

begin

P2 := PosEx('

if (P2>0) //if1

then begin

TmpStr := Copy(S,P1,P2-P1+1);

//Delete(S,P1,P2-P1+1);

CSVStr := ''; p11:=1;p22:=1;

P11 := PosEx('

while (P11>0) do //while2

begin

P22 := PosEx('

if (P22>0) //if2

then begin

CSVStr :=

//CSVStr+Trim(Copy(TmpStr,P1+4,P2-P1-4));//+CSVTempSeparator;

CSVStr+Trim(Copy(TmpStr,P11,P22-P11))+CSVTempSeparator;

//Delete(TmpStr,P1,P2-P1+1);

end //if2

else begin

Result := False;

Exit;

end; //if2

P11 := PoseX('

end; //while2

P11 := PosEx('

while (P11>0) do //while2

begin

P22 := PosEx('

if (P22>0) //if2

then begin

CSVStr :=

//CSVStr+Trim(Copy(TmpStr,P1+4,P2-P1-4));//+CSVTempSeparator;

CSVStr+Trim(Copy(TmpStr,P11,P22-P11))+CSVTempSeparator;

//Delete(TmpStr,P1,P2-P1+1);

end //if2

else begin

Result := False;

Exit;

end; //if2

P11 := PosEx('

end; //while2

end //if1

else begin

Result:=false;

exit;

end; //if1

CSV.Add(HTMLCleanUp(CSVStr));

P1 := PosEx('

end; //while1

end;

procedure TForm11.Button1Click(Sender: TObject);

const

xlExcel7 = $00000027;

TmpFileName='c:\test\Test.txt';

VAR

Excel: Olevariant;

HTMLStrList,CSVSTRList : TStringList;

begin

HTMLStrList := TStringList.Create;

try

HTMLStrList.LoadFromFile('C:\test\TestTable1.htm');

CSVSTRList := TStringList.Create;

try

if HTMLTableToCSV(HTMLStrList,CSVSTRList)

then Begin

CSVSTRList.SaveToFile(TmpFileName);

Excel:= CreateOleObject('Excel.Application');

Excel.WorkBooks.opentext(TmpFileName);//OPEN TXT WITH EXCEL

Excel.DisplayAlerts := False;

Excel.WorkBooks[1].SaveAs('c:\test\Nisa.xls', xlExcel7);//SAVE TAB DELIMITED TEXT FILE

Excel.WorkBooks[1].close;

Excel.quit;

Excel:=unassigned;

End

else ShowMessage('Error converting HTML table to CSV');

finally

CSVSTRList.Free;

end;

finally

HTMLStrList.Free;

DeleteFile(TmpFileName);

end;

end;

procedure TForm11.FormCreate(Sender: TObject);

begin

webBrowser1.Navigate('http://samples.msdn.microsoft.com/workshop/samples/author/tables/HTML_ Table.htm');

end;

procedure TForm11.WebBrowser1DocumentComplete(ASender: TObject;

const pDisp: IDispatch; var URL: OleVariant);

var

Document: IHtmlDocument2;

CurWebrowser : IWebBrowser;

TopWebBrowser: IWebBrowser;

WindowName : string;

begin

CurWebrowser := pDisp as IWebBrowser;

TopWebBrowser := (ASender as TWebBrowser).DefaultInterface;

if CurWebrowser=TopWebBrowser then

begin

document := webbrowser1.document as IHtmlDocument2;

memo3.lines.add(trim(document.body.innerhtml)); // to get html

ShowMessage('Document is complete.')

end;

end;

end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值