Import data in DBGRID into Excel

xiaoxiao2021-03-06  40

Import data in dbgrid into Excel, share: void __fastcall tsearch :: DBGRIDTOEXCEL (TDBGRID * AGRID, ANSISTRING TITLE) // Export from DBGRID to Excel {if (agrid-> DataSource-> DataSet-> iSempty ()) { ShowMessage ( "no data needs to be exported");} else {AGrid-> DataSource-> DataSet-> DisableControls (); TBookmark bmkDataSet = AGrid-> DataSource-> DataSet-> GetBookmark (); try {try {Variant vExcel = CreateOleObject ( "Excel.Application"); vExcel.OlePropertySet ( "Visible", false); Variant vWorkbooks = vExcel.OlePropertyGet ( "Workbooks"); Variant vWorkbook = vWorkbooks.OleFunction ( "Add"); Variant vSheets = vWorkbook.OlePropertyGet ( "Sheets"); variant vcells = vexcel.olepropertyget ("cells");

INT K = 0; for (int i = 0; i columns-> count; i ) {// This loop output title IF (agrid-> columns-> items [i] -> visible) {k ; vcells.olepropertyget ("cells", 1, k) .olePropertyset ("Value", Agrid-> Column-> items [i] -> title-> caption.c_str ()); //vsheets.olepropertyget ("columns " K) .olePropertyget ("ColumnWidth"). OlepropertySet ("Value", // Agrid-> Fields [K - 1] -> DisplayWidth);}} Agrid-> DataSource-> DataSet-> first (); variant vtext = VararrayCreate (OpenArray (Int, (1, Agrid-> DataSource-> Dataset-> Recordcount, 1, Agrid-> Column-> count), varvariant; int = 1; while (! Agrid-> datasource-> DataSet-> EOF) {INT ICOL = 0; for (int J = 1; j <= agrid-> columns-> count; j ) {if (agrid-> columns-> items [j-1] -> visible) {ICOL ; //vtext.getlement (IROW ,ICOL ).ChangeType(V ARSTRING); if (agrid-> columns-> items [j-1] -> Field-> DataType == fTString) // If the character is output, use the Lotus 'Vtelt.putelement ("'" agid-> Columns-> items [J-1] -> Field-> Displaytext, IROW, ICOL); Else vText.putelement (agrid-> columns-> items [j-1] -> Field-> DisplayText, iRow, ICOL); //vcells.olepropertyget ("cells" ,row, childol ).olepropertyset ("NumberformatLocal ", "@");

}}} IRow ; Application-> ProcessMess (); agrid-> datasource-> dataset-> next ();} vcells.olepropertyget ("Range", vcells.olepropertyget ("Item", 2, 1), vCells. OLEPROPERTYGET ("item", agrid-> datasource-> dataset-> recordcount 1, agrid-> columns-> count). OLEPROPERTYSET ("Value", vtext);

VEXCEL.OLEPROPERTYSET ("Visible", true);} catch (exception & e) {showMessage (E.MESSAGE "/ n may not be installed in the system); // Application-> MessageBox (" System No EXCEL ", Application -> Title.c_Str (), MB_ICONITIONSTRITION;}} __final {if (bmkdataset) {agrid-> datasource-> dataset-> gotobookmark (bmkdataset); agrid-> datasource-> dataset-> freebookmark (BMKDataSet);} AGRID -> DataSource-> DataSet-> EnableControls ();}}} Export the queryed database record to the Excel file #include

#pragma hdrstop # include #include

转载请注明原文地址:https://www.9cbs.com/read-71178.html

New Post(0)