操作EXCEL的时候,怎样控制当前的表格以及当前表格的记录个数,并访问其中的数据?(50分)

  • 主题发起人 主题发起人 ydiandian
  • 开始时间 开始时间
Y

ydiandian

Unregistered / Unconfirmed
GUEST, unregistred user!
操作EXCEL的时候,怎样控制当前的表格以及当前表格的记录个数,并访问其中的数据?
 
有多少条记录不是你写进去的吗? 访问其中的数据cells[i,j]
 
是这样的
MSExcel := CreateOleObject('Excel.Application');
MSExcel.WorkBooks.Open(Edit1.Text);//创建一个OleObject
打开EXCEL文件后,我要读出其中所有的行,然后把数据写入一个DBF文件中
,由于不知道EXCEL中记录数的哪个属性,所以只能最加指定的行!
明白了吗?
 
你试试 Worksheet对象的lines 方法
 
这些可能会对你有帮助

dispinterface Worksheet {
properties:
[id(0x00000318), helpcontext(0x00010318)]
VARIANT AutoFilterMode;
[id(0x0000042d), helpcontext(0x0001042d)]
VARIANT CircularReference;
[id(0x00000315), helpcontext(0x00010315)]
VARIANT ConsolidationFunction;
[id(0x00000316), helpcontext(0x00010316)]
VARIANT ConsolidationOptions;
[id(0x00000317), helpcontext(0x00010317)]
VARIANT ConsolidationSources;
[id(0x00000095), helpcontext(0x00010095)]
VARIANT Creator;
[id(0x00000283), helpcontext(0x00010283)]
VARIANT DisplayAutomaticPageBreaks;
[id(0x00000484), helpcontext(0x00010484)]
VARIANT EnableAutoFilter;
[id(0x00000485), helpcontext(0x00010485)]
VARIANT EnableOutlining;
[id(0x00000486), helpcontext(0x00010486)]
VARIANT EnablePivotTable;
[id(0x00000320), helpcontext(0x00010320)]
VARIANT FilterMode;
[id(0x000001e6), helpcontext(0x000101e6)]
VARIANT Index;
[id(0x0000006e), helpcontext(0x0001006e)]
VARIANT Name;
[id(0x000001f6), helpcontext(0x000101f6)]
VARIANT Next;
[id(0x00000271), helpcontext(0x00010271)]
VARIANT OnCalculate;
[id(0x00000275), helpcontext(0x00010275)]
VARIANT OnData;
[id(0x00000274), helpcontext(0x00010274)]
VARIANT OnDoubleClick;
[id(0x00000273), helpcontext(0x00010273)]
VARIANT OnEntry;
[id(0x00000407), helpcontext(0x00010407)]
VARIANT OnSheetActivate;
[id(0x00000439), helpcontext(0x00010439)]
VARIANT OnSheetDeactivate;
[id(0x00000066), helpcontext(0x00010066)]
VARIANT Outline;
[id(0x000003e6), helpcontext(0x000103e6)]
VARIANT PageSetup;
[id(0x00000096), helpcontext(0x00010096)]
VARIANT Parent;
[id(0x000001f7), helpcontext(0x000101f7)]
VARIANT Previous;
[id(0x00000124), helpcontext(0x00010124)]
VARIANT ProtectContents;
[id(0x00000125), helpcontext(0x00010125)]
VARIANT ProtectDrawingObjects;
[id(0x00000487), helpcontext(0x00010487)]
VARIANT ProtectionMode;
[id(0x00000126), helpcontext(0x00010126)]
VARIANT ProtectScenarios;
[id(0x00000197), helpcontext(0x00010197)]
VARIANT StandardHeight;
[id(0x00000198), helpcontext(0x00010198)]
VARIANT StandardWidth;
[id(0x00000191), helpcontext(0x00010191)]
VARIANT TransitionExpEval;
[id(0x00000192), helpcontext(0x00010192)]
VARIANT TransitionFormEntry;
[id(0x0000006c), helpcontext(0x0001006c)]
VARIANT Type;
[id(0x0000019c), helpcontext(0x0001019c)]
VARIANT UsedRange;
[id(0x0000022e), helpcontext(0x0001022e)]
VARIANT Visible;
methods:
[id(0x00000130), helpcontext(0x00010130)]
VARIANT Activate();
[id(0x00000094), helpcontext(0x00010094)]
VARIANT Application();
[id(0x000002f8), helpcontext(0x000102f8)]
VARIANT Arcs(VARIANT Index);
[id(0x0000022d), helpcontext(0x0001022d)]
VARIANT Buttons(VARIANT Index);
[id(0x00000117), helpcontext(0x00010117)]
VARIANT Calculate();
[id(0x000000ee), helpcontext(0x000100ee)]
VARIANT Cells(
VARIANT RowIndex,
VARIANT ColumnIndex);
[id(0x00000424), helpcontext(0x00010424)]
VARIANT ChartObjects(VARIANT Index);
[id(0x00000338), helpcontext(0x00010338)]
VARIANT CheckBoxes(VARIANT Index);
[id(0x000001f9), helpcontext(0x000101f9)]
VARIANT CheckSpelling(
VARIANT CustomDictionary,
VARIANT IgnoreUppercase,
VARIANT AlwaysSuggest);
[id(0x000003ca), helpcontext(0x000103ca)]
VARIANT ClearArrows();
[id(0x000000f1), helpcontext(0x000100f1)]
VARIANT Columns(VARIANT Index);
[id(0x00000227), helpcontext(0x00010227)]
VARIANT Copy(
VARIANT Before,
VARIANT After);
[id(0x00000075), helpcontext(0x00010075)]
VARIANT Delete();
[id(0x00000058), helpcontext(0x00010058)]
VARIANT DrawingObjects(VARIANT Index);
[id(0x00000304), helpcontext(0x00010304)]
VARIANT Drawings(VARIANT Index);
[id(0x00000344), helpcontext(0x00010344)]
VARIANT DropDowns(VARIANT Index);
[id(0x00000001), helpcontext(0x00010001)]
VARIANT _Evaluate(VARIANT Name);
[id(0xfffffffb), helpcontext(0x00010001)]
VARIANT Evaluate(VARIANT Name);
[id(0x00000342), helpcontext(0x00010342)]
VARIANT GroupBoxes(VARIANT Index);
[id(0x00000459), helpcontext(0x00010459)]
VARIANT GroupObjects(VARIANT Index);
[id(0x00000349), helpcontext(0x00010349)]
VARIANT Labels(VARIANT Index);
[id(0x000002ff), helpcontext(0x000102ff)]
VARIANT Lines(VARIANT Index);
[id(0x00000340), helpcontext(0x00010340)]
VARIANT ListBoxes(VARIANT Index);
[id(0x0000027d), helpcontext(0x0001027d)]
VARIANT Move(
VARIANT Before,
VARIANT After);
[id(0x000001ba), helpcontext(0x000101ba)]
VARIANT Names(
VARIANT Index,
VARIANT IndexLocal,
VARIANT RefersTo);
[id(0x0000031f), helpcontext(0x0001031f)]
VARIANT OLEObjects(VARIANT Index);
[id(0x0000033a), helpcontext(0x0001033a)]
VARIANT OptionButtons(VARIANT Index);
[id(0x00000321), helpcontext(0x00010321)]
VARIANT Ovals(VARIANT Index);
[id(0x000000d3), helpcontext(0x000100d3)]
VARIANT Paste(
VARIANT Destination,
VARIANT Link);
[id(0x00000403), helpcontext(0x00010403)]
VARIANT PasteSpecial(
VARIANT Format,
VARIANT Link,
VARIANT DisplayAsIcon,
VARIANT IconFileName,
VARIANT IconIndex,
VARIANT IconLabel);
[id(0x00000303), helpcontext(0x00010303)]
VARIANT Pictures(VARIANT Index);
[id(0x000002b2), helpcontext(0x000102b2)]
VARIANT PivotTables(VARIANT Index);
[id(0x000002ac), helpcontext(0x000102ac)]
VARIANT PivotTableWizard(
VARIANT SourceType,
VARIANT SourceData,
VARIANT TableDestination,
VARIANT TableName,
VARIANT RowGrand,
VARIANT ColumnGrand,
VARIANT SaveData,
VARIANT HasAutoFormat,
VARIANT AutoPage,
VARIANT Reserved);
[id(0x00000389), helpcontext(0x00010389)]
VARIANT PrintOut(
VARIANT From,
VARIANT To,
VARIANT Copies,
VARIANT Preview,
VARIANT ActivePrinter,
VARIANT PrintToFile,
VARIANT Collate);
[id(0x00000119), helpcontext(0x00010119)]
VARIANT PrintPreview();
[id(0x0000011a), helpcontext(0x0001011a)]
VARIANT Protect(
VARIANT Password,
VARIANT DrawingObjects,
VARIANT Contents,
VARIANT Scenarios,
VARIANT UserInterfaceOnly);
[id(0x000000c5), helpcontext(0x000100c5)]
VARIANT Range(
VARIANT Cell1,
VARIANT Cell2);
[id(0x00000306), helpcontext(0x00010306)]
VARIANT Rectangles(VARIANT Index);
[id(0x00000102), helpcontext(0x00010102)]
VARIANT Rows(VARIANT Index);
[id(0x0000011c), helpcontext(0x0001011c)]
VARIANT SaveAs(
VARIANT Filename,
VARIANT FileFormat,
VARIANT Password,
VARIANT WriteResPassword,
VARIANT ReadOnlyRecommended,
VARIANT CreateBackup);
[id(0x0000038c), helpcontext(0x0001038c)]
VARIANT Scenarios(VARIANT Index);
[id(0x0000033e), helpcontext(0x0001033e)]
VARIANT ScrollBars(VARIANT Index);
[id(0x000000eb), helpcontext(0x000100eb)]
VARIANT Select(VARIANT Replace);
[id(0x000004a4), helpcontext(0x000104a4)]
VARIANT SetBackgroundPicture(VARIANT Filename);
[id(0x0000031a), helpcontext(0x0001031a)]
VARIANT ShowAllData();
[id(0x00000199), helpcontext(0x00010199)]
VARIANT ShowDataForm();
[id(0x00000346), helpcontext(0x00010346)]
VARIANT Spinners(VARIANT Index);
[id(0x00000309), helpcontext(0x00010309)]
VARIANT TextBoxes(VARIANT Index);
[id(0x0000011d), helpcontext(0x0001011d)]
VARIANT Unprotect(VARIANT Password);
 
没看出是哪个属性的!
 
得到excel的行数、列数:
Maxc :=ExlApp.WorkSheets[1].UsedRange.Columns.Count;
Maxr :=ExlApp.WorkSheets[1].UsedRange.Rows.Count;
 
多人接受答案了。
 
MyApp:=CreateOleObject('Excel.Application');
MyApp.WorkBooks.open(filename);
MyApp.visible[1]:=true;
//表中行数
Edit1.Text:=MyApp.workSheets[1].Rowcount;
//表中列数
Edit2.text:=MyApp.workSheets[1].Colcount;
//操纵你表中的数据,读到一个stringgrid中
for i:1 to (一个大于你表中行数) do
for j:0 to (一个大于你表中列数) do
if Trim(ExcelWorksheet[1].Cells.Item[i,1]) <>'' then
begin
StringGrid1.RowCount:=i;
StringGrid1.Cells[j,i-1]:=ExcelWorksheet1.Cells
.Item[i,j+1];
 
后退
顶部