Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Excel Save as. URGENT!!!

1,130 views
Skip to first unread message

Andres Montoya

unread,
Oct 14, 2000, 3:00:00 AM10/14/00
to
Hello


I am wrinting a program that must take an excel file and export that as a
CSV from Delphi.

I am able to open the file but When it comes to save it I am getting some
problems. Please HELP!!!!!

Here is the code:


procedure TForm1.Button1Click(Sender: TObject);
var
LCID:integer;
begin
try
LCID := GetUserDefaultLCID;
XL.Connect;
Xl.Visible[LCID]:=true;
XL.Workbooks.Open('D:\xl\xl3\xltest.xls',
EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam
,EmptyParam,EmptyParam,EmptyParam,EmptyParam,EmptyParam,LCID);

XL.ThisWorkbook[LCID].SaveAs('D:\xl\XL3\xltest2.csv',Excel97.xlCSV,'','',fal
se,false,Excel97.xlNoChange,Excel97.xlLocalSessionChanges,true,0,0,0);
XL.DisplayAlerts[LCID] := False;
finally
XL.Quit;
XL.Disconnect;
end;
end;

All I need is to save the file in CSV or DBF format.

Thanks

Michael G

unread,
Oct 15, 2000, 3:00:00 AM10/15/00
to
I think what your problem is that you are trying to save the workbook as a
csv instead of saving the worksheets. If I remember correctly, Excel will
only do a "Save As" to a csv on the "current" worksheet in your workbook.
Below is some code that I have used to do a save as on a worksheet named
"Daily Fuel Price":

var
EXWorkSheets : Sheets;
EXSheet : Variant;
lFileName: WideString;

ExWorkSheets := ExcelApp.Worksheets;
EXSheet := EXWorkSheets.Item['Daily Fuel Price'];
lFileName := ExtractFilePath(Application.exename) + 'Daily Fuel
Price.csv';
exSheet.SaveAs(lFileName, xlCSV, Null, Null, Null, Null, Null, Null,
Null);

HTH,

MichaelG

"Andres Montoya" <andr...@hotmail.com> wrote in message
news:8sae5p$82...@bornews.borland.com...

Stefano Gallina

unread,
Oct 16, 2000, 3:00:00 AM10/16/00
to
Hi Andres,

Don't forget to close your "Workbooks" before quitting.
Just call the "close" method.
Cheers.

Stefano

Andres Montoya <andr...@hotmail.com> wrote in message

peter

unread,
Oct 25, 2000, 3:00:00 AM10/25/00
to

Andres Montoya <andr...@hotmail.com> schreef in berichtnieuws

maybe the next source is a help for you :


Function ExcelSaveAsText(
Excel : Variant;
ExcelFirstRow : Integer;
ExcelFirstCol : Integer;
ExcelLastRow : Integer;
ExcelLastCol : Integer;
OutFilePath : ShortString;
OutFileName : ShortString): Boolean;
{
OutFileFormat: Use one of the following
xlAddIn xlExcel3 xlTextMSDOS
xlCSV xlExcel4 xlTextWindows
xlCSVMac xlExcel4Workbook xlTextPrinter
xlCSVMSDOS xlIntlAddIn xlWK1
xlCSVWindows xlIntlMacro xlWK3
xlDBF2 xlNormal xlWKS
xlDBF3 xlSYLK xlWQ1
xlDBF4 xlTemplate xlWK3FM3
xlDIF xlText xlWK1FMT
xlExcel2 xlTextMac xlWK1ALL
}
Var
FullOutName : String;
Begin
Result := False;
Try
If OutFilePath <> '' Then
Begin
If Not (Copy(OutFilePath,Length(OutFilePath),1) = '\') Then
Begin
OutFilePath := OutFilePath + '\';
End;
End;
FullOutName := OutFilePath + OutFileName;
If FileExists(FullOutName) Then DeleteFile(FullOutName);

If ExcelVersion(Excel) = '8.0' Then
Begin
ExcelSelectCell(Excel,ExcelFirstRow,ExcelFirstCol);
ExcelSelectBlockWhole(Excel);
file://Excel.SendKeys('^+{END}');
End
Else
Begin
Excel.
Range(
ExcelColIntToStr(ExcelFirstCol)+
IntToStr(ExcelFirstRow)+
':'+
ExcelColIntToStr(ExcelLastCol)+
IntToStr(ExcelLastRow)
).
Select;
End;
{
FileFormat = (xlAddIn, xlCSV, xlCSVMac, xlCSVMSDOS, xlCSVWindows, xlDBF2,
xlDBF3, xlDBF4, xlDIF, xlExcel2, xlExcel3, xlExcel4,
xlExcel4Workbook, xlIntlAddIn, xlIntlMacro, xlNormal,
xlSYLK, xlTemplate, xlText, xlTextMac, xlTextMSDOS,
xlTextWindows, xlTextPrinter, xlWK1, xlWK3, xlWKS,
xlWQ1, xlWK3FM3, xlWK1FMT, xlWK1ALL);
}
(*
file://CHECKING OUT THE GARBLED OUTPUT
// Produces an *.xls
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'02',xlCSV);
*)
// Produces an *.txt
// Excel.
// ActiveSheet.
// SaveAs(
// FullOutName,xlCSVMSDOS);
(*
// Produces nothing
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'05',xlCSVWindows);
// Produces nothing
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'06',xlDBF2);

// Produces an *.txt comma separated
Excel.
ActiveSheet.
SaveAs(
FullOutName,xlDBF3);
*)
// Produces an *.txt
Excel.
ActiveSheet.
SaveAs(
FullOutName,xlTextMSDOS);
(*
// Produces an *.dbf
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'08',xlDBF4);
// Produces an *.dbf
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'09',xlDIF);
// Produces an *.dif
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'10',xlExcel2);
// Produces an *.slk
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'11',xlExcel3);
// Produces an *.dbf
Excel.
ActiveSheet.
SaveAs(
OutFilePath+OutFileName+'12',xlExcel4);

*)
Result := True;
Except
Result := False;
End;
End;

Peter
iT2
Boxtel
the Netherlands

Martin Robson

unread,
Oct 25, 2000, 3:00:00 AM10/25/00
to
Peter,
I think you just need to type-cast your parameters.

This works fine for me...

var
WkBk : TExcelWorkBook;
CurrentApp : TExcelApplication;
OVFile,
OVFileFormat,
OVFalse : OLEVariant;
...
begin
....
OVFileFormat := TOleEnum(xlCSV);
OVFile := sFilename; {'D:\xl\XL3\xltest2.csv'}
OVFalse := false;
...
WkBk.ConnectTo(CurrentApp.ActiveWorkbook as _Workbook);
.....
WkBk.SaveAs(OVFile, OVFileFormat, EmptyParam, EmptyParam, OVFalse,
EmptyParam, TOLEEnum(xlNoChange), EmptyParam, EmptyParam, EmptyParam,
EmptyParam, LCID);
...

Martin.

0 new messages