📄 shtmlreport.cpp
字号:
// SHTMLReport.cpp: implementation of the CSHTMLReport class.
// 模块:启程报表(基于DHTML)
// 说明:采用HTML提供对表格形式的数据的显示,目前只提供了几个常用的接口,
// 接口:
// void SetIndexFormat(CString strIndexFormat);
// BOOL MergeCol(int iCol);
// BOOL MergeRow(int iRow);
// BOOL DeleteRow(int index);
// BOOL SetItemHTML(int iRow,int iCol,CString html);
// BOOL InsertRow(int index);
// void SetTableName(CString name);
// void SetHtmlDocPtr(IHTMLDocument2 *pDoc);
// 本代码可以任意使用、修改、传播。
// 黄建雄 2003-10-14
// huangjianxiong@sina.com
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "sreport.h"
#include "SHTMLReport.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CSHTMLReport::CSHTMLReport()
{
m_pHtmlDoc2=NULL;
m_iHeadLines=1;
}
CSHTMLReport::~CSHTMLReport()
{
}
//***********************************************
// 指定HTML文档接口
//**********************************************
void CSHTMLReport::SetHtmlDocPtr(IHTMLDocument2 *pDoc)
{
m_pHtmlDoc2=pDoc;
}
//***********************************************
// 指定操作的表的名称,表名称在HTML模板中指定
//*********************************************
void CSHTMLReport::SetTableName(CString name)
{
m_strTableName=name;
}
//**************************************************
// 获取HTML表格COM接口,内部接口
//**************************************************
IHTMLTable * CSHTMLReport::GetTableDispatch()
{
ASSERT(m_pHtmlDoc2);
IHTMLElementCollection *all;
m_pHtmlDoc2->get_all(&all);
IDispatch *distable;
all->item(COleVariant(m_strTableName),COleVariant(short(0)),&distable);
IHTMLTable *pITable=NULL;
HRESULT hr=distable->QueryInterface(IID_IHTMLTable, (void**)&pITable);//get table dispatch
ASSERT(hr==S_OK);
return pITable;
}
//*******************************************************
// 在指定位置插入一行,index==-1表示append
//*******************************************************
BOOL CSHTMLReport::InsertRow(int index)
{
IHTMLTable *pITable=GetTableDispatch();
IDispatch *disrow;
HRESULT hr=pITable->insertRow(index,&disrow);//insert a row at 1 position
if(hr!=S_OK) return FALSE;
IHTMLTableRow *pIRow;
hr=disrow->QueryInterface(IID_IHTMLTableRow, (void**)&pIRow);
ASSERT(hr==S_OK);
long cols;
hr=pITable->get_cols(&cols);
ASSERT(hr==S_OK&&cols!=0);
IDispatch *discell;
IHTMLElement *cell;
CString str= " ";
BSTR bsStr = str.AllocSysString();
for(int i=0;i<cols;i++)
{
pIRow->insertCell(i,&discell);
discell->QueryInterface(IID_IHTMLElement,(void **)&cell);
cell->put_innerHTML(bsStr);
cell->Release();
discell->Release();
}
SysFreeString(bsStr);
pIRow->Release();
if(!m_strIndexFormat.IsEmpty())
{
IHTMLElementCollection *irows;
hr=pITable->get_rows(&irows);
ASSERT(hr==S_OK);
long rows;
hr=irows->get_length(&rows);
ASSERT(hr==S_OK);
CString strHead="";
if(index==-1) index=rows-1;
for(long i=index;i<rows;i++)
{
strHead.Format(m_strIndexFormat,i);
BSTR bsStrHead=strHead.AllocSysString();
hr=irows->item(COleVariant(long(i)),COleVariant(long(0)),&disrow);
ASSERT(hr==S_OK);
//get row interface
hr=disrow->QueryInterface(IID_IHTMLTableRow, (void**)&pIRow);
ASSERT(hr==S_OK);
IHTMLElementCollection *icells;
hr=pIRow->get_cells(&icells);
ASSERT(hr==S_OK);
hr=icells->item(COleVariant(long(0)),COleVariant(long(0)),&discell);
ASSERT(hr==S_OK);
discell->QueryInterface(IID_IHTMLElement,(void **)&cell);
ASSERT(hr==S_OK);
cell->put_innerText(bsStrHead);
SysFreeString(bsStrHead);
cell->Release();
pIRow->Release();
disrow->Release();
}
irows->Release();
}
pIRow->Release();
disrow->Release();
pITable->Release();
return TRUE;
}
//***************************************************************
// 修改单元格的内容:可以使用html语法
//***************************************************************
BOOL CSHTMLReport::SetItemHTML(int iRow, int iCol, CString html)
{
IHTMLTable *pITable=GetTableDispatch();
//get row collect
IHTMLElementCollection *pIRows;
HRESULT hr=pITable->get_rows(&pIRows);
ASSERT(hr==S_OK);
//get specisfied row
IDispatch *disrow;
hr=pIRows->item(COleVariant(long(iRow)),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 collect
IHTMLElementCollection *rowcells;
pIRow->get_cells(&rowcells);
//get cell
IDispatch *discell;
IHTMLElement *cell;
hr=rowcells->item(COleVariant(long(iCol)),COleVariant(short(0)),&discell);//iCol col
if(hr!=S_OK||discell==NULL) return FALSE;
hr=discell->QueryInterface(IID_IHTMLElement,(void **)&cell);
ASSERT(hr==S_OK);
BSTR bsStr = html.AllocSysString();
cell->put_innerHTML(bsStr);
SysFreeString(bsStr);
cell->Release();
pIRow->Release();
pITable->Release();
return TRUE;
}
//************************************************
// 删除指定行
//************************************************
BOOL CSHTMLReport::DeleteRow(int index)
{
IHTMLTable *pITable=GetTableDispatch();
HRESULT hr=pITable->deleteRow(index);
pITable->Release();
return hr==S_OK;
}
//************************************************
// 将指定行数据相同的单元格合并
//************************************************
BOOL CSHTMLReport::MergeRow(int iRow)
{
IHTMLTable *pITable=GetTableDispatch();
//get row collect
IHTMLElementCollection *pIRows;
HRESULT hr=pITable->get_rows(&pIRows);
ASSERT(hr==S_OK);
//get specisfied row
IDispatch *disrow;
hr=pIRows->item(COleVariant(long(iRow)),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 collect
IHTMLElementCollection *rowcells;
pIRow->get_cells(&rowcells);
long cells;
hr=rowcells->get_length(&cells);
IDispatch *discell;
IHTMLElement *icellElement;
long begin=-1,end=-1;
CString beginStr;
CString cellStr;
long beginRowSpan=0,rowSpan;
for(long i=m_strIndexFormat.IsEmpty()?0:1;i<cells;i++)
{
//get cell
hr=rowcells->item(COleVariant(long(i)),COleVariant(short(0)),&discell);
ASSERT(hr==S_OK&&discell!=NULL);
hr=discell->QueryInterface(IID_IHTMLElement,(void **)&icellElement);
ASSERT(hr==S_OK&&icellElement!=NULL);
cellStr=GetElementText(icellElement,inner_text);
IHTMLTableCell *icell;
hr=discell->QueryInterface(IID_IHTMLTableCell,(void **)&icell);
ASSERT(hr==S_OK&&icell!=NULL);
icell->get_rowSpan(&rowSpan);
if(cellStr!=beginStr||rowSpan!=beginRowSpan)
{
if(end-begin+1>1&&begin!=-1)
{
ASSERT(beginRowSpan!=0);
MergeRowPrivate(pIRow,begin,end);
cells-=end-begin;
i-=end-begin;
}
begin=end=i;
beginStr=cellStr;
beginRowSpan=rowSpan;
}else
end=i;
icell->Release();
icellElement->Release();
}
if(begin!=end)
MergeRowPrivate(pIRow,begin,end);
pIRow->Release();
pITable->Release();
return TRUE;
}
//************************************************
// 将指定列数据相同的单元格合并
//************************************************
BOOL CSHTMLReport::MergeCol(int iCol)
{
IHTMLTable *pITable=GetTableDispatch();
//get row collect
IHTMLElementCollection *pIRows;
HRESULT hr=pITable->get_rows(&pIRows);
ASSERT(hr==S_OK);
CSArray<COLCELLINFO> colArray;
if(!MakeColDispatchCollection(pIRows,iCol,colArray)) return FALSE;
int colCells=colArray.GetSize();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -