Code
if(SaveDialog1->Execute()==false)
{
MessageBox(NULL,"请选择文件!","提示",MB_OK|MB_ICONEXCLAMATION);
return;
}
int nFileHandle=FileCreate(SaveDialog1->FileName);
AnsiString m_FileName;
if(FileExists(SaveDialog1->FileName)&&(MessageBox(NULL,"文件重复,是否替换?","提示",MB_YESNO|MB_ICONEXCLAMATION)==IDNO))
{
return;//m_FileName=SaveDialog1->FileName;
}
else
{
m_FileName=SaveDialog1->FileName;
//MessageBox(NULL,"文件不存在!","捍卫者",MB_OK|MB_ICONERROR);
//return;
}
FileClose(nFileHandle);
Variant Ex,Wb,Sh1;
//String m_FileName="d:\test.xls";
try
{
Ex=Variant::CreateObject("Excel.Application");
}
catch()
{
MessageBox(NULL,"启动Excel出错,可能没有安装Excel","捍卫者",MB_OK|MB_ICONERROR);
Ex=Unassigned;
return;
}
Ex.OlePropertySet("Visible",false); //设置不可见
Ex.OlePropertySet("DisplayAlerts",false); //设置不可弹出对话框
//Ex.OlePropertyGet("WorkBooks").OleProcedure("Open",SaveDialog1->FileName.c_str());
Ex.OlePropertyGet("WorkBooks").OleFunction("Add",1); //添加一个sheet
Sh1=Ex.OlePropertyGet("ActiveWorkBook").OlePropertyGet("ActiveSheet"); //获取默认的sheet
Sh1.OleProcedure("Select"); //选择sheet
Sh1.OlePropertyGet("Cells").OleProcedure("Select"); //设置celles,列选择
Ex.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Size",ListView1->Font->Size); //设置字体大小
Ex.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Name",ListView1->Font->Name.c_str()); //设置第一行,标题
Ex.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("FontStyle","常规"); //设置字体样式
Sh1.OlePropertyGet("Cells",1,1).OleProcedure("Select"); //选择表1,1
int nRowCount(ListView1->Items->Count+1);
nRowCount=nRowCount<2?2:nRowCount;
int nColCount(ListView1->Columns->Count);
nColCount=nColCount<1?1:nColCount;
for(int i=0;i<nColCount;i++)
{
int nColWidth;
if(ListView1->Columns->Count>i)
{
nColWidth=ListView1->Columns->Items[i]->Width;
}
else
{
nColWidth=100;
}
Ex.OlePropertyGet("Columns",i+1).OlePropertySet("ColumnWidth",nColWidth); //设置列宽
}
for(int j=0;j<ListView1->Columns->Count;j++)
{
Ex.OlePropertyGet("Rows",1).OlePropertySet("RowHeight",20); //设置行高
Sh1.OlePropertyGet("Cells",1,j+1).OlePropertySet("Value",ListView1->Columns->Items[j]->Caption.c_str()); //添加内容
Variant vInter=Sh1.OlePropertyGet("Cells",1,j+1).OlePropertyGet("Interior"); //设置
vInter.OlePropertySet("ColorIndex",15); //设置颜色
vInter.OlePropertySet("Pattern",1); //设置样式
vInter.OlePropertySet("PatternColorIndex",-4105);
}
for(int i=0;i<nRowCount-1;i++)
{
Ex.OlePropertyGet("Rows",i+2).OlePropertySet("RowHeight",16);
Sh1.OlePropertyGet("Cells",i+2,1).OlePropertySet("Value",ListView1->Items->Item[i]->Caption.c_str());
for(int j=0;j<ListView1->Items->Item[i]->SubItems->Count;j++)
{
Sh1.OlePropertyGet("Cells",i+2,j+2).OlePropertySet("Value",ListView1->Items->Item[i]->SubItems->Strings[j].c_str());
}
}
Ex.OlePropertyGet("ActiveWorkbook").OleProcedure("SaveAs",m_FileName.c_str()); //另存
//Ex.OlePropertyGet("ActiveWorkBook").OleProcedure("Save");
Ex.OlePropertyGet("ActiveWorkBook").OleProcedure("Close"); //关闭sheet
Ex.OleFunction("Quit"); //退出excel
Sh1=Unassigned; //释放
MessageBox(NULL,"导出成功","捍卫者",MB_OK|MB_ICONINFORMATION);
网上找了些资料,才搞成这个玩意,感觉很麻烦,但是很有成就感。大家自己看看,无非4个函数,来回调用excel的属性,方法,过程。if(SaveDialog1->Execute()==false)
{
MessageBox(NULL,"请选择文件!","提示",MB_OK|MB_ICONEXCLAMATION);
return;
}
int nFileHandle=FileCreate(SaveDialog1->FileName);
AnsiString m_FileName;
if(FileExists(SaveDialog1->FileName)&&(MessageBox(NULL,"文件重复,是否替换?","提示",MB_YESNO|MB_ICONEXCLAMATION)==IDNO))
{
return;//m_FileName=SaveDialog1->FileName;
}
else
{
m_FileName=SaveDialog1->FileName;
//MessageBox(NULL,"文件不存在!","捍卫者",MB_OK|MB_ICONERROR);
//return;
}
FileClose(nFileHandle);
Variant Ex,Wb,Sh1;
//String m_FileName="d:\test.xls";
try
{
Ex=Variant::CreateObject("Excel.Application");
}
catch()
{
MessageBox(NULL,"启动Excel出错,可能没有安装Excel","捍卫者",MB_OK|MB_ICONERROR);
Ex=Unassigned;
return;
}
Ex.OlePropertySet("Visible",false); //设置不可见
Ex.OlePropertySet("DisplayAlerts",false); //设置不可弹出对话框
//Ex.OlePropertyGet("WorkBooks").OleProcedure("Open",SaveDialog1->FileName.c_str());
Ex.OlePropertyGet("WorkBooks").OleFunction("Add",1); //添加一个sheet
Sh1=Ex.OlePropertyGet("ActiveWorkBook").OlePropertyGet("ActiveSheet"); //获取默认的sheet
Sh1.OleProcedure("Select"); //选择sheet
Sh1.OlePropertyGet("Cells").OleProcedure("Select"); //设置celles,列选择
Ex.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Size",ListView1->Font->Size); //设置字体大小
Ex.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("Name",ListView1->Font->Name.c_str()); //设置第一行,标题
Ex.OlePropertyGet("Selection").OlePropertyGet("Font").OlePropertySet("FontStyle","常规"); //设置字体样式
Sh1.OlePropertyGet("Cells",1,1).OleProcedure("Select"); //选择表1,1
int nRowCount(ListView1->Items->Count+1);
nRowCount=nRowCount<2?2:nRowCount;
int nColCount(ListView1->Columns->Count);
nColCount=nColCount<1?1:nColCount;
for(int i=0;i<nColCount;i++)
{
int nColWidth;
if(ListView1->Columns->Count>i)
{
nColWidth=ListView1->Columns->Items[i]->Width;
}
else
{
nColWidth=100;
}
Ex.OlePropertyGet("Columns",i+1).OlePropertySet("ColumnWidth",nColWidth); //设置列宽
}
for(int j=0;j<ListView1->Columns->Count;j++)
{
Ex.OlePropertyGet("Rows",1).OlePropertySet("RowHeight",20); //设置行高
Sh1.OlePropertyGet("Cells",1,j+1).OlePropertySet("Value",ListView1->Columns->Items[j]->Caption.c_str()); //添加内容
Variant vInter=Sh1.OlePropertyGet("Cells",1,j+1).OlePropertyGet("Interior"); //设置
vInter.OlePropertySet("ColorIndex",15); //设置颜色
vInter.OlePropertySet("Pattern",1); //设置样式
vInter.OlePropertySet("PatternColorIndex",-4105);
}
for(int i=0;i<nRowCount-1;i++)
{
Ex.OlePropertyGet("Rows",i+2).OlePropertySet("RowHeight",16);
Sh1.OlePropertyGet("Cells",i+2,1).OlePropertySet("Value",ListView1->Items->Item[i]->Caption.c_str());
for(int j=0;j<ListView1->Items->Item[i]->SubItems->Count;j++)
{
Sh1.OlePropertyGet("Cells",i+2,j+2).OlePropertySet("Value",ListView1->Items->Item[i]->SubItems->Strings[j].c_str());
}
}
Ex.OlePropertyGet("ActiveWorkbook").OleProcedure("SaveAs",m_FileName.c_str()); //另存
//Ex.OlePropertyGet("ActiveWorkBook").OleProcedure("Save");
Ex.OlePropertyGet("ActiveWorkBook").OleProcedure("Close"); //关闭sheet
Ex.OleFunction("Quit"); //退出excel
Sh1=Unassigned; //释放
MessageBox(NULL,"导出成功","捍卫者",MB_OK|MB_ICONINFORMATION);
OlePropertyGet //获取
OlePropertySet //设置
OleProcedure //调用一个过程
OleFunction //函数