Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Eliminar Fila en Hoja de Excel (https://www.clubdelphi.com/foros/showthread.php?t=83602)

steelha 05-07-2013 20:52:07

Eliminar Fila en Hoja de Excel
 
Buenas, he tratado de buscar como eliminar una fila de un archivo en excel desde delphi, y he tratado varios métodos sin ningún resultado acá dejo parte del código para que sea verificado a ver que le hace falta. Aaah Al final de eliminar las filas me interesa guardar el archivo con los cambios.

Código Delphi [-]
procedure TfrmGenerarTxt.Button1Click(Sender: TObject);
Var
  i     : Integer;
  j     : Integer;
  si    : String;
  h     : integer;
  ultfil: Integer;
  excel : OleVariant;
  Book  : OleVariant;
  hoja  : _Worksheet;
begin
  with UArchivoExcel do
  begin
    InitialDir := GetCurrentDir;
    Options := [ofFileMustExist];
    Filter  := 'Excel 97-2003|*.xls|Excel 2007-2010|*.xlss';
    FilterIndex := 1;

    if Execute then
      lstArchivo.Lines.Text := FileName
    else
      ShowMessage('Selección de Archivo Cancelada por el usuario');
  end;

  try
    ExcelApp.Workbooks.Open(Trim(lstArchivo.Lines.Text), EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, 
                            EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, 0);
    hoja := ExcelApp.Worksheets.Item[1] as _WorkSheet;
    ExcelApp.Cells.SpecialCells(xlCellTypeLastCell,EmptyParam).Activate;
    ultfil := ExcelApp.ActiveCell.Row;
    ShowMessage(IntToStr(ultfil));

  finally
    ShowMessage(IntToStr(ultfil));
    for i:=1 to ultfil do
    begin
      hoja.Cells.Range['A'+IntToStr(i),'A'+IntToStr(i)].Activate;
      If not VarIsNumeric(LeftStr(ExcelApp.Range['A'+IntToStr(i),'A'+IntToStr(i)].Value,1)) then
      begin
//        hoja.Cells.Range['A'+IntToStr(i),'A'+IntToStr(i)].Row.Delete;
      end;
    end;
  end;
  ExcelApp.Workbooks.Close(0);
end;

nlsgarcia 06-07-2013 07:11:15

steelha,

Cita:

Empezado por steelha
..he tratado de buscar como eliminar una fila de un archivo en excel desde Delphi...sin ningún resultado...

Revisa este código:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComObj;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
const
   xlOpenXMLWorkbook =  51; // Open XML Workbook Format

var
   Excel, WrkS : Variant;
   FileExcel : String;
   i : Integer;

begin

   FileExcel := ExtractFilePath(Application.ExeName) + 'TestExcel.xlsx';

   try

      // Crea una instancia de Automatización de Excel
      Excel := CreateOleObject('Excel.Application');

      // Configura parámetros de ejecución de Excel
      Excel.DisplayAlerts := False;
      Excel.Visible := False;

      // Abre el libro de Excel referenciado en el String FileExcel
      Excel.Workbooks.Open(FileExcel);

      // Asigna la primera hoja de cálculo del libro de Excel a WrkS
      WrkS := Excel.Worksheets[1];

      // Borra toda la información previa en WrkS
      WrkS.Cells.ClearContents;

      // Adiciona 25 filas de información a WrkS (Filas 1 a 25, Columnas A, B y C)
      for i := 1 to 25 do
      begin
         WrkS.Cells[i, 1].Value := i;
         WrkS.Cells[i, 2].Value := 'Item-'+IntToStr(i);
         WrkS.Cells[i, 3].Value := 'Description-'+IntToStr(i);
      end;

      // Borra las filas 21 a la 25 en WrkS
      for i := 25 downto 21 do
         WrkS.Rows[i].Delete;
  
      // Borra un rango de filas en WrkS (Filas 1 a la 6, Columnas A, B y C)
      WrkS.Range['A1:C6'].EntireRow.Delete(EmptyParam);

      // Asigna valores al rango A2:C2 (Fila 2, Columnas A,B y C)
      WrkS.Range['A2:C2'].Value := 777;

      // Salva WrkS
      WrkS.SaveAs(FileExcel,xlOpenXMLWorkbook);

   finally

      // Finaliza la automatización de Excel y libera recursos
      Excel.Quit;
      Excel := Unassigned;
      WrkS := Unassigned;

   end;

end;

end.
El código anterior es un ejemplo de manipulación de datos por medio de Automatización en Excel con Delphi 7 y Excel 2010.

Te sugiero consultar esta información:
Cita:

Delphi and Microsoft Office Automating Excel and Word : http://edn.embarcadero.com/article/10126
Espero sea útil :)

Nelson.

steelha 06-07-2013 14:18:58

Muchas gracias nlsgarcia, me gusta que cuando ayudas explicas muy bien para que sirven cada linea de código que es lo mejor para aprender.


La franja horaria es GMT +2. Ahora son las 22:32:44.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi