PDA

Ver la Versión Completa : exportar/importa a excel


martonbarbosa
03-08-2011, 19:18:32
Hola gente buenas!!!

Tengo un problema que me vuelve loco!
Estoy haciendo un programa para cargar clientes y exportarlos a excel, pero tambien puedo importalos.

El problema es el siguiente:

Cuando exporta una planilla que contiene fechas, yo en excel la veo bien, pero cuando importo esa planilla a mi StringGrid veo la fecha como numeros!

Digamos en excel veo: 01/08/2011 y en el stringgrid veo: 40551
la verdad me vuelve loco... todo mi programa anda bien menos eso...

paso a dejar los codigos de cada accion!

Abrir una planilla de excel:

procedure TTrabajos.BitBtn3Click(Sender: TObject);
var
i, r, x, lin : Integer;
si: String;
Hoja: _WorkSheet;
buttonSelected : Integer;
begin
if BitBtn3.Caption = 'Abrir' then
begin
PlaySound('open', hInstance, SND_RESOURCE or SND_ASYNC);
BitBtn3.Kind := bkCancel;
BitBtn3.Caption := 'Cerrar';
DecimalSeparator:='.';
Excel.Workbooks.Open( ExtractFilePath( Application.ExeName ) + 'Data\Trabajos.xls',
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, 0 );
Hoja := Excel.Worksheets.Item[1] as _WorkSheet;
i := 2;
si := IntToStr( i );
r := 1;
StringGrid1.Row := StringGrid1.Rowcount - 1;
repeat
with StringGrid1 do
begin
Cells[0,r] := Hoja.Range['A'+si,'A'+si].Value2;
Cells[1,r] := Hoja.Range['B'+si,'B'+si].Value2;
Cells[2,r] := Hoja.Range['C'+si,'C'+si].Value2;
Cells[3,r] := Hoja.Range['D'+si,'D'+si].Value2;
r := r + 1;
RowCount := StringGrid1.RowCount + 1;
end;
Inc( i );
si := IntToStr( i );
until ( VarType( Excel.Range['A'+si,'A'+si].Value2 ) = VarEmpty );
Excel.Workbooks.Close( 0 );
With Combobox1.Items do
Begin
for x := 1 to StringGrid1.RowCount -1 do
begin
Add(StringGrid1.Cells[0,x]);
end;
end;
end
else
begin
PlaySound('close', hInstance, SND_RESOURCE or SND_ASYNC);
buttonSelected := MessageDlg('Desea Guardar los datos?',mtInformation , [mbYes,mbCancel], 0);
if buttonSelected = mrYes then
begin
BitBtn2.Click;
BitBtn3.Kind := bkOK;
BitBtn3.Caption := 'Abrir';
Combobox1.Clear;
for lin := 1 to StringGrid1.RowCount -1 do
StringGrid1.Rows[lin].Clear;
Stringgrid1.RowCount := 2;
end
else
begin
BitBtn3.Kind := bkOk;
BitBtn3.Caption := 'Abrir';
Combobox1.Clear;
For lin := 1 to StringGrid1.RowCount -1 do
stringGrid1.Rows[lin].clear;
StringGrid1.RowCount := 2;
end;
end;
end;


Procedimiento para guardar la planilla!

procedure TTrabajos.BitBtn2Click(Sender: TObject);
var
Hoja: _WorkSheet;
i, r : Integer;
si, format : String;
begin
PlaySound('start', hInstance, SND_RESOURCE or SND_ASYNC);
// Abrimos excel
Excel.Connect;
// Creamos un nuevo libro con tres hojas (predeterminado)
Excel.Workbooks.Add( NULL, 0 );
// Apuntamos a la primera hoja y le cambiamos el nombre
Hoja := Excel.Worksheets.Item[1] as _WorkSheet;
Hoja.Name := 'Clientes';
// Nombre Columnas
Hoja.Range['A1','A1'].Value2 := StringGrid1.Cells[0,0];
Hoja.Range['A1','A1'].ColumnWidth := 14;
Hoja.Range['B1','B1'].Value2 := StringGrid1.Cells[1,0];
//Hoja.Range['B1','B1'].ColumnWidth := 70;
Hoja.Range['C1','C1'].Value2 := StringGrid1.Cells[2,0];
Hoja.Range['C1','C1'].ColumnWidth := 14;
Hoja.Range['D1','D1'].Value2 := StringGrid1.Cells[3,0];
Hoja.Range['D1','D1'].ColumnWidth := 14;
// Datos
i := 2;
si := IntToStr( i );
r := 1;
Repeat
Hoja.Range['A'+si,'A'+si].Value2 := StringGrid1.Cells[0,r];
Hoja.Range['B'+si,'B'+si].Value2 := StringGrid1.Cells[1,r];
Hoja.Range['C'+si,'C'+si].Value2 := StringGrid1.Cells[2,r];
Hoja.Range['D'+si,'D'+si].Value2 := StringGrid1.Cells[3,r];
r := r + 1;
Inc( i );
si := IntToStr( i );
Until (r >= 100);
//formato de fecha
Format := 'dd/mm/yyyy';
Hoja.Range['C2','D10'].NumberFormat := Format;
// Damos formato a los totales
Hoja.Range['A1','D1'].Font.Bold := True; // fuente negrita
Hoja.Range['A1','D1'].Interior.Color := clLime; // Interior Verde Lima
Hoja.Range['A1','D1'].Borders.Weight := xlMedium;
hoja.Range['A2','D10'].Borders.Weight := xlThin;
Hoja.Range['A1','D1'].HorizontalAlignment := xlCenter;
Hoja.Range['B2','B10'].Columns.AutoFit;
//Hoja.Range['C2','D10'].HorizontalAlignment := xlLeft;
//Deshabilitamos alerta de sobreescritura
Excel.Application.DisplayAlerts[1] := False;
// Lo primero que hacemos es guardarlo
Excel.ActiveWorkbook.SaveAs( ExtractFilePath( Application.ExeName ) + 'Data\Trabajos.xls',
EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, xlNoChange,
EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, 0);
Excel.Quit;
Excel.Disconnect;
end;


La verdad que parte del codigo lo saque de internet y anda bien... y no veo errores!

si alguien me puede dar una mano seria GENIAL!

Desde ya muchas gracias

Salu2
4n71chr157