📄 reportctl.cpp
字号:
int i=1;
while(1)
{
cell=range.GetItem (_variant_t((long)i),vtMissing).pdispVal ;
_variant_t f= cell.GetValue ();
BSTR bstr=f.bstrVal ;
CString text(bstr);
if(!text.GetLength ()) break;
ReMoveChar (text,' ');
prostrarr.Add (text);
i++;
}
if(range.m_lpDispatch !=NULL)range.ReleaseDispatch ();
return i-1;*/
GetExcelRowToArray(1,prostrarr);
return prostrarr.GetSize ();
}
//false表示全空 n --row SpaceEndOrAppointCol=0表示空格结束否则表示列数
bool CReportCtrl::GetExcelRowToArray(int n, CStringArray &prostrarr, int SpaceEndOrAppointCol)
{
if(SpaceEndOrAppointCol<0) return false;
Range range,cell;
range=worksheet.GetRows ();
range=range.GetItem (_variant_t((long)n),vtMissing).pdispVal ;
range=range.GetCells ();
int i=1;
while(1)
{
cell=range.GetItem (_variant_t((long)i),vtMissing).pdispVal ;
_variant_t f= cell.GetValue ();
f.ChangeType( VT_BSTR ,NULL);
BSTR bstr=f.bstrVal ;
CString text(bstr);
ReMoveChar (text,' ');
// text.Replace (' ','_');
if(SpaceEndOrAppointCol==0)
{if(!text.GetLength ()) break;}
else
if(i>SpaceEndOrAppointCol) break;
prostrarr.Add (text);
i++;
}
if(range.m_lpDispatch !=NULL)range.ReleaseDispatch ();
int j; i=prostrarr.GetSize ();
if(i==0) return false;
for(j=0;j<i;j++)
if(!prostrarr[j].IsEmpty ()) return true;
return false;
}
bool CReportCtrl::IsOneStrBelongAnotherStr(CStringArray &one, CStringArray &another,int *p )
{
CString tempstr1,tempstr2;
bool cmp=false;
int m,n,i,j;
m=one.GetSize ();
n=another.GetSize ();
for(i=1;i<=m;i++)
{
tempstr1=one.GetAt (i-1);
cmp=false;
for(j=1;j<=n;j++)
{
tempstr2=another.GetAt (j-1);
if(tempstr1==tempstr2) {if(p!=NULL) *p++=j; cmp=true;break;}
}
if(cmp==false) return false;
}
return cmp;
}
bool CReportCtrl::IsGroupPropertyExist()
{
bool result;
if(!m_groupProperty.GetLength ()) return false;
int grouppropertynum=ExtractSubtring(m_groupProperty,
GroupDataSourceProperty);
pGroupDataSourcePropertySN=new int[GroupDataSourceProperty.GetSize ()];
result=IsOneStrBelongAnotherStr(GroupDataSourceProperty,
DataSourceProperty,
pGroupDataSourcePropertySN);
if(result==false) { delete pGroupDataSourcePropertySN;
pGroupDataSourcePropertySN=NULL;}
return result;
}
bool CReportCtrl::DataSourceKeyIsExist()
{ bool result;
if(!m_dataSourceKey.GetLength ()) return false;
int grouppropertynum=ExtractSubtring(m_dataSourceKey,
DataSourcePropertyKey);
pDataSourcePropertyKeySN=new int[ DataSourcePropertyKey.GetSize ()];
result=IsOneStrBelongAnotherStr(DataSourcePropertyKey,
DataSourceProperty,
pDataSourcePropertyKeySN);
if(result==false) { delete pDataSourcePropertyKeySN;
pDataSourcePropertyKeySN=NULL;}
return result;
}
//extendname含.
bool CReportCtrl::IsSpecificallyFile(CString filename, CString extendname)
{
if(filename.Right(extendname.GetLength ())==extendname) return true;
return false;
}
//flag=1 删前面 =2删后面 =3同时
short CReportCtrl::DeleteSpace(CString &str, int flag)
{ short count=0, i=0;
switch(flag)
{
case 1:
while(str.GetLength ())
{
if(str[0]==' ')
{str.Delete (0);count++;}
else break;
}
break;
case 2:
while(i=str.GetLength ())
{
if(str[i-1]==' ')
{str.Delete (i-1);count++;}
else break;
}
break;
case 3:
while(str.GetLength ())
{
if(str[0]==' ')
{count++;
str.Delete (0);}
else break;
}
while(i=str.GetLength ())
{
if(str[i-1]==' ')
{count++;
str.Delete (i-1);}
else break;
}
break;
}
return count;
}
short CReportCtrl::BookmarkType(CString &BookmarklName)
{
if(BookmarklName.Left (9)=="yjjyjjyjj") return 2;//用于保留
if(BookmarklName.Left (6)=="yjjyjj") return 1;//用于保留
return 0;//一般数据
}
int CReportCtrl::FindBookmarkName(CStringArray &x, CString &str)
{
int i=x.GetSize ();
for(int j=0;j<i;j++)
{
if(x[j]==str) return j;
}
return -1;
}
short CReportCtrl::MakeReport()
{ bool result;
// TODO: Add your dispatch handler code here
if(!IsSpecificallyFile(m_DateSourceName,".xls"))
{m_ErrorinformationCode=8;return 8; }
if(!IsSpecificallyFile(m_reportFileName,".doc"))
{m_ErrorinformationCode=9;return 9; }
if(!IsSpecificallyFile(m_reportTemplateFileName,".doc"))
{m_ErrorinformationCode=10;return 10;}
if((result=IsDirExist(m_filePath))==false)
{
m_ErrorinformationCode=2;return 2;}
if((result=IsFileExist(m_DateSourceName,m_filePath))==false)
{m_ErrorinformationCode=1;return 1;}
if((result=IsFileExist(m_reportTemplateFileName,m_filePath))==false)
{m_ErrorinformationCode=3;return 3;}
if((result=IsLegalFileName(m_reportFileName))==false)
{m_ErrorinformationCode=4;return 4;}
//=GetDataSourceProperty打开excel
int pronum=GetDataSourceProperty(DataSourceProperty);
if(pronum==-1)
{m_ErrorinformationCode=7; EndExcel();return 7;}
if(grouping()&&!IsGroupPropertyExist())
{m_ErrorinformationCode=5; return 5;}
if(!DataSourceKeyIsExist())
{m_ErrorinformationCode=6; return 6;}
switch(m_ReportType)
{
case OneRecordOneReport:if(OneRecordOneReportMakeReport()==-1)
{m_ErrorinformationCode=11; return 11; };
break;
}
DataSourceProperty.RemoveAll ();
GroupDataSourceProperty.RemoveAll ();
DataSourcePropertyKey.RemoveAll ();
if(pGroupDataSourcePropertySN){delete pGroupDataSourcePropertySN;
pGroupDataSourcePropertySN=NULL;}
if(pDataSourcePropertyKeySN){delete pDataSourcePropertyKeySN;
pDataSourcePropertyKeySN=NULL;}
EndExcel();
return 0;
}
void CReportCtrl::EndExcel()
{
if(excel.m_lpDispatch !=NULL)
{
book.Close(_variant_t((long)0),vtMissing,vtMissing);
books.Close ();
excel.Quit ();
excel.ReleaseDispatch ();
books.ReleaseDispatch ();
book.ReleaseDispatch ();
sheets.ReleaseDispatch ();
worksheet.ReleaseDispatch ();
}
}
void CReportCtrl::QuitExcel()
{
excel.Quit (); // TODO: Add your dispatch handler code here
}
bool IsStringArrSpeciPositionNoEmpty( CStringArray &array , int *p,int n)
{
if(array.GetSize()<n) return false;
if(n<1) return false;
for(int i=0;i<n;i++)
if(array[p[i]-1].IsEmpty()) return false;
return true;
}
//暂时返回-1表示错
short CReportCtrl::OneRecordOneReportMakeReport()
{
Rangeword range;
int exceldatarownum=0;
CStringArray excelrow;
long count,i,j,colnum;//count 标签数量 colnum excel当前行
long priorposition_end=0;//粘贴前的文件结尾
_variant_t end;
long pagenum,priorpagenum; //粘贴前后的页数
short *point;//标签所在的列
int validbookname=0;//暂时没用
if(word.m_lpDispatch !=NULL) return -1;
if(!word.CreateDispatch ("Word.Application",NULL)) return -1;
if(worddoc_ReportFile.m_lpDispatch ==NULL)
{
word.SetVisible (false);
word.SetWindowState (1);
worddocs=word.GetDocuments ();
worddoc_ReportFile=worddocs.Add(&vtMissing,&vtMissing,&vtMissing,&vtMissing);
_variant_t file=m_filePath+'\\'+ m_reportTemplateFileName;
worddoc_TemplateFile=worddocs.Open (&file,&vtMissing,&vtMissing,
&vtMissing,&vtMissing, &vtMissing,&vtMissing,
&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing);
bookmarks=worddoc_TemplateFile.GetBookmarks ();
count=bookmarks.GetCount ();
point =new short[count+1];
for(i=1;i<=count;i++)//确定标签对应excel表的列数
{
bookmark=bookmarks.Item (&_variant_t((long)i));
CString str=bookmark.GetName ();//空格处理以后加
point[i]= FindBookmarkName(DataSourceProperty,str);
if(point[i]>0) validbookname++;
}
colnum=2;
while(GetExcelRowToArray(colnum,excelrow,DataSourceProperty.GetSize ())&&
IsStringArrSpeciPositionNoEmpty(excelrow,
pDataSourcePropertyKeySN, DataSourcePropertyKey.GetSize ())
)
{
exceldatarownum++;
i=1;
while(i<=count)
{
bookmark=bookmarks.Item (&_variant_t((long)i));
range=bookmark.GetRange ();
int k=point[i];
if(k>0)
range.SetText (excelrow[k]);
i++;
}
range= worddoc_TemplateFile.GetContent ();
range.Copy ();
range=worddoc_ReportFile.GetContent();//粘贴
end=(long) (range.GetEnd ()-1);
priorposition_end=end;
priorpagenum=(range.GetInformation (4)).lVal ;//注意调试
range=worddoc_ReportFile.Range (&end,&end);
range.Paste ();
switch(m_ReportCharacter&0x0001?1:2)
{
case 1:range=worddoc_ReportFile.GetContent();//分页
end=(long) (range.GetEnd ()-1);
range=worddoc_ReportFile.Range (&end,&end);
range.InsertBreak (&_variant_t((long)7));//分页
break;
case 2:
range=worddoc_ReportFile.GetContent();//分页
end=(long) (range.GetEnd ()-1);
pagenum=range.GetInformation (4).lVal;
if(pagenum!=priorpagenum)
{
range=worddoc_ReportFile.Range (&_variant_t((long)(priorposition_end)),&_variant_t((long)(priorposition_end)));
range.InsertBreak (&_variant_t((long)7));//如跨页则分页
}
break;
}
colnum++;
excelrow.RemoveAll ();
//取消对worddoc2的修改??
worddoc_TemplateFile.Undo (&_variant_t((long)validbookname));
}//while
//如temp.doc刚好满页则结果最后多一个空页
//解决:计算页数,多的减一
switch(m_ReportCharacter&0x0001?1:2)
{ case 2:
range= worddoc_ReportFile.GetContent ();
j=range.GetInformation (4).lVal;
if(j>exceldatarownum) //case 2最多一页每条记录
{
characters=worddoc_ReportFile.GetCharacters ();
range=characters.Item (1);
range.Delete (&_variant_t(long(1)),&_variant_t(long(1)));
}
case 1:
range= worddoc_TemplateFile.GetContent ();
i=range.GetInformation (4).lVal; //取得页数
range= worddoc_ReportFile.GetContent ();
j=range.GetInformation (4).lVal;
if(j>=i* exceldatarownum+1)
{
paragraphs=worddoc_ReportFile.GetParagraphs ();
count=paragraphs.GetCount ();
if(count>0)paragraph=paragraphs.Item (count);
range=paragraph.GetRange ();
range.Delete (&_variant_t(long(1)),&_variant_t(long(1)));
}
break;
}
//删除最后一页?
window=word.GetActiveWindow ();
view=window.GetView ();
view.SetShowBookmarks (FALSE);
_variant_t filename= m_filePath+'\\'+m_reportFileName;
worddoc_ReportFile.SaveAs(&filename,&vtMissing,&vtMissing,&vtMissing,&vtMissing,
&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing,&vtMissing);
worddoc_TemplateFile.Close (&_variant_t((long)0),&vtMissing,&vtMissing);
worddoc_ReportFile.Close (&_variant_t((long)0),&vtMissing,&vtMissing);
EndWord();
}
return 0;
}
void CReportCtrl::EndWord()
{
word.Quit(&vtMissing,&vtMissing,&vtMissing) ;
word.ReleaseDispatch ();
worddocs.ReleaseDispatch ();
worddoc_TemplateFile.ReleaseDispatch ();
worddoc_ReportFile.ReleaseDispatch ();
bookmarks.ReleaseDispatch ();
window.ReleaseDispatch ();
select.ReleaseDispatch ();
bookmark.ReleaseDispatch ();
view.ReleaseDispatch ();
paragraphs.ReleaseDispatch ();
paragraph.ReleaseDispatch ();
characters.ReleaseDispatch ();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -