⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ftwordrpt.cpp

📁 一个通过USB->UART读取C8051F060的程序
💻 CPP
字号:
// FTWordRpt.cpp: implementation of the CFTWordRpt class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FTWordRpt.h"
#include "Atlbase.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#if !defined(EXISTPRINTER)
#define    EXISTPRINTER		/*连接打印机时,可以打印*/
#endif

#ifndef    OFFICE_WORD2K	
#define    OFFICE_WORD2K	/*安装Office2000/XP*/
#endif

/*
 假定已经运行AfxOleInit()函数。
*/
CFTWordRpt::CFTWordRpt()
{
			m_nCurrRow   = 2;
			m_bInitOk	 = FALSE;	

#ifdef    OFFICE_WORD2K
			m_bWordEnvOk = m_objWordApp.CreateDispatch(_T("Word.Application.8"));
#else
			m_bWordEnvOk = m_objWordApp.CreateDispatch(_T("Word.Application.9"));
#endif
}
//
CFTWordRpt::~CFTWordRpt()
{
			m_objWordApp.ReleaseDispatch();
}

//填充单元格值
void	CFTWordRpt::CellCtx(Table &objTbl, int nRow, int nCol, char *szCellCtx)
{
			if (szCellCtx != NULL)
			{
					Cell    objCell;
					Range   objRange;

					objCell = objTbl.Cell(nRow, nCol);	
					
					//取得对象
					objRange.AttachDispatch(objCell.GetRange());
					objRange.SetText(szCellCtx);
			}
}

//
void    CFTWordRpt::CellWidth(Table &objTbl, int nRow, int nCol, float fCellWidth)
{
			Cell    objCell;
			objCell = objTbl.Cell(nRow, nCol);
			
			objCell.SetWidth(fCellWidth);
}

//填充单元格值
void	CFTWordRpt::CellCtx(Table &objTbl, int nRow, int nCol, float fCellVal)
{
			CString    strCell;
			strCell.Format("%.2f", fCellVal);
			
			this->CellCtx(objTbl, nRow, nCol, (LPSTR)(LPCTSTR)strCell);
}

//填充单元格值
void	CFTWordRpt::CellCtx(Table &objTbl, int nRow, int nCol, int nCellVal)
{
			CString    strCell;
			strCell.Format("%d", nCellVal);
			//
			this->CellCtx(objTbl, nRow, nCol, (LPSTR)(LPCTSTR)strCell);
}

typedef  struct   _tagItemPositionParagraph
{
public:
			BOOL	bPlotVis;
			BOOL	bFeatureVis;
			BOOL	bDataTableVis;

#pragma  pack(2)
			short  nGraphParaNum;		//图形跳段
			short  nFeatureTblParaNum;	//特性表跳段
			short  nRealTblParaNum;		//实时表跳段
#pragma  pack()

public:
			/*仅用在对象挑转的语句内*/
			int   _CalcGraphPara(BOOL bPlotVis, BOOL bFeatureVis, BOOL bDataTableVis)
			{
					if (bPlotVis == TRUE)
						return  2;

					return  0;
			}
			int   _CalcFeatureTblPara(BOOL bPlotVis, BOOL bFeatureVis, BOOL bDataTableVis)
			{
					if (bPlotVis == TRUE)
						return 4;
					
					if (bPlotVis == FALSE)
						return 2;

					return  0;
			}
			int   _CalcRealTblPara(BOOL bPlotVis, BOOL bFeatureVis, BOOL bDataTableVis)
			{
					if (bPlotVis == TRUE && bFeatureVis == TRUE)
						return 9;
					
					if (bPlotVis == TRUE && bFeatureVis == FALSE)
						return 4;

					if (bPlotVis == FALSE && bFeatureVis == TRUE)
						return 6;

					if (bPlotVis == FALSE && bFeatureVis == FALSE)
						return 2;					

					return  0;
			}
}ItemPositionParaInfo;
static   ItemPositionParaInfo   g_sItemPositionParaInfo;



