13,871
社区成员




void CreateExcelFiles(AnsiString filename)
{
Variant vExcelApp, vSheet;
try
{
CoInitialize(NULL);
vExcelApp = Variant::CreateObject("Excel.Application");
}
catch(...)
{
MessageBox(0, "启动 Excel 出错, 可能是没有安装Excel.",
"WARNING", MB_OK | MB_ICONERROR);
vExcelApp = Unassigned;
return;
}
// 隐藏Excel界面
vExcelApp.OlePropertySet("Visible", false);
// 新建一个工作表
vExcelApp.OlePropertyGet("Workbooks").OleFunction("Add", 1); // 工作表
// 操作这个工作表
vSheet = vExcelApp.OlePropertyGet("ActiveWorkbook")
.OlePropertyGet("Sheets", 1);
// 表格的列数
int nColCount(8);
// 设置单元格的宽度
for(int i=0; i<nColCount; i++)
{
int nColWidth = 28;
vExcelApp.OlePropertyGet("Columns", i + 1)
.OlePropertySet("ColumnWidth", nColWidth / 2.6);
}
......//其他代码
vExcelApp.OlePropertyGet("ActiveWorkbook")
.OleFunction("SaveAs", filename.c_str());
vExcelApp.OleFunction("Quit");
vSheet = Unassigned;
vExcelApp = Unassigned;
CoUninitialize();
}
void AddData(AnsiString filename)
{
Variant vExcelApp, vSheet;
try
{
// CoInitialize(NULL);
vExcelApp = Variant::CreateObject("Excel.Application"); //这里出现问题
}
catch(...)
{
MessageBox(0, "启动 Excel 出错, 可能是没有安装Excel.",
"WARNING", MB_OK | MB_ICONERROR);
vExcelApp = Unassigned;
return;
}
// 隐藏Excel界面
vExcelApp.OlePropertySet("Visible", false);
vExcelApp.OlePropertyGet("workbooks").OleFunction("open",filename.c_str());
// 操作这个工作表
vSheet = vExcelApp.OlePropertyGet("ActiveWorkbook")
.OlePropertyGet("Sheets", 1);
int nRow = vSheet.OlePropertyGet("UsedRange").OlePropertyGet("Rows")
.OlePropertyGet("Count") + 1;
......//这里是插入数据代码
vExcelApp.OlePropertyGet("ActiveWorkbook").OleFunction("Save");
vExcelApp.OlePropertySet("DisplayAlerts",false);
vExcelApp.OleFunction("Quit");
vSheet = Unassigned;
vExcelApp = Unassigned;
// CoUninitialize();
}
void TfrmMain::CreateExcelFiles(const int Port)
{
try
{
CoInitialize(NULL);
m_vExcelApp[Port] = Variant::CreateObject("Excel.Application");
}
catch(...)
{
MessageBox(0, "启动 Excel 出错, 可能是没有安装Excel.",
"WARNING", MB_OK | MB_ICONERROR);
m_vExcelApp[Port] = Unassigned;
return;
}
// 隐藏Excel界面
m_vExcelApp[Port].OlePropertySet("Visible", false);
// 新建一个工作表
m_vExcelApp[Port].OlePropertyGet("Workbooks").OleFunction("Add", 1); // 工作表
// 操作这个工作表
m_vWorkBook[Port] = m_vExcelApp[Port].OlePropertyGet("ActiveWorkbook");
m_vSheet[Port] = m_vWorkBook[Port].OlePropertyGet("Sheets", 1);
// 表格的列数
int nColCount(8);
// 设置单元格的宽度
for(int i=0; i<nColCount; i++)
{
int nColWidth = 28;
m_vExcelApp[Port].OlePropertyGet("Columns", i + 1)
.OlePropertySet("ColumnWidth", nColWidth / 2.6);
}
// 第一行写入测试相关信息
m_vExcelApp[Port].OlePropertyGet("Rows", 1).OlePropertySet("RowHeight", 18);
Variant Range1 = m_vSheet[Port].OlePropertyGet("Range","A1:D1");
Range1.OleFunction("Merge",false);
AnsiString Str = GetCurData().c_str();
Str = "当前操作时间: "+Str;
m_vSheet[Port].OlePropertyGet("Cells", 1, 1)
.OlePropertySet("Value", Str.c_str());
// 先将列名写入Excel表格
const char* Name[18] = {"test"};
for(int j=0; j<18; j++)
{
// 标题行的行高
m_vExcelApp[Port].OlePropertyGet("Rows", 2).OlePropertySet("RowHeight", 15);
// 写入标题
m_vSheet[Port].OlePropertyGet("Cells", 2, j + 1)
.OlePropertySet("Value",Name[j]);
// 设置列名单元格的背景色
Variant vInter = m_vSheet[Port].OlePropertyGet(
"Cells", 2, j + 1).OlePropertyGet("Interior");
vInter.OlePropertySet("ColorIndex", 15); // 灰色
vInter.OlePropertySet("Pattern", 1); // xlSolid
vInter.OlePropertySet("PatternColorIndex", -4105); // xlAutomatic
}
}
//这个函数是每接到一包解析正确的数据后,就调用它保存数据
void TfrmMain::AddData(const int Port)
{
//调试到这里的时候,就提示错误。
//EOleSysError with message'因为应用程序正在发送一个输入同步呼叫,所以无法执行传出的呼叫'.
int nRow = m_vSheet[Port].OlePropertyGet("UsedRange").OlePropertyGet("Rows")
.OlePropertyGet("Count") + 1;
int nCount = 1 + nRow;
TListItem *Item = lvTable->Items->operator [](m_ItemIndex[t0]);
if( NULL == Item ) return;
AnsiString Caption = Item->Caption;
m_vExcelApp[Port].OlePropertyGet("Rows", nCount).OlePropertySet("RowHeight", 16);
m_vSheet[Port].OlePropertyGet("Cells", nCount, 1)
.OlePropertySet("Value", Caption.c_str());
for(int j=0; j<Item->SubItems->Count; j++)
{
AnsiString tmp = Item->SubItems->Strings[j];
if( j == 0 ) tmp = "'" + tmp;
m_vSheet[Port].OlePropertyGet("Cells", nCount, j + 2)
.OlePropertySet("Value",tmp.c_str());
}
}
//数据收发完毕后,关闭EXCEL,保存数据。
void TfrmMain::CloseExcelSave(const int Port)
{
// 保存Excel文档并退出
AnsiString filename = m_FileName;
filename = filename.SubString(1,filename.Length()-4);
filename = filename + "_" + IntToStr(Port);
filename = filename + ".xls";
m_vExcelApp[Port].OlePropertyGet("ActiveWorkbook")
.OleFunction("SaveAs", filename.c_str());
m_vWorkBook[Port].OleProcedure("Close");
m_vExcelApp[Port].OleFunction("Quit");
m_vSheet[Port] = Unassigned;
m_vExcelApp[Port] = Unassigned;
CoUninitialize();
}
真不知道问题在哪里了,麻烦大侠们给指点下迷津,同时也指导下调试方法,万分感谢。
void AddData(AnsiString filename)
{
//这里设置断点后,提示Hr类型不匹配错误。
m_vExcelApp[Port].OlePropertyGet("workbooks").OleFunction("open",filename.c_str());
int nRow = m_vSheet[Port].OlePropertyGet("UsedRange").OlePropertyGet("Rows")
.OlePropertyGet("Count") + 1;
......//数据添加代码
}
Void CloseExcelSave()
{
m_vExcelAppOlePropertyGet("ActiveWorkbook").OleFunction("Save");
m_vExcelAppOlePropertySet("DisplayAlerts",false);
m_vExcelAppOleFunction("Quit");
m_vSheet = Unassigned;
m_vExcelApp = Unassigned;
}