- UID
- 759598
- 积分
- 28
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2016-3-16
- 最后登录
- 1970-1-1
|
楼主 |
发表于 2016-5-8 16:14:24
|
显示全部楼层
void fillexcel(CStringArray* inputarr,int ls,int zs)
{
CStringArray teststr1;
teststr1.SetSize(zs);
CString temp;
int i1;
for(i1=0;i1<zs;i1++)
{
temp=inputarr->GetAt(i1);
teststr1.SetAt(i1,temp);
}
CApplication app;
CWorkbook book;
CWorksheet sheet;
CWorkbooks books;
CWorksheets sheets;
CRange range;
LPDISPATCH lpDisp;
LPDISPATCH prange;
COleVariant
covTrue((short)TRUE),covFalse((short)FALSE),covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
if(!app.CreateDispatch(_T("Excel.Application")))
{
AfxMessageBox(_T("连接失败"));
return;
}
app.put_Visible(TRUE);
lpDisp=app.get_Workbooks();
books.AttachDispatch(lpDisp);
lpDisp=books.Add(covOptional);
book.AttachDispatch(lpDisp);
lpDisp=book.get_Sheets();
sheets.AttachDispatch(lpDisp);
lpDisp=sheets.get_Item(COleVariant((short)(1)));
sheet.AttachDispatch(lpDisp);
//
int hangshu;//i,j,
hangshu=zs/ls;
CStringArray pos;
pos.SetSize(zs);
for(int j=0;j<hangshu;j++)
{
for(int i=0;i<ls;i++)
{
CString temp;
temp.Format((_T("%d")),j+1);//定义excel表的行数
pos.SetAt(4*j+0,(_T("A")+temp));//定义excel表的第一列(SetAt((ls*j+0)
pos.SetAt(4*j+1,(_T("B")+temp));//定义excel表第二列
pos.SetAt(4*j+2,(_T("C")+temp));//定义excel表的第一列
pos.SetAt(4*j+3,(_T("D")+temp));//定义excel表第二列
}
}
for(int l=0;l<hangshu;l++)
{
for(int k=0;k<ls;k++)//
{
prange=sheet.get_Range(COleVariant(pos.GetAt(ls*l+k)),COleVariant(pos.GetAt(ls*l+k)));//
range.AttachDispatch(prange);
CString tempneirong;
tempneirong=teststr1.GetAt(ls*l+k);
range.put_Value2(COleVariant(tempneirong));
delete tempneirong;//rangevaluedatatype,
}
}
range.ReleaseDispatch();
sheet.ReleaseDispatch();
}
主函数如下:
static void EXE_MyCommand1(void)
{
// Add your code for command EXE._MyCommand1 here
CStringArray teststr;
CString test1,test2;
ACHAR ch1[50];
wcscpy(ch1,_T("熔断器"));
ACHAR ch2[50];
wcscpy(ch2,_T("333"));
test1.Format(_T("%s"),ch1);
test2.Format(_T("%s"),ch2);
teststr.SetSize(2);
teststr.SetAt(0,test1);
teststr.SetAt(1,test2);
int lieshu=2;
int len2=2;
fillexcel(&teststr,lieshu,len2);
}
|
|