//创建表对象
void       CFTWordRpt::CreateTable(BOOL bPlotVis, BOOL bFeatureVis, BOOL bDataTableVis, int  nItemsNo)
{
				m_objTbls =  m_objDocu.GetTables();				   //得到文档中所有表对象

				/////////////////////////////////////////////////////////////////////////
				//1.特征值表格
				if (bFeatureVis)
				{
						Paragraphs        objParags;
						objParags = m_objDocu.GetParagraphs();
						objParags.Add(&CComVariant(m_objSelection.GetRange()));		//移到下一段
						m_objSelection.GoTo(&CComVariant(3), &CComVariant(1), 
							&CComVariant(g_sItemPositionParaInfo._CalcFeatureTblPara(bPlotVis, bFeatureVis, bDataTableVis)), 
							&CComVariant(_T("")) );
			
						m_objRptTable = m_objTbls.Add(m_objSelection.GetRange(),/*范围*/
													 3/*行*/, 4/*列*/, 
													 &CComVariant(1),
													 &CComVariant(0));  
						//填写表头标题
						this->CellCtx(m_objRptTable, 1, 1, _T("电池型号:"));
						this->CellCtx(m_objRptTable, 1, 3, _T("试验日期:"));
						this->CellCtx(m_objRptTable, 2, 1, _T("激活时间:"));
						this->CellCtx(m_objRptTable, 2, 3, _T("最高电压:"));
						this->CellCtx(m_objRptTable, 3, 1, _T("最低电压:"));
						this->CellCtx(m_objRptTable, 3, 3, _T("工作时间:"));
						
						this->CellWidth(m_objRptTable, 1, 1, 70);
						this->CellWidth(m_objRptTable, 1, 3, 70);
						this->CellWidth(m_objRptTable, 2, 1, 70);
						this->CellWidth(m_objRptTable, 2, 3, 70);
						this->CellWidth(m_objRptTable, 3, 1, 70);
						this->CellWidth(m_objRptTable, 3, 3, 70);
						this->CellWidth(m_objRptTable, 1, 2, 140);
						this->CellWidth(m_objRptTable, 1, 4, 140);
						this->CellWidth(m_objRptTable, 2, 2, 140);
						this->CellWidth(m_objRptTable, 2, 4, 140);
						this->CellWidth(m_objRptTable, 3, 2, 140);
						this->CellWidth(m_objRptTable, 3, 4, 140);




						//得到增加表格的边框
						Borders  objBorders = m_objRptTable.GetBorders();				
						for (int k = -8; k < 0; k++)	//设定边框可视性(-2,-4,-1,-3)
						{
								m_objBrder = objBorders.Item(k);				/*每个Border*/	
								m_objBrder.SetLineStyle(0);						//隐藏
						}
						objBorders.SetShadow(FALSE);							//隐藏阴影
				}		

			
				//////////////////////////////////////////////////////////////////////////
				//2.增加实时电压表
				if (bDataTableVis)
				{
						m_objSelection.GoTo(&CComVariant(3), &CComVariant(1), &CComVariant(8), &CComVariant(_T("")));
						Paragraphs         objParags1;
 						objParags1 = m_objDocu.GetParagraphs();
 						objParags1.Add(&CComVariant(m_objSelection.GetRange()));	//移到下一段				
						m_objSelection.GoTo(&CComVariant(3), &CComVariant(1), 
							&CComVariant(g_sItemPositionParaInfo._CalcRealTblPara(bPlotVis, bFeatureVis, bDataTableVis)), 
							&CComVariant(_T("")));

						_ParagraphFormat  objPF;
						objPF = m_objSelection.GetParagraphFormat();
						objPF.SetAlignment(1);					//center

						m_objRptTable = m_objTbls.Add(m_objSelection.GetRange()/*范围*/,
													  1/*行*/, 6/*列*/, 
													  &CComVariant(1),
													  &CComVariant(0)); 											
										//调整列宽度
										this->CellWidth(m_objRptTable, 1, 1, 40);
										this->CellWidth(m_objRptTable, 1, 2, 80);
										this->CellWidth(m_objRptTable, 1, 3, 80);
										this->CellWidth(m_objRptTable, 1, 4, 80);
										this->CellWidth(m_objRptTable, 1, 5, 80);
										this->CellWidth(m_objRptTable, 1, 6, 80);
										
										//填写表头标题
										this->CellCtx(m_objRptTable, 1, 1, _T("序号"));
										this->CellCtx(m_objRptTable, 1, 2, _T("时刻点(ms)"));
										this->CellCtx(m_objRptTable, 1, 3, _T("交流通道1"));
										this->CellCtx(m_objRptTable, 1, 4, _T("直流通道1"));
										this->CellCtx(m_objRptTable, 1, 5, _T("交流通道2"));
										this->CellCtx(m_objRptTable, 1, 6, _T("直流通道2"));

						//从10行开始
						m_objSelection.GoTo(&CComVariant(3), &CComVariant(1), &CComVariant(10), &CComVariant(_T("")));
						m_objSelection.InsertRows(&CComVariant(nItemsNo));
				}
}

