Jump to content


Photo

Ejecutar una funcion de excel desde Delphi


  • Please log in to reply
1 reply to this topic

#1 FGarcia

FGarcia

    Advanced Member

  • Miembro Platino
  • PipPipPip
  • 687 posts
  • LocationMéxico

Posted 20 January 2010 - 10:30 AM

¿Como puedo ejecutar desde delphi una funcion de excel?
La funcion no es estandar de excel es un add-in o complemento.

En la Hoja declaro:

A1 = 1
A2 = AHORA()  */La fecha y hora actual
A3 = +A2-A1 */ El dia de ayer
A4 = "=fphxnumval(Tag,A3,A2,30)" */Una funcion a ejecutar con parametros, regresa 30 elementos o menos si existen
A5 = "=fphxgetval(i,"d") */de los 30 resultados obtenidos muestro el elemento i en modo float
B5 = "=fphxgetval(i,"t") */de los 30 resultados obtenidos muestro el tiempo de captura del elemento i


La intencion es generar la hora y fecha actual y el dia de ayer (A2 y A3) desde un datetimepicker y que al pulsar un boton se ejecute la funcion de A4 y B5 en un loop para mostrar los 30 resultados y guardarlos en excel.

El formulario, la generacion de las cadenas de fecha y hora ya esta solucionado, solo que no se como ejecutar las funciones de excel desde delphi (fphxnumval y fphgetval).



¿alguna sugerencia?
  • 0

#2 Wilson

Wilson

    Advanced Member

  • Moderadores
  • PipPipPip
  • 2137 posts

Posted 20 January 2010 - 11:26 AM

Puede que esto te sirva de algo


