Delphi5 控制 Excel2000 心得(0分)

  • 主题发起人 主题发起人 sandy suen
  • 开始时间 开始时间
Thank you!这正是我需要的。
 
很奇怪,我在通过程序关闭excel时执行到 excelApp1:=UnAssigned; 这条语句时候总是报错
出现“Method "UnAssigned" not supported by automation object!”
谁知道怎么才能释放这个variant变量啊
 
问题已经解决,在引用excel对象前执行一次 excelApps:=unAssigned; 就不会出错,菜鸟问的问题也菜,让
各位见笑了;采用 comObj,有不少好处,但是,不能直接引用类型库中定义的常量,如不能引用xlWBATWorksheet,
而只能是-4167,下面是一个对应表(是我下载的一个源程序中带的,各大家共享,高手就不必管它啦:)

const
{XlPageOrientation}
xlLandscape = 2;
xlPortrait = 1;
{XlPaperSize}
xlPaper10x14 = 16;
xlPaper11x17 = 17;
xlPaperA3 = 8;
xlPaperA4 = 9;
xlPaperA4Small = 10;
xlPaperA5 = 11;
xlPaperB4 = 12;
xlPaperB5 = 13;
xlPaperCsheet = 24;
xlPaperDsheet = 25;
xlPaperEnvelope10 = 20;
xlPaperEnvelope11 = 21;
xlPaperEnvelope12 = 22;
xlPaperEnvelope14 = 23;
xlPaperEnvelope9 = 19;
xlPaperEnvelopeB4 = 33;
xlPaperEnvelopeB5 = 34;
xlPaperEnvelopeB6 = 35;
xlPaperEnvelopeC3 = 29;
xlPaperEnvelopeC4 = 30;
xlPaperEnvelopeC5 = 28;
xlPaperEnvelopeC6 = 31;
xlPaperEnvelopeC65 = 32;
xlPaperEnvelopeDL = 27;
xlPaperEnvelopeItaly = 36;
xlPaperEnvelopeMonarch = 37;
xlPaperEnvelopePersonal = 38;
xlPaperEsheet = 26;
xlPaperExecutive = 7;
xlPaperFanfoldLegalGerman = 41;
xlPaperFanfoldStdGerman = 40;
xlPaperFanfoldUS = 39;
xlPaperFolio = 14;
xlPaperLedger = 4;
xlPaperLegal = 5;
xlPaperLetter = 1;
xlPaperLetterSmall = 2;
xlPaperNote = 18;
xlPaperQuarto = 15;
xlPaperStatement = 6;
xlPaperTabloid = 3;
xlPaperUser = 256;
{XlPasteSpecialOperation}
xlPasteSpecialOperationAdd = 2;
xlPasteSpecialOperationDivide = 5;
xlPasteSpecialOperationMultiply = 4;
xlPasteSpecialOperationNone = -4142;
xlPasteSpecialOperationSubtract = 3;
{XlPasteType}
xlPasteAll = -4104;
xlPasteAllExceptBorders = 6;
xlPasteFormats = -4122;
xlPasteFormulas = -4123;
xlPasteComments = -4144;
xlPasteValues = -4163;
{XlClipboardFormat}
xlClipboardFormatBIFF = 8;
xlClipboardFormatBIFF2 = 18;
xlClipboardFormatBIFF3 = 20;
xlClipboardFormatBIFF4 = 30;
xlClipboardFormatBinary = 15;
xlClipboardFormatBitmap = 9;
xlClipboardFormatCGM = 13;
xlClipboardFormatCSV = 5;
xlClipboardFormatDIF = 4;
xlClipboardFormatDspText = 12;
xlClipboardFormatEmbeddedObject = 21;
xlClipboardFormatEmbedSource = 22;
xlClipboardFormatLink = 11;
xlClipboardFormatLinkSource = 23;
xlClipboardFormatLinkSourceDesc = 32;
xlClipboardFormatMovie = 24;
xlClipboardFormatNative = 14;
xlClipboardFormatObjectDesc = 31;
xlClipboardFormatObjectLink = 19;
xlClipboardFormatOwnerLink = 17;
xlClipboardFormatPICT = 2;
xlClipboardFormatPrintPICT = 3;
xlClipboardFormatRTF = 7;
xlClipboardFormatScreenPICT = 29;
xlClipboardFormatStandardFont = 28;
xlClipboardFormatStandardScale = 27;
xlClipboardFormatSYLK = 6;
xlClipboardFormatTable = 16;
xlClipboardFormatText = 0;
xlClipboardFormatToolFace = 25;
xlClipboardFormatToolFacePICT = 26;
xlClipboardFormatVALU = 1;
xlClipboardFormatWK1 = 10;

