shtmlreport.cpp

来自「如果在您的软件中需要输出报表」· C++ 代码 · 共 559 行 · 第 1/2 页

CPP
559
字号
	COLCELLINFO cci;
	CString beginStr,cellStr;
	long	beginColSpan=0,colSpan;
	int		begin=-1,end;
	for(int i=0;i<colCells;i++)
	{
		cci=colArray.GetAt(i);
		if(cci.pDisCell==NULL)
			continue;
		IHTMLElement *icellElement;
		hr=cci.pDisCell->QueryInterface(IID_IHTMLElement,(void **)&icellElement);
		ASSERT(hr==S_OK&&icellElement!=NULL);
		cellStr=GetElementText(icellElement,inner_text);
		IHTMLTableCell *icell;
		hr=cci.pDisCell->QueryInterface(IID_IHTMLTableCell,(void **)&icell);
		ASSERT(hr==S_OK&&icell!=NULL);
		hr=icell->get_colSpan(&colSpan);
		if(cellStr!=beginStr||colSpan!=beginColSpan)
		{
			if(begin!=-1&&end-begin+1>1)
			{
				ASSERT(beginColSpan!=0);
				MergeColPrivate(pIRows,colArray,begin,end);
			}
			begin=end=i;
			beginStr=cellStr;
			beginColSpan=colSpan;
		}else
			end=i;
		icell->Release();
		icellElement->Release();
	}
	if(begin!=end)
		MergeColPrivate(pIRows,colArray,begin,end);
	pITable->Release();
	return TRUE;
}

//***********************************************
//	设置索引列的显示格式:遵循sprintf函数的规则
//***********************************************
void CSHTMLReport::SetIndexFormat(CString strIndexFormat)
{
	m_strIndexFormat=strIndexFormat;
}

//*************************************************
//	获取元素的字符
//*************************************************
CString CSHTMLReport::GetElementText(IHTMLElement *pElement, int type)
{
	CString str= "";
	BSTR bsStr = str.AllocSysString();
	switch(type)
	{
	case inner_text:
		pElement->get_innerText(&bsStr);
		break;
	case inner_html:
		pElement->get_innerHTML(&bsStr);
		break;
	case outer_text:
		pElement->get_outerText(&bsStr);
		break;
	case outer_html:
		pElement->get_outerHTML(&bsStr);
		break;
	default:
		ASSERT(0);
		break;
	}
	_bstr_t bstStr;
	bstStr = bsStr;
	str.Format("%s",(LPCTSTR)bstStr);
	SysFreeString(bsStr);
	return str;
}

//********************************************************
//	设置元素字符
//*******************************************************
BOOL CSHTMLReport::SetElementText(IHTMLElement *pElement, int type,CString str)
{
	BSTR bsStr = str.AllocSysString();
	HRESULT hr;
	switch(type)
	{
	case inner_text:
		hr=pElement->put_innerText(bsStr);
		break;
	case inner_html:
		hr=pElement->put_innerHTML(bsStr);
		break;
	case outer_text:
		hr=pElement->put_outerText(bsStr);
		break;
	case outer_html:
		hr=pElement->put_outerHTML(bsStr);
		break;
	default:
		ASSERT(0);
		break;
	}
	SysFreeString(bsStr);
	return (hr==S_OK);
}

//******************************************************************
//	在指定的行中从指定的起始及终止位置进行合并:内部函数
//******************************************************************
void CSHTMLReport::MergeRowPrivate(IHTMLTableRow *pIRow, long begincol, long endcol)
{
	long			colSpan=0;
	IHTMLElementCollection *picells;
	HRESULT hr=pIRow->get_cells(&picells);
	for(long i=endcol;i>=begincol;i--)
	{
		IDispatch *discell;
		hr=picells->item(COleVariant(i),COleVariant(short(0)),&discell);
		ASSERT(hr==S_OK&&discell!=NULL);
		IHTMLTableCell *picell;
		hr=discell->QueryInterface(IID_IHTMLTableCell,(void **)&picell);
		ASSERT(hr==S_OK&&picell!=NULL);
		long tmpColSpan;
		picell->get_colSpan(&tmpColSpan);
		colSpan+=tmpColSpan;
		if(i==begincol)
			picell->put_colSpan(colSpan);
		else
			pIRow->deleteCell(i);
		picell->Release();
	}
}

