Home > Back-end >  C builder6 export excel
C builder6 export excel

Time:09-26

A bcb5 development program before transplantation into bcb6 need to add an export excel function, the question now is whether the ole method program error
Access violation at address 005 ae277. Before a project with bcb6 development with the same code, there is no error, just began to learn perfect BCB can not find direction, please everyone a great god help

CodePudding user response:

For the source code. (...... )

CodePudding user response:

Use ole need a computer must be installed in the office, or error
In demon elder brother several related article site, you can consult
http://www.ccrun.com/page.asp? C=5 & amp; S=13

CodePudding user response:

The complete code,

# define PG OlePropertyGet
# define PS OlePropertySet
# define OP OleProcedure
# define OF OleFunction
The Variant Ex, Wb, Sheet;
AnsiString FN="";
AnsiString row="";
Try
{
FN=GetCurrentDir () + "\ \ Excel \ \ CuStyle XLS";
if(! FileExists (FN))
{
Application - & gt; MessageBox (L "report template file does not exist, cannot open!" , L "error", MB_ICONSTOP | MB_OK);
return;
}
Try
{
The Ex=the Variant: : CreateObject (" Excel. Application ");
}
The catch (... )
{
Application - & gt; MessageBox (L "Excel cannot be started," L "error", MB_ICONSTOP | MB_OK);
return;
}
Ex. PS (" Visible ", false);
Ex. The PG (" WorkBooks "). The OP (" Open ", FN. C_str ());
Wb=Ex. PG (" ActiveWorkBook ");
AnsiString shname="Style";
Sheet=Wb. PG (" sheets ", shname c_str ());
//Sheet. PG (" Range "and" A2: I3000 "). The OP (" the Clear ");
Int iRows, LI;
LI=OrdHis - & gt; RecordCount.
OrdHis - & gt; The First ();
For (iRows=2; IRows
  • {
    Sheet. PG (" Cells ", iRows, 1), PS (" Value ", OrdHis - & gt; Fields - & gt; Fields [0] - & gt; AsAnsiString. C_str ());
    Sheet. PG (" Cells ", iRows, 2). PS (" Value ", OrdHis - & gt; Fields - & gt; Fields [1] - & gt; AsAnsiString. C_str ());
    Sheet. PG (" Cells ", iRows, 3). PS (" Value ", OrdHis - & gt; Fields - & gt; Fields [2] - & gt; AsFloat);
    Sheet. PG (" Cells ", iRows, 4). PS (" Value ", OrdHis - & gt; Fields - & gt; Fields [3] - & gt; AsFloat);
    Sheet. PG (" Cells ", iRows, 5). PS (" Value ", OrdHis - & gt; Fields - & gt; Fields [4] - & gt; AsFloat);
    Sheet. PG (" Cells ", iRows, 6). PS (" Value ", OrdHis - & gt; Fields - & gt; Fields [5] - & gt; AsFloat);
    Sheet. PG (" Cells ", iRows, 7). PS (" Value ", OrdHis - & gt; Fields - & gt; Fields [6] - & gt; AsAnsiString. C_str ());
    Sheet. PG (" Cells ", iRows, 8). PS (" Value ", OrdHis - & gt; Fields - & gt; Fields [7] - & gt; AsAnsiString. C_str ());
    OrdHis - & gt; The Next ();
    }
    Row=AnsiString (). Sprintf (" % d: 5000, "iRows);
    Sheet. PG (" Rows ", WideString (row). The OP (" delete ");

    Wb. OP (" Save ");
    Wb. OP (" Close ");
    Ex. OF (" Quit ");
    The Ex=Unassigned;
    Wb=Unassigned;
    Sheet=Unassigned;
    }
    The catch (... )
    {
    Application - & gt; MessageBox (L "operation Excel spreadsheet fail!" , L "error", MB_ICONSTOP | MB_OK);
    Wb. OP (" Save ");
    Wb. OP (" Close ");
    Ex. OF (" Quit ");
    The Ex=Unassigned;
    Wb=Unassigned;
    Sheet=Unassigned;
    }
    ShowMessage (" data export success... ");

    CodePudding user response:

    The client program host to install office,

    CodePudding user response:

    //-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
    //function: export EXCEL, save the path, whether preview
    //-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    Void __fastcall TfrmMain: : DBGToExcel (TADOQuery * ADOQuery, TDBGrid * DBGrid, String XlsFile, bool PreView)
    {
    ADOQuery - & gt; The First ();
    If (ADOQuery - & gt; RecordCount==0)
    {
    MessageBox (NULL, "no export content, operation cancelled!" , "tip", MB_OK | MB_ICONINFORMATION);
    return;
    }

    TFileStream * fs=new TFileStream (XlsFile fmCreate);
    String lf="\ r \ n";//line breaks
    String TAB=char (9);//in column operator
    String v.
    //export field name
    For (int I=0; i {
    If (DBGrid - & gt; The Columns - & gt; The Items [I] - & gt; The Visible==true)
    {
    V=DBGrid - & gt; The Columns - & gt; The Items [I] - & gt; The Title - & gt; Caption;//FieldName;
    V=v + TAB;//the next column
    Fs - & gt; Write ((void *) v.c _str (), v.L ength ());
    }
    }
    //export field content
    Fs - & gt; Write (lf (void *). The c_str (), lf the Length ());//the next row

    For (int j=0; J & lt; ADOQuery - & gt; RecordCount. J++)
    {
    For (int I=0; i {
    If (DBGrid - & gt; The Columns - & gt; The Items [I] - & gt; The Visible==true)
    {
    V=ADOQuery - & gt; Fields - & gt; Fields [I] - & gt; AsString. The Trim ();
    V=v + TAB;//the next column
    Fs - & gt; Write ((void *) v.c _str (), v.L ength ());
    }
    }
    Fs - & gt; Write (lf (void *). The c_str (), lf the Length ());//the next row
    ADOQuery - & gt; The Next ();
    }
    Delete the fs.

    If (PreView)
    ShellExecute (NULL, "open", XlsFile c_str (), NULL, NULL, SW_SHOW);
    The else
    MessageBox (NULL, "export finished!" , "tip", MB_OK | MB_ICONINFORMATION);
    }

    CodePudding user response:

    With a third party plug-in DevExpress, inside CGrid control with the function of export to excel

    CodePudding user response:

    Yes, grideh also export function

    CodePudding user response:

    Ehlib

    CGrid

    Bring their own export function

    CodePudding user response:

    Sheet. H:
     
    # # ifndef SheetH
    # define SheetH

    nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
    • Related