如何将数据导入Excel,将Excel的行复制一行
Excel的格式已经固定好,将数据导入Excel时,如何将Excel的行复制一行并插入(如将Excel的第5行复制并插入成第6行,原第6行自动下移)
[解决办法]
void __fastcall TForm1::Button1Click(TObject *Sender)
{
WideString theXls;
theXls=ExtractFilePath(Application-> ExeName) + "yourExcel.xls ";
if(!FileExists(theXls)){ShowMessage( "当前目录下yourExcel.xls不存在文件 ");return;}
TCOM_Application iExcelApp; //#include "Excel_2K_SRVR.h "
try{iExcelApp=CoExcelApplication::Create();}
catch(...) {ShowMessage( "启动 Excel 出错, 大概是没装Excel ");return;}
iExcelApp-> set_Visible(0,true);
Workbooks *ibooksPtr;
iExcelApp-> get_Workbooks(&ibooksPtr);
ibooksPtr-> Open(theXls.c_bstr(),
TNoParam(),TNoParam(),TNoParam(),TNoParam(),
TNoParam(),TNoParam(),TNoParam(),TNoParam(),
TNoParam(),TNoParam(),TNoParam(),TNoParam(),
0);
_WorksheetPtr isheet;
isheet=iExcelApp-> get_ActiveWorkbook()-> get_ActiveSheet();
long ColCount=isheet-> get_UsedRange(1)-> Columns-> get_Count();
Range* iRangePtr0, *iRangePtr;
Variant Cell1, Cell2;
Cell1=isheet-> Cells-> get_Item(TVariant(5), TVariant(1));
Cell2=isheet-> Cells-> get_Item(TVariant(5), TVariant(ColCount));
isheet-> get_Range(Cell1, Cell2,&iRangePtr0);
iRangePtr0-> Activate();
iRangePtr0-> Copy();
Cell1=isheet-> Cells-> get_Item(TVariant(6), TVariant(1));
Cell2=isheet-> Cells-> get_Item(TVariant(6), TVariant(ColCount));
isheet-> get_Range(Cell1, Cell2,&iRangePtr);
iRangePtr-> Columns-> Insert(TNoParam()); //第6行前加一行
ShowMessage( "关闭 ");
iRangePtr-> Release();
iRangePtr0-> Release();
isheet-> Release();
ibooksPtr-> Release();
iExcelApp-> Quit();
}
//---------------------------------------
事先,准备好yourExcel.xls文件