//******************************************************************
//	在指定的行集中对指定列从指定的起始及终止位置进行合并:内部函数
//******************************************************************
void CSHTMLReport::MergeColPrivate(IHTMLElementCollection *pIRowArray,CSArray<COLCELLINFO> &colArray,long beginrow, long endrow)
{
	IHTMLTableCell *ibegincell=NULL;
	long			rowSpan=0;
	for(int i=beginrow;i<=endrow;i++)
	{
		IHTMLTableCell *icell;
		COLCELLINFO cci=colArray.GetAt(i);
		ASSERT(cci.iIndexInRow!=-1);
		IDispatch *disrow;
		HRESULT hr=pIRowArray->item(COleVariant(cci.iRow),COleVariant(short(0)),&disrow);//iRow row
		if(hr!=S_OK||disrow==NULL) return ;
		IHTMLTableRow *pIRow;
		hr=disrow->QueryInterface(IID_IHTMLTableRow, (void**)&pIRow);
		ASSERT(hr==S_OK);
		//get cell collection
		IHTMLElementCollection *rowcells;
		pIRow->get_cells(&rowcells);
		//get cell element
		IDispatch *discell;
		hr=rowcells->item(COleVariant(cci.iIndexInRow),COleVariant(short(0)),&discell);
		ASSERT(hr==S_OK&&discell!=NULL);
		
		hr=discell->QueryInterface(IID_IHTMLTableCell,(void **)&icell);
		ASSERT(hr==S_OK&&icell!=NULL);
		long       tmpRowSpan;
		hr=icell->get_rowSpan(&tmpRowSpan);
		ASSERT(hr==S_OK);
		rowSpan+=tmpRowSpan;
		if(i==beginrow)
		{
			ibegincell=icell;
		}else{
			icell->Release();										
			if(cci.iIndexInRow!=-1)
				pIRow->deleteCell(cci.iIndexInRow);
		}
		pIRow->Release();
	}
	ibegincell->put_rowSpan(rowSpan);
	ibegincell->Release();
}

//******************************************************************
//	构造指定列的集合:内部函数
//******************************************************************
BOOL CSHTMLReport::MakeColDispatchCollection(
	IHTMLElementCollection *pIRowArray/*in*/,
	long iCol/*in*/,
	CSArray<COLCELLINFO> &colArray/*out*/)
{
	long rows;
	HRESULT hr=pIRowArray->get_length(&rows);
	ASSERT(hr==S_OK);
	IHTMLElementCollection **rowcellsarray =new IHTMLElementCollection *[rows];

	//get cell collect array
	for(int j=0;j<rows;j++)
	{
		//get specisfied row
		IDispatch *disrow;
		hr=pIRowArray->item(COleVariant(long(j)),COleVariant(short(0)),&disrow);//iRow row
		if(hr!=S_OK||disrow==NULL) return FALSE;
		IHTMLTableRow *pIRow;
		hr=disrow->QueryInterface(IID_IHTMLTableRow, (void**)&pIRow);
		ASSERT(hr==S_OK);
		//get cell collection
		pIRow->get_cells(&rowcellsarray[j]);
		pIRow->Release();
	}
	
	struct colindexinfo{
		long index;	//在行中的索引
		long iCol;	//在列中的索引
		long colSpan;//列跨越
	};
	struct colindexinfo *ciis=new struct colindexinfo[rows];
	memset(ciis,0,rows*sizeof(struct colindexinfo));

	for(j=1;j<=iCol;j++)//追踪到第iCol列:初始化的数据即为第一列的数据,不需要计算
	{
		for(int k=0;k<rows;k++)
		{
			if(ciis[k].iCol+ciis[k].colSpan>j) continue;
			IDispatch *discell;
			hr=rowcellsarray[k]->item(COleVariant(ciis[k].index),COleVariant(short(0)),&discell);
			ASSERT(hr==S_OK&&discell!=NULL);
			IHTMLTableCell *icell;
			hr=discell->QueryInterface(IID_IHTMLTableCell,(void **)&icell);
			ASSERT(hr==S_OK&&icell!=NULL);
			long colSpan,rowSpan;
			hr=icell->get_colSpan(&colSpan);
			ASSERT(hr==S_OK);
			hr=icell->get_rowSpan(&rowSpan);
			icell->Release();

			ciis[k].index++;
			ciis[k].iCol+=colSpan;
			ciis[k].colSpan=colSpan-1;
			for(int kk=k+1;kk<k+rowSpan;kk++)
			{
				if(ciis[kk].iCol+ciis[kk].colSpan>j){
					delete []ciis;
					delete []rowcellsarray;
					return FALSE;
				}
				ciis[kk].iCol++;
				ciis[kk].colSpan=0;
			}
			k+=rowSpan-1;
		}
	}
	for(int i=0;i<rows;i++)
	{
		if(ciis[i].colSpan){
			COLCELLINFO cci={i,NULL,-1};
			colArray.Add(cci);
			continue;
		}
		IDispatch *discell;
		hr=rowcellsarray[i]->item(COleVariant(ciis[i].index),COleVariant(short(0)),&discell);
		ASSERT(hr==S_OK&&discell!=NULL);
		IHTMLTableCell *icell;
		hr=discell->QueryInterface(IID_IHTMLTableCell,(void **)&icell);
		ASSERT(hr==S_OK&&icell!=NULL);
		long rowSpan;
		hr=icell->get_rowSpan(&rowSpan);
		icell->Release();

		COLCELLINFO cci;
		cci.iRow=i;
		cci.iIndexInRow=ciis[i].index;
		cci.pDisCell=discell;
		colArray.Add(cci);
		i+=rowSpan-1;
	}
	delete []ciis;
	delete []rowcellsarray;
	return TRUE;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?