xlAll = -4104;
xlAutomatic = -4105;
xlBoth = 1;
xlCenter = -4108;
xlChecker = 9;
xlCircle = 8;
xlCorner = 2;
xlCrissCross = 16;
xlCross = 4;
xlDiamond = 2;
xlDistributed = -4117;
xlDoubleAccounting = 5;
xlFixedValue = 1;
xlFormats = -4122;
xlGray16 = 17;
xlGray8 = 18;
xlGrid = 15;
xlHigh = -4127;
xlInside = 2;
xlJustify = -4130;
xlLightDown = 13;
xlLightHorizontal = 11;
xlLightUp = 14;
xlLightVertical = 12;
xlLow = -4134;
xlManual = -4135;
xlMinusValues = 3;
xlModule = -4141;
xlNextToAxis = 4;
xlNone = -4142;
xlNotes = -4144;
xlOff = -4146;
xlOn = 1;
xlPercent = 2;
xlPlus = 9;
xlPlusValues = 2;
xlSemiGray75 = 10;
xlShowLabel = 4;
xlShowLabelAndPercent = 5;
xlShowPercent = 3;
xlShowValue = 2;
xlSimple = -4154;
xlSingle = 2;
xlSingleAccounting = 4;
xlSolid = 1;
xlSquare = 1;
xlStar = 5;
xlStError = 4;
xlToolbarButton = 2;
xlTriangle = 3;
xlGray25 = -4124;
xlGray50 = -4125;
xlGray75 = -4126;
xlBottom = -4107;
xlLeft = -4131;
xlRight = -4152;
xlTop = -4160;
xl3DBar = -4099;
xl3DSurface = -4103;
xlBar = 2;
xlColumn = 3;
xlCombination = -4111;
xlCustom = -4114;
xlDefaultAutoFormat = -1;
xlMaximum = 2;
xlMinimum = 4;
xlOpaque = 3;
xlTransparent = 2;
xlBidi = -5000;
xlLatin = -5001;
xlContext = -5002;
xlLTR = -5003;
xlRTL = -5004;
xlVisualCursor = 2;
xlLogicalCursor = 1;
xlSystem = 1;
xlPartial = 3;
xlHindiNumerals = 3;
xlBidiCalendar = 3;
xlGregorian = 2;
xlComplete = 4;
xlScale = 3;
xlClosed = 3;
xlColor1 = 7;
xlColor2 = 8;
xlColor3 = 9;
xlConstants = 2;
xlContents = 2;
xlBelow = 1;
xlCascade = 7;
xlCenterAcrossSelection = 7;
xlChart4 = 2;
xlChartSeries = 17;
xlChartShort = 6;
xlChartTitles = 18;
xlClassic1 = 1;
xlClassic2 = 2;
xlClassic3 = 3;
xl3DEffects1 = 13;
xl3DEffects2 = 14;
xlAbove = 0;
xlAccounting1 = 4;
xlAccounting2 = 5;
xlAccounting3 = 6;
xlAccounting4 = 17;
xlAdd = 2;
xlDebugCodePane = 13;
xlDesktop = 9;
xlDirect = 1;
xlDivide = 5;
xlDoubleClosed = 5;
xlDoubleOpen = 4;
xlDoubleQuote = 1;
xlEntireChart = 20;
xlExcelMenus = 1;
xlExtended = 3;
xlFill = 5;
xlFirst = 0;
xlFloating = 5;
xlFormula = 5;
xlGeneral = 1;
xlGridline = 22;
xlIcons = 1;
xlImmediatePane = 12;
xlInteger = 2;
xlLast = 1;
xlLastCell = 11;
xlList1 = 10;
xlList2 = 11;
xlList3 = 12;
xlLocalFormat1 = 15;
xlLocalFormat2 = 16;
xlLong = 3;
xlLotusHelp = 2;
xlMacrosheetCell = 7;
xlMixed = 2;
xlMultiply = 4;
xlNarrow = 1;
xlNoDocuments = 3;
xlOpen = 2;
xlOutside = 3;
xlReference = 4;
xlSemiautomatic = 2;
xlShort = 1;
xlSingleQuote = 2;
xlStrict = 2;
xlSubtract = 3;
xlTextBox = 16;
xlTiled = 1;
xlTitleBar = 8;
xlToolbar = 1;
xlVisible = 12;
xlWatchPane = 11;
xlWide = 3;
xlWorkbookTab = 6;
xlWorksheet4 = 1;
xlWorksheetCell = 3;
xlWorksheetShort = 5;
xlAllExceptBorders = 6;
xlLeftToRight = 2;
xlTopToBottom = 1;
xlVeryHidden = 2;
xlDrawingObject = 14;
{XlChartType}
xlColumnClustered = 51;
xlColumnStacked = 52;
xlColumnStacked100 = 53;
xl3DColumnClustered = 54;
xl3DColumnStacked = 55;
xl3DColumnStacked100 = 56;
xlBarClustered = 57;
xlBarStacked = 58;
xlBarStacked100 = 59;
xl3DBarClustered = 60;
xl3DBarStacked = 61;
xl3DBarStacked100 = 62;
xlLineStacked = 63;
xlLineStacked100 = 64;
xlLineMarkers = 65;
xlLineMarkersStacked = 66;
xlLineMarkersStacked100 = 67;
xlPieOfPie = 68;
xlPieExploded = 69;
xl3DPieExploded = 70;
xlBarOfPie = 71;
xlXYScatterSmooth = 72;
xlXYScatterSmoothNoMarkers = 73;
xlXYScatterLines = 74;
xlXYScatterLinesNoMarkers = 75;
xlAreaStacked = 76;
xlAreaStacked100 = 77;
xl3DAreaStacked = 78;
xl3DAreaStacked100 = 79;
xlDoughnutExploded = 80;
xlRadarMarkers = 81;
xlRadarFilled = 82;
xlSurface = 83;
xlSurfaceWireframe = 84;
xlSurfaceTopView = 85;
xlSurfaceTopViewWireframe = 86;
xlBubble = 15;
xlBubble3DEffect = 87;
xlStockHLC = 88;
xlStockOHLC = 89;
xlStockVHLC = 90;
xlStockVOHLC = 91;
xlCylinderColClustered = 92;
xlCylinderColStacked = 93;
xlCylinderColStacked100 = 94;
xlCylinderBarClustered = 95;
xlCylinderBarStacked = 96;
xlCylinderBarStacked100 = 97;
xlCylinderCol = 98;
xlConeColClustered = 99;
xlConeColStacked = 100;
xlConeColStacked100 = 101;
xlConeBarClustered = 102;
xlConeBarStacked = 103;
xlConeBarStacked100 = 104;
xlConeCol = 105;
xlPyramidColClustered = 106;
xlPyramidColStacked = 107;
xlPyramidColStacked100 = 108;
xlPyramidBarClustered = 109;
xlPyramidBarStacked = 110;
xlPyramidBarStacked100 = 111;
xlPyramidCol = 112;
xl3DColumn = -4100;
xlLine = 4;
xl3DLine = -4101;
xl3DPie = -4102;
xlPie = 5;
xlXYScatter = -4169;
xl3DArea = -4098;
xlArea = 1;
xlDoughnut = -4120;
xlRadar = -4151;
{XlBarShape}
xlBox = 0;
xlPyramidToPoint = 1;
xlPyramidToMax = 2;
xlCylinder = 3;
xlConeToPoint = 4;
xlConeToMax = 5;
{XlSheetType}
xlChart = -4109;
xlDialogSheet = -4116;
xlExcel4IntlMacroSheet = 4;
xlExcel4MacroSheet = 3;
xlWorksheet = -4167;

 
我这儿还有一个关于word的常量对应表,也不知道各位需不需要
 
