線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:3429
推到 Plurk!
推到 Facebook!

用BCB Read Word表格

缺席
sb055665
一般會員


發表:24
回覆:25
積分:14
註冊:2006-07-12

發送簡訊給我
#1 引用回覆 回覆 發表時間:2006-11-17 14:39:22 IP:211.20.xxx.xxx 未訂閱
如題,找了相關的文章 試過bcb提供的wordapplication,也試過ole
可是都沒試出來,想請問有經驗的大大,分享一下方法與方向
謝謝!
sb055665
一般會員


發表:24
回覆:25
積分:14
註冊:2006-07-12

發送簡訊給我
#2 引用回覆 回覆 發表時間:2006-11-17 15:07:37 IP:211.20.xxx.xxx 未訂閱
附上一段code,我的Office是2003的
Variant WordApp, WordDocs, WordDoc,
WordSelection, WordChange,WordTables,WordTable;
if(OpenDialog1->Execute())
{
//init
WordApp=Variant::CreateObject("Word.Application");
WordApp.OlePropertySet("Visible", true);
WordDocs=WordApp.OlePropertyGet("Documents");
//open file
WordDoc=WordDocs.OleFunction("Open", OpenDialog1->FileName.c_str() );

//get page
int j;
j=WordDoc.OleFunction("ComputeStatistics",2);
Label2->Caption="文件" WordDoc.OlePropertyGet("Name") "頁數是" IntToStr(j);
//Get Table
WordTables=WordApp.OlePropertyGet("Tables"); //死在這裡
//Variant wordTables = WordApp.OlePropertyGet("Tables");
//WordTable = WordTables.OleFunction("Item",1).OleFunction("Cell",1);
//Variant wordCell = wordTable1.OleFunction("Cell",(Variant) 2, (Variant) 3 );
//Variant wordRange = wordCell.OlePropertyGet("Range");
//wordRange.OlePropertySet("Text", (Variant) "We are at 2/3");


WordDoc.OleFunction("Close");
}
sb055665
一般會員


發表:24
回覆:25
積分:14
註冊:2006-07-12

發送簡訊給我
#3 引用回覆 回覆 發表時間:2006-11-21 13:35:08 IP:211.20.xxx.xxx 未訂閱
已找到相關讀word的方法了,在此附上code給想了解的人一個方向,不過我遇到了讀資料量大的word時會當掉到,可否請高手告知要如何才能讓它不要當掉
AnsiString text,text1;

if( openDialog->Execute())
{
// used for the file name
OleVariant fileName;
fileName = openDialog->FileName;
AnsiString file = openDialog->FileName;
Variant my_word;
Variant my_docs;
my_word = Variant::CreateObject( "word.application" );
my_word.OlePropertySet( "Visible", (Variant) true );
my_docs = my_word.OlePropertyGet( "documents" );

Variant wordActiveDocument = my_docs.OleFunction( "open", fileName );
//select all
Variant WordDocs;
Variant WordSelection;
Variant WordChange;
Variant WordReplace;
TVariant FindText,OleFalse, OleTrue, Replace, ReplaceText, Wrap;
wordActiveDocument=my_word.OlePropertyGet("ActiveDocument");
wordActiveDocument.OleProcedure("Select");
WordSelection=my_word.OlePropertyGet("Selection");
//set font size
WordChange=WordSelection.OlePropertyGet("Font");
WordChange.OlePropertySet("Size",8);

Variant wordTables = wordActiveDocument.OlePropertyGet( "Tables" );

FILE *fp;
fp=fopen("OutPutText.txt","w ");
//等待時間,根據資料大小而定
int waitTime;
waitTime=ComboBox1->Text.ToInt();
waitTime=waitTime*60*1000;
Sleep(waitTime);

long Table_count = wordTables.OlePropertyGet( "count");
int tableCount, rowsCount, columnsCount;
try
{
for( tableCount=1; tableCount<=Table_count; tableCount )
{
Variant wordTable1 = wordTables.OleFunction( "Item", (Variant) tableCount );//item
Variant tableRows = wordTable1.OlePropertyGet( "Rows" ); //rows
Variant tableCols = wordTable1.OlePropertyGet( "Columns" ); //columns
long row_count, col_count;
row_count = tableRows.OlePropertyGet( "count" ); //計算row count
col_count = tableCols.OlePropertyGet( "count" ); //計算colum count
for( rowsCount=1; rowsCount<=row_count; rowsCount )
{
Variant tableRow = tableRows.OleFunction( "Item", (Variant) rowsCount );
tableRow.OleProcedure( "Select" );
Variant rowSelection = my_word.OlePropertyGet( "Selection");
Variant rowColumns = rowSelection.OlePropertyGet( "Columns" );
Variant selectionRows = rowSelection.OlePropertyGet( "Rows" );
long rowColumn = rowColumns.OlePropertyGet( "count" ); //計算選取幾個欄位
for( columnsCount=1; columnsCount<=rowColumn; columnsCount )
{
Variant rowCells = tableRow.OlePropertyGet( "cells" );
Variant wordCell = wordTable1.OleFunction( "Cell", (Variant) rowsCount, (Variant) columnsCount );
Variant cellRange = wordCell.OlePropertyGet( "Range" );
Variant rangeWords = cellRange.OlePropertyGet( "Words" );
long words_count = rangeWords.OlePropertyGet( "count" );

for( int Word=1; Word<=words_count; Word )
{
text=(AnsiString)(rangeWords.OleFunction("Item",(Variant)Word));
rangeWords.OleFunction("Item",(Variant)Word).Clear() ;
int pos=text.AnsiPos("\r\a");
if(pos!=0) text.Delete(pos,4);
text1 =text;
}
// Output The Content to the datafile
fputs(text1.c_str(),fp);
fputs("\t\t",fp);
text1="";
text="";
}
fputs("\n",fp);
Sleep(200);
}
}
fclose(fp);
my_word.OleFunction( "Quit" );
}
catch( Exception &e )
{
ShowMessage( e.Message "\nType: " __ThrowExceptionName()
"\nFile: " __ThrowFileName()
"\nLine: " AnsiString(__ThrowLineNumber()) );
}
}

系統時間:2024-05-06 22:18:33
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!