How to set Excel column types and formatting?

I am exporting data from CxDBGrid to an Excel file. I can create a file and copy the data into it, but I had real problems with formatting the columns. Since I am extracting data from the database, I would like the table to display the type: NUMBER, VARCHAR2, DATE, etc. I visually created a macro, went looking for VBA code and reproduced it in a Delphi project:

sheet.Columns[K+2].NumberFormat := '0,000'; //Number
sheet.Columns[K+2].NumberFormat := '@'; //Text
sheet.Columns[K+2].NumberFormat := 'm/d/yyyy'; //Date

Number formatting work is done in most cases, but the other two do not work. When I open the generated file, the text columns are displayed as type "Custom", and each cell displays "-64". If I move on to editing the cell, in fact there will be the correct value. Date is another problem: the database format is dd / mm / yyyy, and if I submit it to Excel as is, it will all go bad. I tried to set the correct format, but Excel does not recognize it.

Any clues?

I also set the column width. It works flawlessly.

+5
source share
2 answers

The problem is that the assigned values ​​are Unicode strings. Try the following:

sheet.Columns[K+2].NumberFormat := AnsiChar('@');
sheet.Columns[K+2].NumberFormat := AnsiString('m/d/yyyy');
+4
source

" ", , , . , :

  • , Express Spreadsheet Developer Express . . "" (0,00) . . , Express.

  • , "", , OLE-, OLE Automation Excel, CELLS , , . , - , . , , .

  • , , Excel OLE, Excel XLS. , DX CX (DevEx), , . DB TJvDBGridExcelExport, Jedi JVCL, VCL DB.

+3

All Articles