关于dongxsoft的一个问题:
其实,我第一次回答个问题,惶恐中~~(我是才学DELPHI不久,今天刚刚尝试delphi控制excel,就遇上了dongxsoft的那个问题),你说的
那种情况,我估计是excel对象没有释放干净,还驻留在进程中,你可以在任务管理器中查看一下。我建议不要用server组件,而直接
用excelApp:=CreateOleObject('excel.Application'); 要不出现你那种只能见到主窗体,而不能见到工作表的情况,
应该在执行上面那条语句前执行 excelApp:=UnAssigned; ,在关闭excel的语句后再执行excelApp:=UnAssigned,
应该就不会出现你说的那种情况,你可以试一试。 这个variant变量的释放很重要。
 
我要word的常量对应表,能贴出来吗?或E_mail一份:losdg@163.com
 
请问:
如何知道UsedRange的行数和列数?
 
我下载了一个 formula one 6.1,发现可以直接读入excel2000的文件,界面和excel差不多,
看起来就是集成在自己的界面中(我正为这问题犯愁,oleContainer不好用),可惜就是没有
formula one的帮助文件,谁知道怎么用这个Active控件吗
 
关于cxzhu的提问:
可以利用VBA中的SpecialCells函数,返回一个 Range 对象,此对象代表与指定类型及
值相匹配的所有单元格,下面为其可用的常数:

