13,825
社区成员
发帖
与我相关
我的任务
分享
class THH22RecordExportTool : public TForm
{
__published: // IDE-managed Components
TGroupBox *UART;
TLabel *Comm;
TLabel *Baudrate;
TComboBox *CommCBox;
TComboBox *BaudRateCBox;
TButton *OpenBtn;
TButton *CloseBtn;
TButton *FindCommBtn;
TMemo *Memo1;
TEdit *Edit1;
TGroupBox *GroupBox1;
TLabel *RecordLbl1;
TLabel *RecordLbl2;
TEdit *RecordNum;
TButton *GetInPKRecordBtn;
TButton *GetAddTimeRecordBtn;
TButton *GetOutPKRecordBtn;
TLabel *RecordLbl3;
TLabel *LblMid;
TEdit *RecordNum1;
TLabel *RecordLbl4;
TPanel *Panel1;
void __fastcall FindCommBtnClick(TObject *Sender);
void __fastcall OpenBtnClick(TObject *Sender);
void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
void __fastcall CloseBtnClick(TObject *Sender);
void __fastcall GetInPKRecordBtnClick(TObject *Sender);
void __fastcall GetAddTimeRecordBtnClick(TObject *Sender);
void __fastcall GetOutPKRecordBtnClick(TObject *Sender);
private: // User declarations
HANDLE g_hUartCommDll;
bool g_bUartOpen;
unsigned long g_MachineNum;
ST_CAR_INPARK_RECORD stCarInParkRecord;
ST_CAR_EXPARK_RECORD stCarExParkRecord;
ST_CARD_ADDTIME_RECORD stCardAddTimeRecord;
TURN_DATA tData;
AnsiString appPath;
AnsiString SavePath;
AnsiString SaveAsPath;
AnsiString xlsPath;
public: // User declarations
__fastcall THH22RecordExportTool(TComponent* Owner);
void __fastcall AddLog(bool bAddTime, UnicodeString str);
uchar8* _fastcall TimeNumToStr(uchar8* TimeBuf);
void __fastcall THH22RecordExportTool::AddInParkRecordToExl(void);
void __fastcall THH22RecordExportTool::AddADTimeRecordToExl(void);
void __fastcall THH22RecordExportTool::AddOutParkRecordToExl(void);
};
void __fastcall THH22RecordExportTool::AddInParkRecordToExl(void)
{
UnicodeString Ustr;
AnsiString Astr;
uchar8 strBuf[32] = {0};
SavePath = appPath + "Record\\";
SaveAsPath = appPath + "Record\\Files\\入场记录.xls";
if(!DirectoryExists(SavePath)) //确保路径的有效性
{
CreateDir(SavePath);
}
xlsPath = SavePath +"入场记录" +Now().FormatString("yyyymmdd") + ".xls";
if(!FileExists(xlsPath))
{
try
{
TStrings* lst = new TStringList();
Astr = "记录笔数\t用户卡号\t操作员卡号\t用户卡有效性\t用户卡类\t用户姓名(车牌)\t入场时间\t有效期";
lst->Add(Astr);
lst->SaveToFile(xlsPath);
delete lst;lst=0;
}
catch(...){}
}
Variant Ex,Wb,Sheet;
//建立Excel的Ole对象Ex
try
{
CoInitialize(NULL); // 解决下面运行出错的问题
Ex = Variant::CreateObject("Excel.Application");
}
catch(...)
{
ShowMessage("无法启动Excel");
return;
}
try
{
Ex.OlePropertySet("Visible",true); //设置Excel为不可见
Ex.OlePropertySet("DisplayAlerts",false); //关闭所有提示信息
Ex.OlePropertyGet("WorkBooks").OleProcedure("Open",xlsPath.c_str()); //打开指定的Excel报表文件。报表文件中最好设定只有一个Sheet。
Wb = Ex.OlePropertyGet("ActiveWorkBook");
Sheet = Wb.OlePropertyGet("ActiveSheet");//获得当前默认的Sheet
int iRows = 1;//记录行数
int iCols = 1;//记录列数
AnsiString s1;
while(1) //读取空行
{
s1 = Sheet.OlePropertyGet("Cells",iRows,iCols).OlePropertyGet("Value");
if(s1.IsEmpty())
{
break;
}
else
{
iRows++ ; //继续检查下一行
}
Sleep(100);
}
//卡号、操作员卡号 RecordNum->Text VerticalAlignment
Sheet.OlePropertyGet("Cells").OlePropertySet("NumberFormatLocal", "@"); // 设置数据 以文本形式存储
Sheet.OlePropertyGet("Cells").OlePropertySet("VerticalAlignment", 2); //设置文本居中对齐
Sheet.OlePropertyGet("Columns", 7).OlePropertySet("ColumnWidth", 30); //设置第7列列宽
Sheet.OlePropertyGet("Cells", 1, 7).OlePropertySet("ColumnWidth", 30);
Astr = RecordNum->Text;
Sheet.OlePropertyGet("Cells",iRows,iCols++).OlePropertySet("Value",Astr.c_str());
Sheet.OlePropertyGet("Cells",iRows,iCols++).OlePropertySet("Value",UIntToStr((uint32)stCarInParkRecord.u16CardNo).t_str());
Sheet.OlePropertyGet("Cells",iRows,iCols++).OlePropertySet("Value",UIntToStr((uint32)stCarInParkRecord.u16OptNo).t_str());
//卡有效性
if(0 == stCarInParkRecord.u8Valid)
{
Astr = "有效";
}
else
{
Astr = "无效";
}
Sheet.OlePropertyGet("Cells",iRows,iCols++).OlePropertySet("Value",Astr.c_str()); //有效性
//用户卡类
if(0 == stCarInParkRecord.u8CardType)
{
Astr = "临时卡";
}
else if(1 == stCarInParkRecord.u8CardType)
{
Astr = "月租卡";
}
Sheet.OlePropertyGet("Cells",iRows,iCols++).OlePropertySet("Value",Astr.c_str());
//用户姓名或车牌
Astr.sprintf("%s", stCarInParkRecord.Identity);
Sheet.OlePropertyGet("Cells",iRows,iCols++).OlePropertySet("Value",Astr.c_str());
//入场时间
memcpy(strBuf, TimeNumToStr(stCarInParkRecord.InTime), 32);
Astr.sprintf("%s", strBuf);
Sheet.OlePropertyGet("Columns", 7).OlePropertySet("ColumnWidth", 20); //设置第7列列宽
Sheet.OlePropertyGet("Cells",iRows,iCols++).OlePropertySet("Value",Astr.c_str());
//有效期
if(1 == stCarInParkRecord.u8CardType)
{
Astr.printf("20%02d-%02d-%02d",stCarInParkRecord.ValidityData[0],
stCarInParkRecord.ValidityData[1], stCarInParkRecord.ValidityData[2]);
Sheet.OlePropertyGet("Cells",iRows,iCols++).OlePropertySet("Value",Astr.c_str());
}
// Wb.OleProcedure("Save");//保存表格
// Sheet=Unassigned;
// Wb.OleProcedure("Close");//关闭表格
Wb.OleFunction("SaveAs", SaveAsPath.c_str());
// Ex.OleFunction("Quit");//退出Excel
//记录数加1,准备导出下一笔记录
Ustr = "SLV->导出第" + RecordNum->Text +"笔记录成功!";
int tNum = StrToInt(RecordNum->Text);
RecordNum->Text = IntToStr(++tNum);
AddLog(0, Ustr);
}
catch(...)
{
Application->MessageBox(L"Excel操作出错!", L"错误",MB_ICONSTOP|MB_OK);
}
}
Sheet.OlePropertyGet("Columns").OlePropertySet("ColumnWidth",28); // 设置所有列的列宽为28
Sheet.OlePropertyGet("Columns",3).OlePropertySet("ColumnWidth",68); // 设置第3列的列宽为28
Sheet.OlePropertyGet("Cells",1,1).OlePropertySet("HorizontalAlignment",3); // 设置指定单元格文字居中
if(!FileExists(xlsPath))
{
try
{
TStrings* lst = new TStringList();
Astr = "记录笔数\t用户卡号\t操作员卡号\t用户卡有效性\t用户卡类\t用户姓名(车牌)\t入场时间\t有效期";
lst->Add(Astr);
lst->SaveToFile(xlsPath);
delete lst;lst=0;
}catch(...){}
}
Variant Ex,Wb,Sheet;
//建立Excel的Ole对象Ex
try
{
CoInitialize(NULL); // 解决下面运行出错的问题
Ex = Variant::CreateObject("Excel.Application");
}
catch(...)
{
ShowMessage("无法启动Excel");
return;
}
try
{
Ex.OlePropertySet("Visible",false); //设置Excel为不可见
Ex.OlePropertySet("DisplayAlerts",false); //关闭所有提示信息
Ex.OlePropertyGet("WorkBooks").OleProcedure("Open",xlsPath.c_str()); //打开指定的Excel报表文件。报表文件中最好设定只有一个Sheet。
Wb = Ex.OlePropertyGet("ActiveWorkBook");
Sheet = Wb.OlePropertyGet("ActiveSheet");//获得当前默认的Sheet
int iRows = 1;//记录行数
int iCols = 1;//记录列数
AnsiString s1;
while(1) //读取空行
{
s1 = Sheet.OlePropertyGet("Cells",iRows,iCols).OlePropertyGet("Value");
if(s1.IsEmpty())
{
break;
}
else
{
iRows++ ; //继续检查下一行
}
Sleep(100);
}
//卡号、操作员卡号 RecordNum->Text VerticalAlignment
Sheet.OlePropertyGet("Columns", 7).OlePropertySet("ColumnWidth", 30); //设置第7列列宽
Sheet.OlePropertyGet("Cells").OlePropertySet("ColumnWidth", 30);
.h 相关头文件
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <ComCtrls.hpp>
#include <CheckLst.hpp>
#include <Dialogs.hpp>
#include <IniFiles.hpp>
#include <FileCtrl.hpp>
#include <Comobj.hpp>
#include <OleServer.hpp>
是少包含什么头文件吗?头大啊 Sheet.OlePropertyGet("Columns", 7).OlePropertySet("ColumnWidth", 20); //设置第7列列宽
这是代码,怎么回事不行