(中行雷威2018.8.12)
(同一个世界,同一个梦想,交流学习C++Builder XE10,传承c++builder的魅力!欢迎各地朋友加入我的QQ群484979943,进群密码“BCB”,同时也请将该群号广为宣传,希望能够广集各方高手,共同进步。如需下载开发工具及源代码请加入我的QQ群。)
【阅读倡议】
1、有问题请留言;
2、没问题请点赞;
3、看连载请加群;
4、下源码请加群;
【开发工具】
1、C++Builder10.2.3tokyo
2、FMSoft_uniGUI_Complete_Professional_1.10.0build1472(正版)
3、TMS FlexCel for VCL & FMX 6.21.0.0 XE10.2(破解版,下载https://download.csdn.net/download/dlboy2018/10598597,或到我的QQ群里的B10目录下载)
![](/qrcode.jpg)
本例子将详细讲解如何通过FlexCel控件实现EXCEL表格的生成和下载,本文讲三个案例,一个是VCL下如何生成表格并打开,一个是UNIGUI下如何生成表格并下载,第三个是UNIGUI下如何将UniQuery的查询结果保存并下载到本地。本教程的源代码请到我的QQ群共享文件A02-源代码里下载。
第一部分、VCL下案例
FlexCel的安装很简单,直接setup就可以了,我设置的安装目录是C:\TMSSoftware,安装后C:\TMSSoftware\FlexCelVCLNT\Demo\cpp下是演示案例,C:\TMSSoftware\FlexCelVCLNT\Documentation目录下是帮助文件,Examples-》c++ -》是c版的案例和代码。
一、创建一个VCL项目
创建一个项目,在FormMain主窗口中添加一个按钮,放置一个SaveDialog保存文件控件,即可。
1、UnitMain.h文件
//---------------------------------------------------------------------------
#ifndef UnitMainH
#define UnitMainH
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.Buttons.hpp>
#include <Vcl.Dialogs.hpp>
/*自定义*/
#include <VCL.FlexCel.Core.hpp>
#include <FlexCel.XlsAdapter.hpp>
//---------------------------------------------------------------------------
class TFormMain : public TForm
{
__published: // IDE-managed Components
TBitBtn *BitBtn1;
TSaveDialog *SaveDialog1;
void __fastcall BitBtn1Click(TObject *Sender);
void __fastcall FormCreate(TObject *Sender);
/*自定义*/
void __fastcall writeToExcel(TExcelFile *xls);//写表格文件
void __fastcall saveToExcel(TExcelFile *xls); //存表格文件
private: // User declarations
public: // User declarations
__fastcall TFormMain(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFormMain *FormMain;
//---------------------------------------------------------------------------
#endif
2、UnitMain.cpp文件
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "UnitMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::BitBtn1Click(TObject *Sender)
{
TExcelFile *xls = new TXlsFile(true);
__try
{
writeToExcel(xls);//写表格
saveToExcel(xls);//存表格
}
__finally
{
delete xls;
}
//
}
// ---------------------------------------------------------------------------
void __fastcall TFormMain::writeToExcel(TExcelFile *xls)
{
int rowCount=5,colCount=3;
xls->NewFile(1);//创建一个表格
// 写内容
for(int i=1;i<=rowCount;i++)
for(int j=1;j<colCount;j++)
{
xls->SetCellValue(i, j, TCellValue::Create((double)i*j));
}
xls->PrintLandscape = true;
}
//-----------------------------------------------------------------------------
void __fastcall TFormMain::saveToExcel(TExcelFile *xls)
{
if (!SaveDialog1->Execute())
return;
xls->Save(SaveDialog1->FileName);//保存表格
if (MessageDlg("打开生成的表格文件看看不?", mtConfirmation,
TMsgDlgButtons() << mbYes << mbNo, 0) == mrYes)
{
ShellExecute(0, L"open", SaveDialog1->FileName.c_str(), NULL, NULL,SW_SHOWNORMAL);
}
}
//-----------------------------------------------------------------------------
void __fastcall TFormMain::FormCreate(TObject *Sender)
{
SaveDialog1->DefaultExt="xlsx";
SaveDialog1->Filter="表格文件(*.xlsx)|*.xlsx|表格文件(*.xls)|*.xls|所有文件(*.*)|*.*" ;
}
//---------------------------------------------------------------------------
第二部分、UNIGUI下案例
一、创建一个UniGui项目
只需要放置一个UniBitBtn按钮即可
1、Main.h文件
//---------------------------------------------------------------------------
#ifndef MainH
#define MainH
#include "uniBitBtn.hpp"
#include "uniButton.hpp"
#include "uniGUIBaseClasses.hpp"
#include "uniGUIClasses.hpp"
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
/*自定义*/
#include <VCL.FlexCel.Core.hpp>
#include <FlexCel.XlsAdapter.hpp>
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <uniGUIForm.hpp>
#include <uniGUIRegClasses.hpp>
#include "uniBitBtn.hpp"
#include "uniButton.hpp"
#include "uniGUIBaseClasses.hpp"
#include "uniGUIClasses.hpp"
//---------------------------------------------------------------------------
class TMainForm : public TUniForm
{
__published: // IDE-managed Components
TUniBitBtn *UniBitBtn1;
/*自定义*/
void __fastcall writeToExcel(TExcelFile *xls);//写表格
void __fastcall saveToExcel(TExcelFile *xls); //存表格
void __fastcall UniBitBtn1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TMainForm(TComponent* Owner);
};
//---------------------------------------------------------------------------
TMainForm *MainForm(void);
//---------------------------------------------------------------------------
#endif
2、Main.cpp文件
//---------------------------------------------------------------------------
#include <vcl.h>
#include <uniGUIVars.hpp>
#pragma hdrstop
#include "Main.h"
#include "MainModule.h"
#include "ServerModule.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "uniBitBtn"
#pragma link "uniButton"
#pragma link "uniGUIBaseClasses"
#pragma link "uniGUIClasses"
#pragma link "uniBitBtn"
#pragma link "uniButton"
#pragma link "uniGUIBaseClasses"
#pragma link "uniGUIClasses"
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
TMainForm *MainForm(void)
{
return( (TMainForm *)UniMainModule()->GetFormInstance(__classid(TMainForm)));
}
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TUniForm(Owner)
{
}
//---------------------------------------------------------------------------
void TE39ED013A2244DC798ABA9F61211176E__RegisterFormClass() {
Uniguivars::RegisterAppFormClass(__classid(TMainForm));
Uniguiregclasses::CPPInit();
}
#pragma startup TE39ED013A2244DC798ABA9F61211176E__RegisterFormClass
void __fastcall TMainForm::UniBitBtn1Click(TObject *Sender)
{
TExcelFile *xls = new TXlsFile(true);
__try
{
writeToExcel(xls);//写表格
saveToExcel(xls);//存表格
}
__finally
{
delete xls;
}
//
}
//---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
void __fastcall TMainForm::writeToExcel(TExcelFile *xls)
{
int rowCount=5,colCount=3;
xls->NewFile(1);//创建一个表格
// Set some cell values.
for(int i=1;i<=rowCount;i++)
for(int j=1;j<colCount;j++)
{
xls->SetCellValue(i, j, TCellValue::Create((double)i*j));//写表格内容
}
xls->PrintLandscape = true;
}
//-----------------------------------------------------------------------------
void __fastcall TMainForm::saveToExcel(TExcelFile *xls)
{
/*保存表格
unigui默认的文件操作目录在项目运行同级目录下的files目录下,
因此文件要保存在该目录下,可以在该目录下再创建多级子目录,
本例子为D:\flexCel_uniGui\Win32\Debug\files
*/
AnsiString destPath,destFile,tempFile;
tempFile="测试表格.xlsx";
destPath=UniServerModule()->FilesFolder+"我的表格\\临时文件";//需要在开头#include "ServerModule.h"
//强制在服务端生成临时文件保存目录
if(!DirectoryExists(destPath))
{
ForceDirectories(destPath.c_str());
}
destFile=destPath+"\\"+tempFile;
//保存表格到服务器端的临时目录
xls->Save(destFile);
//浏览器端发起下载表格
UniSession->SendFile(destFile);
}
//-----------------------------------------------------------------------------
第三部分、UNIGUI下将UniQuery结果保存成表格并下载
本例子中我的窗口名字叫UniformFreeQuery,本窗口是一个复杂的查询窗口,后续代码仅讲解如何将一个UniQuery的结果集保存到表格里面,窗口必须的是一个UniQuery控件,一个SaveDialog控件。
1、UnitFreeQuery.h代码
//---------------------------------------------------------------------------
#ifndef UnitFreeQueryH
#define UnitFreeQueryH
#include "DADump.hpp"
#include "DBAccess.hpp"
#include "MemDS.hpp"
#include "Uni.hpp"
#include "uniBasicGrid.hpp"
#include "uniBitBtn.hpp"
#include "uniButton.hpp"
#include "uniComboBox.hpp"
#include "uniDBGrid.hpp"
#include "UniDump.hpp"
#include "uniGUIBaseClasses.hpp"
#include "uniGUIClasses.hpp"
#include "uniLabel.hpp"
#include "uniListBox.hpp"
#include "uniMemo.hpp"
#include "uniMultiItem.hpp"
#include "uniPageControl.hpp"
#include "uniPanel.hpp"
#include "uniSplitter.hpp"
#include <Data.DB.hpp>
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.Forms.hpp>
#include <Vcl.Dialogs.hpp>
/*自定义*/
#include <VCL.FlexCel.Core.hpp>
#include <FlexCel.XlsAdapter.hpp>
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <uniGUIForm.hpp>
#include "uniBasicGrid.hpp"
#include "uniBitBtn.hpp"
#include "uniButton.hpp"
#include "uniComboBox.hpp"
#include "uniDBGrid.hpp"
#include "uniGUIBaseClasses.hpp"
#include "uniGUIClasses.hpp"
#include "uniLabel.hpp"
#include "uniListBox.hpp"
#include "uniMemo.hpp"
#include "uniMultiItem.hpp"
#include "uniPageControl.hpp"
#include "uniPanel.hpp"
#include "DBAccess.hpp"
#include "MemDS.hpp"
#include "Uni.hpp"
#include <Data.DB.hpp>
#include "uniSplitter.hpp"
#include "DADump.hpp"
#include "UniDump.hpp"
//---------------------------------------------------------------------------
class TUniFormFreeQuery : public TUniForm
{
__published: // IDE-managed Components
TUniPageControl *UniPageControl1;
TUniTabSheet *UniTabSheet1;
TUniBitBtn *UniBitBtn4;
TUniBitBtn *UniBitBtn6;
TUniButton *UniButton1;
TUniBitBtn *UniBitBtn7;
TUniBitBtn *UniBitBtn8;
TUniQuery *UniQuery1;
TUniQuery *UniQuery2;
TUniQuery *UniQuery3;
TUniDataSource *UniDataSource1;
TUniContainerPanel *UniContainerPanel7;
TUniComboBox *UniComboBox3;
TUniLabel *UniLabel3;
TUniLabel *UniLabel2;
TUniLabel *UniLabel1;
TUniComboBox *UniComboBox1;
TUniComboBox *UniComboBox2;
TUniBitBtn *UniBitBtn1;
TUniBitBtn *UniBitBtn2;
TUniBitBtn *UniBitBtn3;
TUniContainerPanel *UniContainerPanel8;
TUniContainerPanel *UniContainerPanel9;
TUniListBox *UniListBox1;
TUniListBox *UniListBox2;
TUniPageControl *UniPageControl2;
TUniTabSheet *UniTabSheet2;
TUniContainerPanel *UniContainerPanel1;
TUniDBGrid *UniDBGrid1;
TUniContainerPanel *UniContainerPanel2;
TUniMemo *UniMemo1;
TUniContainerPanel *UniContainerPanel4;
TUniBitBtn *UniBitBtn9;
TUniBitBtn *UniBitBtn10;
TUniBitBtn *UniBitBtn11;
TUniBitBtn *UniBitBtn5;
TUniMemo *UniMemo2;
TUniLabel *UniLabel4;
TUniLabel *UniLabel5;
TUniBitBtn *UniBitBtn12;
TUniSplitter *UniSplitter1;
TSaveDialog *SaveDialog1;
void __fastcall UniFormBeforeShow(TObject *Sender);
void __fastcall UniComboBox1Select(TObject *Sender);
void __fastcall UniComboBox2Select(TObject *Sender);
void __fastcall UniComboBox3Select(TObject *Sender);
void __fastcall UniBitBtn4Click(TObject *Sender);
void __fastcall UniBitBtn5Click(TObject *Sender);
void __fastcall UniBitBtn11Click(TObject *Sender);
void __fastcall UniBitBtn8Click(TObject *Sender);
void __fastcall UniBitBtn7Click(TObject *Sender);
void __fastcall UniBitBtn6Click(TObject *Sender);
void __fastcall UniButton1Click(TObject *Sender);
void __fastcall UniBitBtn1Click(TObject *Sender);
void __fastcall UniBitBtn2Click(TObject *Sender);
void __fastcall UniBitBtn3Click(TObject *Sender);
void __fastcall UniBitBtn9Click(TObject *Sender);
void __fastcall UniBitBtn12Click(TObject *Sender);
/*自定义*/
void __fastcall saveToExcel(TExcelFile *xls); //存表格
void __fastcall UniBitBtn10Click(TObject *Sender);
void __fastcall UniFormCreate(TObject *Sender);
private: // User declarations
String mySql,rowId,tmpStr,tmpStr1,tmpStr2;
public: // User declarations
__fastcall TUniFormFreeQuery(TComponent* Owner);
};
//---------------------------------------------------------------------------
#endif
2、UniFreeQuery.cpp代码
void __fastcall TUniFormFreeQuery::UniBitBtn10Click(TObject *Sender)
{
TExcelFile *xls = new TXlsFile(true);
__try
{
saveToExcel(xls);
}
__finally
{
delete xls;
}
}
//---------------------------------------------------------------------------
void __fastcall TUniFormFreeQuery::saveToExcel(TExcelFile *xls)
{
AnsiString destPath,destFile,tempFile;
int rowCount,colCount,i,j;
if(!UniQuery1->Active)
{
ShowMessage("先查询,再导出!");
return;
}
if(UniQuery1->RecordCount<1)
{
ShowMessage("没有记录,取消导出!");
return;
}
//
xls->NewFile(1);
rowCount=UniQuery1->RecordCount;
colCount=UniQuery1->FieldCount;
//写列头
i=1;
for(j=1;j<colCount;j++)
{
xls->SetCellValue(i, j, TCellValue::Create(UniQuery1->Fields->FieldByNumber(j)->FieldName));
}
//写数据
i=2;
while(!UniQuery1->Eof)
{
for(j=1;j<colCount;j++)
{
xls->SetCellValue(i, j, TCellValue::Create(UniQuery1->Fields->FieldByNumber(j)->AsString));
}
UniQuery1->Next();
i++;
}
xls->PrintLandscape = true;
tempFile="定制导出.xlsx";
destPath=UniServerModule()->FilesFolder+"临时文件\\下载表格";
//强制在服务端生成临时文件保存目录
if(!DirectoryExists(destPath))
{
ForceDirectories(destPath.c_str());
}
destFile=destPath+"\\"+tempFile;
//保存表格到服务器端的临时目录
xls->Save(destFile);
//选择本地保存目录和文件名
if(!SaveDialog1->Execute() )
return;
//浏览器端将服务器端默认临时表格文件下载保存成本地新名字
UniSession->SendFile(destFile,SaveDialog1->FileName);
}
//-----------------------------------------------------------------------------
void __fastcall TUniFormFreeQuery::UniFormCreate(TObject *Sender)
{
SaveDialog1->DefaultExt="xlsx";
SaveDialog1->Filter="表格文件(*.xlsx)|*.xlsx|表格文件(*.xls)|*.xls|所有文件(*.*)|*.*" ;
}
//---------------------------------------------------------------------------
FlexCel的表格功能很强大,需要的兄弟姐妹可以看起安装目录下的官方文档和案例。