xlCellTypeAllFormatConditions 任意格式的单元格
xlCellTypeAllValidation 具有有效条件的单元格
xlCellTypeBlanks 空单元格
xlCellTypeComments 包含注释的单元格
xlCellTypeConstants 包含常量的单元格
xlCellTypeFormulas 包含公式的单元格
xlCellTypeLastCell 已用区域的最后一个单元格 /////××××//这个常数在确定范围最有用!
xlCellTypeSameFormatConditions 具有相同格式的单元格
xlCellTypeSameValidation 具有相同有效条件的单元格
xlCellTypeVisible 所有可见单元格

比如我们可以利用这个函数确定最后一个包含数据的单元格:
ExcelApp.WorkBooks.ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell,EmptyParam).Activate;
lastRow := ExcelApp.ActiveCell.Row;
lastCol := ExcelApp.ActiveCell.Column;

在用Servers组件 和用 Use comobj 两种方法上调用VBA函数有所不同,前面的方法不能
省略常数(如上面的EmptyParam),但好处是可以直接用其常量(如xlCellTypeLastCell),
而后者则可省略一些默认常数(比如EmptyParam可不用写,不过不知道是不是对所有的都适用)
在不知道VBA函数一些参数是有这种方法比较好,可以像VBA那样直接调用,但也有个很大缺陷
就是不能直接用那些常量,而须是其直接代表的常数(所以有了上面的那张常用常量对照表)

 
上面打错了几个字........不能直接省略其参数(如上面的EmptyParam)........

我很少上论坛,可能有些礼仪不太懂,我发了帖子,就发现给很多人都发出了e-mail........
不好意思啊。我真的是菜鸟
 
SpecialCells真的很好用,谢谢海之豚。
顺便说一下,是否接收e_mail是由参加讨论的人自己选择的,你不能控制,也不必担心。
 
to 火龙真人:
具体格式你可以看看excel中的"单元格格式自定义",照着上面的格式抄就行了
 
to lop兄:
发给你的那个word的常量对照表并不是全(我上面贴的那个excel的对照也不全,比如
SpecialCells函数中的xlCellTypeLastCell=11就没有,而不用servers组件的话又非知道
不可),所以有里面没包括的常数,你直接找对应,其实稍微花点时间,这些对照都可以知道的
 
word2000=creatoleobject(word.application);
运行时提示“呼叫请求被对方拒绝”
 
谢谢海之豚兄。
 
谢谢!有关于WORD和POWERPOINT的吗?有的话我可以给分以示感谢。
 
to wang_junfan:
其实,你需要这些对照表,完全可以直接利用DELPHI6的SERVERS组件,首先找到office
自带的VBA函数帮助(完全安装都会有),找到你需要用的函数的参数(比如你想知道
wdPaperA4 = ??),把一个WordApplication组件拖到Form上, 通过
ShowMessage(IntToStr(wdPaperA4));就可以知道wdPaperA4 = 7,依次类推,你需要powerpoint
或者word的一些常量都可以通过类似方法找到其代表的具体数值
 
后退
顶部