////////////////////////////////////////////////////////////////////////////////////
/*填写检测项名称*/
/*Word中列行的第一有效数为1,ADO中为0。*/
void  CFTWordRpt::FillBattHeader(char *szBattType, char *szTestDate, float fFireTime,
							   float fMaxVol, float fMinVol, float fElapseTime)
{		
			Table  objRptTable = m_objTbls.Item(1);
			
			this->CellCtx(objRptTable, 1, 2/*col 2*/, szBattType);
			this->CellCtx(objRptTable, 1, 4/*col 2*/, szTestDate);
			this->CellCtx(objRptTable, 2, 2/*col 2*/, fFireTime);
			this->CellCtx(objRptTable, 2, 4/*col 2*/, fMaxVol);
			this->CellCtx(objRptTable, 3, 4/*col 2*/, fElapseTime);
			this->CellCtx(objRptTable, 3, 2/*col 2*/, fMinVol);
}

/*填写检测项子项的一行数据*/
void	CFTWordRpt::FillRealTbl(float fElapse, float fCh1DC, float fCh1AC, float fCh2DC, float fCh2AC)
{
			if (!m_bInitOk && g_sItemPositionParaInfo.bFeatureVis == FALSE)								//未初始化退出
				return ;
				
			Table  objRptTable = m_objTbls.Item( (g_sItemPositionParaInfo.bFeatureVis == TRUE)?2:1);	//对第2个表	
			this->CellCtx(objRptTable, m_nCurrRow, 1, m_nCurrRow-1);//序号
			this->CellCtx(objRptTable, m_nCurrRow, 2, fElapse);		//时刻点
			this->CellCtx(objRptTable, m_nCurrRow, 3, fCh1AC);		//交流通道1
			this->CellCtx(objRptTable, m_nCurrRow, 4, fCh1DC);		//直流通道1
			this->CellCtx(objRptTable, m_nCurrRow, 5, fCh2AC);		//交流通道2
			this->CellCtx(objRptTable, m_nCurrRow, 6, fCh2DC);		//直流通道2
			
			m_nCurrRow ++;
}


//初始化报表
BOOL	CFTWordRpt::InitEnv(BOOL bPlotVis, CString strPicNm, BOOL bFeatureVis, BOOL bDataTableVis, 
							char *szTitle, int nItemsNo/*实时电压表的行数*/)
{
			m_bInitOk =  FALSE;
			if (!m_bWordEnvOk)
				return  FALSE;
			
			///////////////////////////////////////////////////////////////////////////////
			g_sItemPositionParaInfo.bPlotVis      = bPlotVis;
			g_sItemPositionParaInfo.bFeatureVis   = bFeatureVis;
			g_sItemPositionParaInfo.bDataTableVis = bDataTableVis;

			///////////////////////////////////////////////////////////////////////////////
			m_objWordApp.SetVisible(TRUE);
			Documents  objDocs = m_objWordApp.GetDocuments();
			m_objDocu = objDocs.Add(&CComVariant(""), &CComVariant(0),
									&CComVariant(0), &CComVariant(1));
			/*表格文档对象*/
			m_objSelection = m_objWordApp.GetSelection();
			_Font  objFont = m_objSelection.GetFont();			
			float  fOldSize = objFont.GetSize();
			objFont.SetSize(22);
			objFont.SetName(_T("宋体"));
			
			///////////////////////////////////////////////////////////////////////////////
			/*1.文档的标题 */
			_ParagraphFormat  objPF;
			objPF = m_objSelection.GetParagraphFormat();
			objPF.SetAlignment(1);					//center
			m_objSelection.TypeText(szTitle);
			objFont.SetSize(fOldSize);
			//objPF.SetAlignment(0);				//left
			
			if (bPlotVis)
			{
					Paragraphs        objParags;
					objParags = m_objDocu.GetParagraphs();
					objParags.Add(&CComVariant(m_objSelection.GetRange()));		//移到下一段
					m_objSelection.GoTo(&CComVariant(3), &CComVariant(1), 
										&CComVariant(g_sItemPositionParaInfo._CalcGraphPara(bPlotVis, bFeatureVis, bDataTableVis)), 
										&CComVariant(_T("")) );					

					this->InsertImage(strPicNm);
					objParags.Add(&CComVariant(m_objSelection.GetRange()));		//移到下一段
			}


			////////////////////////////////////////////////////////////////////////////////////////////
			/*2.创建特征值和实时电压表表格对象*/
			this->CreateTable(bPlotVis, bFeatureVis, bDataTableVis, nItemsNo);			//表头的标题
			
			/*表格左移,调整水平位置*/
			Rows theRows = m_objRptTable.GetRows();
			theRows.SetLeftIndent(-6, 0);	
			m_bInitOk =  TRUE;


			return   m_bInitOk;
}