delphi
  1. uses
  2.   ComObj;
  3.  
  4. var
  5.   ExcelApp: OleVariant;
  6.  
  7. implementation
  8.  
  9.  
  10. procedure TForm1.Button1Click(Sender: TObject);
  11. const
  12.   // SheetType
  13.   xlChart = -4109;
  14.   xlWorksheet = -4167;
  15.   // WBATemplate
  16.   xlWBATWorksheet = -4167;
  17.   xlWBATChart = -4109;
  18.   // Page Setup
  19.   xlPortrait = 1;
  20.   xlLandscape = 2;
  21.   xlPaperA4 = 9;
  22.   // Format Cells
  23.   xlBottom = -4107;
  24.   xlLeft = -4131;
  25.   xlRight = -4152;
  26.   xlTop = -4160;
  27.   // Text Alignment
  28.   xlHAlignCenter = -4108;
  29.   xlVAlignCenter = -4108;
  30.   // Cell Borders
  31.   xlThick = 4;
  32.   xlThin = 2;
  33. var
  34.   ColumnRange: OleVariant;
  35.  
  36.   // Function to get the number of Rows in a Certain column
  37.  
  38.   function GetLastLine(AColumn: Integer): Integer;
  39.   const
  40.     xlUp = 3;
  41.   begin
  42.     Result := ExcelApp.Range[Char(96 + AColumn) +
  43. IntToStr(65536)].end[xlUp].Rows.Row;
  44.   end;
  45.  
  46. begin
  47.   { Start Excel }
  48.  
  49.   // By using GetActiveOleObject, you use an instance of Word that's
  50. already running,
  51.   // if there is one.
  52.   try
  53.     ExcelApp := GetActiveOleObject('Excel.Application');
  54.   except
  55.     try
  56.       // If no instance of Word is running, try to Create a new Excel
  57. Object
  58.       ExcelApp := CreateOleObject('Excel.Application');
  59.     except
  60.       ShowMessage('Cannot start Excel/Excel not installed ?');
  61.       Exit;
  62.     end;
  63.   end;
  64.  
  65.   // Add a new Workbook, Neue Arbeitsmappe öffnen
  66.   ExcelApp.Workbooks.Add(xlWBatWorkSheet);
  67.  
  68.   // Open a Workbook, Arbeitsmappe öffnen
  69.   ExcelApp.Workbooks.Open('c:\YourFileName.xls');
  70.  
  71.  
  72.   // Rename the active Sheet
  73.   ExcelApp.ActiveSheet.Name := 'This is Sheet 1';
  74.  
  75.   // Rename
  76.   ExcelApp.Workbooks[1].WorkSheets[1].Name := 'This is Sheet 1';
  77.  
  78.   // Insert some Text in some Cells[Row,Col]
  79.   ExcelApp.Cells[1, 1].Value := 'SwissDelphiCenter.ch';
  80.   ExcelApp.Cells[2, 1].Value := '[url]http://www.swissdelphicenter.ch[/url]';
  81.   ExcelApp.Cells[3, 1].Value := FormatDateTime('dd-mmm-yyyy', Now);
  82.  
  83.   // Setting a row of data with one call
  84.   ExcelApp.Range['A2', 'D2'].Value := VarArrayOf([1, 10, 100, 1000]);
  85.  
  86.   // Setting a formula
  87.   ExcelApp.Range['A11', 'A11'].Formula := '=Sum(A1:A10)';
  88.  
  89.   // Change Cell Alignement
  90.   ExcelApp.Cells[2, 1].HorizontalAlignment := xlright;
  91.  
  92.   // Change the Column Width.
  93.   ColumnRange := ExcelApp.Workbooks[1].WorkSheets[1].Columns;
  94.   ColumnRange.Columns[1].ColumnWidth := 20;
  95.   ColumnRange.Columns[2].ColumnWidth := 40;
  96.  
  97.   // Change Rowheight / Zeilenhöhe ändern:
  98.   ExcelApp.Rows[1].RowHeight := 15.75;
  99.  
  100.   // Merge cells, Zellen verbinden:
  101.   ExcelApp.Range['B3:D3'].Mergecells := True;
  102.  
  103.   // Apply borders to cells, Zellen umrahmen:
  104.   ExcelApp.Range['A14:M14'].Borders.Weight := xlThick; // Think line/
  105. Dicke Linie
  106.   ExcelApp.Range['A14:M14'].Borders.Weight := xlThin;  // Thin line
  107. Dünne Linie
  108.  
  109.   // Set Bold Font in cells, Fettdruck in den Zellen
  110.  
  111.   ExcelApp.Range['B16:M26'].Font.Bold := True;
  112.  
  113.   // Set Font Size, Schriftgröße setzen
  114.   ExcelApp.Range['B16:M26'].Font.Size := 12;
  115.  
  116.   //right-aligned Text, rechtsbündige Textausrichtung
  117.   ExcelApp.Cells[9, 6].HorizontalAlignment := xlright;
  118.  
  119.   // horizontal-aligned text, horizontale Zentrierung
  120.   ExcelApp.Range['B14:M26'].HorizontalAlignment := xlHAlignCenter;
  121.  
  122.   // left-aligned Text, vertikale Zentrierung
  123.   ExcelApp.Range['B14:M26'].VerticallyAlignment := xlVAlignCenter;
  124.  
  125.  
  126.   { Page Setup }
  127.  
  128.   ExcelApp.ActiveSheet.PageSetup.Orientation := xlLandscape;
  129.  
  130.   // Left, Right Margin (Seitenränder)
  131.   ExcelApp.ActiveSheet.PageSetup.LeftMargin  := 35;
  132.   ExcelApp.ActiveSheet.PageSetup.RightMargin := -15;
  133.  
  134.   // Set Footer Margin
  135.   ExcelApp.ActiveSheet.PageSetup.FooterMargin :=
  136. ExcelApp.InchesToPoints(0);
  137.  
  138.   // Fit to X page(s) wide by Y tall
  139.   ExcelApp.ActiveSheet.PageSetup.FitToPagesWide := 1;  // Y
  140.   ExcelApp.ActiveSheet.PageSetup.FitToPagesTall := 3; // Y
  141.  
  142.   // Zoom
  143.   ExcelApp.ActiveSheet.PageSetup.Zoom := 95;
  144.  
  145.   // Set Paper Size:
  146.   ExcelApp.PageSetup.PaperSize := xlPaperA4;
  147.  
  148.   // Show/Hide Gridlines:
  149.   ExcelApp.ActiveWindow.DisplayGridlines := False;
  150.  
  151.   // Set Black & White
  152.   ExcelApp.ActiveSheet.PageSetup.BlackAndWhite := False;
  153.  
  154.   // footers
  155.   ExcelApp.ActiveSheet.PageSetup.RightFooter := 'Right Footer / Rechte
  156. Fußzeile';
  157.   ExcelApp.ActiveSheet.PageSetup.LeftFooter  := 'Left Footer / Linke
  158. Fußzeile';
  159.  
  160.   // Show Excel Version:
  161.   ShowMessage(Format('Excel Version %s: ', [ExcelApp.Version]));
  162.  
  163.   // Show Excel:
  164.   ExcelApp.Visible := True;
  165.  
  166.   // Save the Workbook                                     
  167.   ExcelApp.SaveAs('c:\filename.xls');
  168.  
  169.   // Save the active Workbook:
  170.   ExcelApp.ActiveWorkBook.SaveAs('c:\filename.xls');
  171.  
  172. end;
  173.  
  174. procedure TForm1.FormDestroy(Sender: TObject);
  175. begin
  176.   // Quit Excel
  177.   if not VarIsEmpty(ExcelApp) then
  178.   begin
  179.     ExcelApp.DisplayAlerts := False;  // Discard unsaved files....
  180.     ExcelApp.Quit;
  181.   end;
  182. end;



Tambien esto



delphi
  1. Function ExcelSetCellFormula(
  2.   Excel        : Variant;
  3.   FormulaString : ShortString;
  4.   RowNum, ColNum: Integer): Boolean;
  5. Begin
  6.   Result := True;
  7.   Try
  8.     Excel.
  9.       ActiveSheet.
  10.         Cells[RowNum, ColNum].
  11.           Formula := FormulaString;
  12.   Except
  13.     Result := False;
  14.   End;
  15. End;
  16.  
  17. Function ExcelGetCellFormula(
  18.   Excel        : Variant;
  19.   RowNum, ColNum: Integer): ShortString;
  20. Begin
  21.   Result := ' ';
  22.   Try
  23.     Result := Excel.
  24.                 ActiveSheet.
  25.                 Cells[RowNum, ColNum].
  26.                 Formula;
  27.   Except
  28.     Result := ' ';
  29.   End;
  30. End;


  • 0




IP.Board spam blocked by CleanTalk.