//打印报表
void	CFTWordRpt::PrintRpt()
{
		#ifdef    EXISTPRINTER
			COleVariant varEmpty(DISP_E_PARAMNOTFOUND, VT_ERROR);

			m_objDocu.PrintOut(&CComVariant(FALSE)/*background*/, &varEmpty, &CComVariant(0)/*Range*/, 
								  &CComVariant("")/*filename*/, &varEmpty/*from*/, &varEmpty/*to*/, 
								  &CComVariant(0)/*item*/, &CComVariant(1)/*ncopies*/, &CComVariant("")/*pages*/,
								  &CComVariant(0)/*pagetype*/, &CComVariant(FALSE)/*printtofile*/, 
								  &CComVariant(TRUE)/*callote*/, &varEmpty, &CComVariant(FALSE)/*ManualDuplexPrint*/, 
								  &varEmpty,&varEmpty,&varEmpty,&varEmpty);
		#endif
}

//在当前鼠标位置插入图片的功能
void   CFTWordRpt::InsertImage(CString strPicNm)
{
           COleVariant  vTrue((short)TRUE),vFalse((short)FALSE),vOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
					  
           Selection     m_WordSelection     = m_objWordApp.GetSelection();
		   InlineShapes  m_WordInlineShapes  = m_WordSelection.GetInlineShapes();
		   InlineShape   m_WordInlineShape   = m_WordInlineShapes.AddPicture(strPicNm, vFalse, vTrue, vOptional);  
           
		   ///////////////////////////////////////////////////////////
		   /*
           Shape  m_WordShape;  
           m_WordShape=m_WordInlineShape.ConvertToShape();  
           WrapFormat  m_WordWrapFormat;  
           m_WordWrapFormat=m_WordShape.GetWrapFormat();  
           //m_WordWrapFormat.SetAllowOverlap(TRUE);
           m_WordWrapFormat.ReleaseDispatch();  
           m_WordShape.ReleaseDispatch();  
           */

		   ///////////////////////////////////////////////////////////
           m_WordInlineShape.ReleaseDispatch();  
           m_WordInlineShapes.ReleaseDispatch();  
           m_WordSelection.ReleaseDispatch();
}

//保存报表
BOOL	CFTWordRpt::SaveRpt(BOOL bPrint, const char *szRptFile)
{
			if (!m_bInitOk)
				return FALSE;
			
			//存储并释放资源
			m_bInitOk    = FALSE;
			m_nCurrRow   = 2;
			
			VARIANT *pVar	  = &CComVariant(0);
			VARIANT *pVarStr  = &CComVariant(_T(""));
			
			m_objDocu.SaveAs(&CComVariant(szRptFile),	/*file name*/ 
							 &CComVariant(0),			/*fomat document*/ 
							 &CComVariant(0), &CComVariant(_T(""))/*password*/, &CComVariant(0), 
							 &CComVariant(_T("")), 
							 &CComVariant(0), &CComVariant(0), &CComVariant(0), 
							 &CComVariant(0), &CComVariant(0));
			
			if (bPrint)
				this->PrintRpt();

			m_objWordApp.Quit(&CComVariant(-1),		/*savechange*/
							  &CComVariant(0),		/*original format*/
							  &CComVariant(0));
			
			return TRUE;
}

⌨️ 快捷键说明

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