QReadExcelToMap.cpp
资源名称:QGIS.rar [点击查看]
上传用户:oybseng
上传日期:2015-04-27
资源大小:7831k
文件大小:39k
源码类别:
GDI/图象编程
开发平台:
Visual C++
- #include "stdafx.h"
- #include "QReadExcelToMap.h"
- #include "QObjects/include/QLayerObj.h"
- #include "QObjects/include/QMapObj.h"
- #include "comdef.h"
- #include "comutil.h"
- #include "afxtempl.h"
- using namespace std;
- //const double c_ColExcelToMM = 2.387;
- //const double c_RowExcelToMM = 0.351;
- #define COLEXCELTOMM 2.387
- #define ROWEXCELTOMM 0.351
- CQReadExcelCell::CQReadExcelCell():m_cMoveOff(0.8)
- {
- m_ptBegin = CQPoint(0.0f,0.0f); // 单元格的起点坐标
- m_ptEnd = CQPoint(0.0f,0.0f);
- m_ptText = CQPoint(0.0f,0.0f);
- m_pObjLabel = NULL; // 单元格中的值对象
- //CQLineObj * m_pGridLine; // 网格线对象指针
- m_fCellWidth = 0.0f; // 指定列的宽
- m_fCellHeight = 0.0f; // 本行单元格的高
- m_nHAllignFormat = 0; // 水平对齐方式0-左对齐1-居中2-右对齐
- m_nVAllignFormat = 1; // 垂直对齐方式0-靠上 1-居中 2-靠下
- m_nMergeCellRows = 0; // 合并的单元格的行数
- m_nMergeCellCols = 0; // 合并的单元格的列数
- m_bCellMerge = FALSE; // 是否为合并单元
- m_szCellText = "";
- }
- CQReadExcelCell::CQReadExcelCell(CQCoordSys * pCoordObj):m_cMoveOff(0.8)
- {
- m_ptBegin = CQPoint(0.0f,0.0f); // 单元格的起点坐标
- m_ptEnd = CQPoint(0.0f,0.0f);
- m_ptText = CQPoint(0.0f,0.0f);
- m_pObjLabel = NULL; // 单元格中的值对象
- //CQLineObj * m_pGridLine; // 网格线对象指针
- m_fCellWidth = 0.0f; // 指定列的宽
- m_fCellHeight = 0.0f; // 本行单元格的高
- m_nHAllignFormat = 0; // 水平对齐方式0-左对齐1-居中2-右对齐
- m_nVAllignFormat = 1; // 垂直对齐方式0-靠上 1-居中 2-靠下
- m_nMergeCellRows = 0; // 合并的单元格的行数
- m_nMergeCellCols = 0; // 合并的单元格的列数
- m_bCellMerge = FALSE; // 是否为合并单元格
- m_szCellText = "";
- if(!pCoordObj) return;
- m_pCoordObj = pCoordObj; // 获取当前工程坐标系统对象的指针
- }
- CQReadExcelCell::~CQReadExcelCell()
- {
- }
- LOGFONT CQReadExcelCell::GetCellTextfont()
- {
- return m_CellFont;
- }
- COLORREF CQReadExcelCell::GetCellTextColor()
- {
- return m_crCellColor;
- }
- float CQReadExcelCell::GetCellTextFontSize()
- {
- return m_fCellFontSize;
- }
- void CQReadExcelCell::MakeCellNextRowPt(CQPoint & ptNext,float fRowHeight,long nMergeRows,CQCoordSys * pCoordObj /* = NULL */)
- {
- if(!pCoordObj && !m_pCoordObj)
- return;
- if(pCoordObj != NULL)
- {
- CQPoint ptNewBegin = m_ptBegin;
- if(nMergeRows == 0)
- nMergeRows = 1;
- double dy = ptNewBegin.GetY();
- dy -= pCoordObj->MMtoWL(fRowHeight*nMergeRows);
- ptNewBegin.SetY(dy);
- ptNext = ptNewBegin;
- }
- else if(m_pCoordObj)
- {
- CQPoint ptNewBegin = m_ptBegin;
- if(nMergeRows == 0)
- nMergeRows = 1;
- double dy = ptNewBegin.GetY();
- dy -= m_pCoordObj->MMtoWL(fRowHeight*nMergeRows);
- ptNewBegin.SetY(dy);
- ptNext = ptNewBegin;
- }
- }
- void CQReadExcelCell::MakeCellNextColPt(CQPoint & ptNext,float fColWidth,long nMergeCols,CQCoordSys * pCoordObj /* = NULL */)
- {
- if(!pCoordObj && !m_pCoordObj)
- return;
- if(pCoordObj != NULL)
- {
- CQPoint ptNextCol = m_ptBegin;
- if(nMergeCols == 0)
- nMergeCols = 1;
- double dx = ptNextCol.GetX();
- dx += pCoordObj->MMtoWL(nMergeCols*fColWidth);
- ptNextCol.SetX(dx);
- ptNext = ptNextCol;
- }
- else if(m_pCoordObj)
- {
- CQPoint ptNextCol = m_ptBegin;
- if(nMergeCols == 0)
- nMergeCols = 1;
- double dx = ptNextCol.GetX();
- dx += pCoordObj->MMtoWL(nMergeCols*fColWidth);
- ptNextCol.SetX(dx); ptNext = ptNextCol;
- }
- }
- CQReadExcelCell & CQReadExcelCell::operator=(CQReadExcelCell & ReadExcel)
- {
- if(&ReadExcel == this)
- return *this;
- m_CellFont = ReadExcel.GetCellTextfont();
- m_crCellColor = ReadExcel.GetCellTextColor();
- m_fCellFontSize = ReadExcel.GetCellTextFontSize();
- m_fCellHeight = ReadExcel.GetCellHeight();
- m_fCellWidth = ReadExcel.GetCellWidth();
- m_nHAllignFormat = ReadExcel.GetCellHAllignFormat("");
- m_nVAllignFormat = ReadExcel.GetCellVAllignFormat("");
- m_nMergeCellCols = ReadExcel.GetMergeCols();
- m_nMergeCellRows = ReadExcel.GetMergeRows();
- m_pCoordObj = ReadExcel.m_pCoordObj;
- ReadExcel.GetCellBeginPt(m_ptBegin);
- m_szCellText = ReadExcel.GetCellText();
- m_bCellMerge = ReadExcel.IsCellMerge();
- InitCell(m_ptBegin,m_szCellText,m_fCellHeight,m_fCellWidth,m_bCellMerge
- ,m_nMergeCellRows,m_nMergeCellCols,m_nHAllignFormat,m_nVAllignFormat
- ,m_CellFont,m_crCellColor,m_fCellFontSize,m_pCoordObj);
- return *this;
- }
- BOOL CQReadExcelCell::SetCoordSys(CQCoordSys * pCoordObj)
- {
- if(!pCoordObj)return FALSE;
- m_pCoordObj = pCoordObj;
- return TRUE;
- }
- void CQReadExcelCell::InitCell(CQPoint ptBegin,CString szText,float fRowHeight,float fColWidth,
- BOOL bCellMegre,long nMergeRows,long nMergeCols,BYTE nHAllignFormat,
- BYTE nVAlligFormat,LOGFONT font,
- COLORREF crFont,float fFontSize,CQCoordSys * pCoodObj)
- {
- if(pCoodObj != NULL)
- m_pCoordObj = pCoodObj;
- m_ptBegin = ptBegin;
- m_szCellText = szText;
- //由Excel的磅值单位转换为毫米单位好进行计算
- //在系统外面改过了
- /*m_fCellWidth = static_cast<float>(CGeoGlobalFunction::ConvertFontPttoMM(fColWidth));
- m_fCellHeight = static_cast<float>(CGeoGlobalFunction::ConvertFontPttoMM(fRowHeight)); */
- m_fCellHeight = fRowHeight;
- m_fCellWidth = fColWidth;
- m_bCellMerge = bCellMegre; // 合并单元格的属性
- m_nMergeCellCols = nMergeCols;
- m_nMergeCellRows = nMergeRows; // 合并单元格的行数和列数
- m_nHAllignFormat = nHAllignFormat;
- m_nVAllignFormat = nVAlligFormat;
- m_CellFont = font; // 文本字体
- m_crCellColor = crFont; // 字体颜色
- m_fCellFontSize = fFontSize; //字号
- m_pObjLabel = new CQTextObj;
- if(!m_pObjLabel)return;
- LOGFONT lFont = m_pObjLabel->GetFont();
- strcpy((char *)lFont.lfFaceName,"黑体"); //设置字体
- m_pObjLabel->SetText(szText);
- m_szCellText = szText;
- if(nMergeCols == 0)
- nMergeCols = 1;
- double fTextWidth = static_cast<double>(fColWidth*nMergeCols);
- m_pObjLabel->SetFont(lFont);
- if(m_fCellFontSize == 0) m_fCellFontSize = 12; //Excel中默认为12
- m_pObjLabel->SetFontSize(m_fCellFontSize);
- fTextWidth = m_pCoordObj->MMtoWL(fTextWidth-2);
- m_pObjLabel->SetRowWidth(fTextWidth,m_pCoordObj);
- m_pObjLabel->SetObjPenColor(m_crCellColor); // 设置画笔颜色
- }
- void CQReadExcelCell::GetCellBeginPt(double & dx,double & dy)
- {
- dx = m_ptBegin.GetX();
- dy = m_ptBegin.GetY();
- }
- //获取起始点的坐标
- void CQReadExcelCell::GetCellBeginPt(CQPoint & pt)
- {
- pt.SetX(m_ptBegin.GetX());
- pt.SetY(m_ptBegin.GetY());
- }
- // 获取水平对齐方式
- BYTE CQReadExcelCell::GetCellHAllignFormat(CString szHAllign)
- {
- // 默认为左对齐
- if(m_nHAllignFormat<0 || m_nHAllignFormat>2) m_nHAllignFormat = 0;
- if(szHAllign == "")
- return m_nHAllignFormat;
- switch(m_nHAllignFormat)
- {
- case 0:
- {
- szHAllign = _T("左对齐");
- break;
- }
- case 1:
- {
- szHAllign = _T("居中");
- break;
- }
- case 2:
- {
- szHAllign = _T("右对齐");
- break;
- }
- default:
- {
- szHAllign = _T("左对齐");
- break;
- }
- }
- return m_nHAllignFormat;
- }
- // 获取垂直对齐方式
- BYTE CQReadExcelCell::GetCellVAllignFormat(CString szVAllign)
- {
- // 默认为居中
- if(m_nVAllignFormat<0 || m_nVAllignFormat>2) m_nVAllignFormat = 1;
- if(szVAllign == "")
- return m_nVAllignFormat;
- switch(m_nHAllignFormat)
- {
- case 0:
- {
- szVAllign = _T("左对齐");
- break;
- }
- case 1:
- {
- szVAllign = _T("居中");
- break;
- }
- case 2:
- {
- szVAllign = _T("右对齐");
- break;
- }
- default:
- {
- szVAllign = _T("左对齐");
- break;
- }
- }
- return m_nVAllignFormat;
- }
- // 获取单元格的高
- float CQReadExcelCell::GetCellHeight()
- {
- return m_fCellHeight;
- }
- // 获取单元格的宽
- float CQReadExcelCell::GetCellWidth()
- {
- return m_fCellWidth;
- }
- // 获取合并单元格的列数
- long CQReadExcelCell::GetMergeCols()
- {
- return m_nMergeCellCols;
- }
- //获取合并单元格的行数
- long CQReadExcelCell::GetMergeRows()
- {
- return m_nMergeCellRows;
- }
- // 获取单元格是否合并
- BOOL CQReadExcelCell::IsCellMerge()
- {
- return m_bCellMerge?TRUE:FALSE;
- }
- //设置文本的字体信息
- void CQReadExcelCell::SetLabelFont(LOGFONT lFont)
- {
- m_CellFont = lFont;
- if(m_pObjLabel && m_pCoordObj)
- m_pObjLabel->SetFont(lFont);
- }
- //设置文本的大小
- void CQReadExcelCell::SetLabelPtSize(float fSize)
- {
- m_fCellFontSize = fSize;
- if(m_fCellFontSize == 0)
- m_fCellFontSize = 12.0f;
- if(m_pCoordObj && m_pObjLabel)
- m_pObjLabel->SetFontSize(m_fCellFontSize);
- }
- //设置字体颜色
- void CQReadExcelCell::SetLabelColor(COLORREF lbCr)
- {
- m_crCellColor = lbCr;
- if(m_pCoordObj && m_pObjLabel)
- m_pObjLabel->SetObjPenColor(m_crCellColor);
- }
- //设置文本内容
- void CQReadExcelCell::SetLabelText(CString szText)
- {
- m_szCellText = szText;
- if(m_pObjLabel)
- m_pObjLabel->SetText(m_szCellText);
- }
- //获取文本对象
- CQTextObj * CQReadExcelCell::GetCellLabelObj()
- {
- if(m_pObjLabel)
- return m_pObjLabel;
- else
- return NULL;
- }
- //获取文本对象的内容
- CString CQReadExcelCell::GetCellText()
- {
- if(m_pObjLabel)
- return m_pObjLabel->GetText();
- else
- return m_szCellText;
- }
- //计算单元格结束点的坐标
- void CQReadExcelCell::CalCellEndPt(float fRowHeight,float fColWidth,long lMergeRows,long lMergeCols,CQCoordSys * pCoordObj /* = NULL */)
- {
- if(!pCoordObj && !m_pCoordObj)
- return;
- if(lMergeCols == 0)
- lMergeCols = 1;
- if(lMergeRows == 0)
- lMergeRows = 1;
- CQPoint ptEnd;
- if(pCoordObj != NULL)
- {
- double dx = m_ptBegin.GetX() + pCoordObj->MMtoWL(fColWidth*lMergeCols);
- double dy = m_ptBegin.GetY() - pCoordObj->MMtoWL(fRowHeight*lMergeRows);
- ptEnd.SetPoint(dx,dy);
- m_ptEnd = ptEnd;
- }
- else if(m_pCoordObj)
- {
- double dx = m_ptBegin.GetX() + pCoordObj->MMtoWL(fColWidth*lMergeCols);
- double dy = m_ptBegin.GetY() - pCoordObj->MMtoWL(fRowHeight*lMergeRows);
- ptEnd.SetPoint(dx,dy);
- m_ptEnd = ptEnd;
- }
- }
- // 根据水平对齐方式设置文本的X坐标
- void CQReadExcelCell::MakeCellXPt(CQPoint & ptBeign,BYTE nHAllignFormat,float fColWidth,long nMergeCols,CQCoordSys * pCoordObj)
- {
- //if(!m_pObjLabel) return; // 假如标住对象不存在就退出
- if(nHAllignFormat<0 || nHAllignFormat>2) nHAllignFormat = 0; //默认为左对齐
- if(!m_pCoordObj) return;
- if(fColWidth == 0)
- fColWidth = m_fCellWidth;
- if(nMergeCols == 0)
- nMergeCols = 1;
- double ddx = 0.0f;
- switch(nHAllignFormat)
- {
- case 0: //左对齐
- {
- ddx += pCoordObj->MMtoWL(m_cMoveOff); // 偏移0.8MM
- double dx = ptBeign.GetX();
- dx += ddx;
- ptBeign.SetX(dx);
- break;
- }
- case 1: //居中
- {
- CBoundaryRect bRect;
- m_pObjLabel->CalculateBoundary(&bRect);
- double dRectWidth = bRect.Width();
- double dNewX = ptBeign.GetX() + (pCoordObj->MMtoWL(fColWidth*nMergeCols)-dRectWidth)/2.0;
- ptBeign.SetX(dNewX);
- break;
- }
- case 2://右对齐
- {
- ddx += pCoordObj->MMtoWL(fColWidth*nMergeCols); //MMToWL
- ddx -= pCoordObj->MMtoWL(m_cMoveOff);
- double dx = ptBeign.GetX();
- dx += ddx;
- ptBeign.SetX(dx);
- break;
- }
- }
- }
- // 根据垂直对齐方式设置文本的Y坐标
- void CQReadExcelCell::MakeCellYPt(CQPoint &ptBeign,BYTE nVAllignFormat,float fRowHeight,long nMergeRows,CQCoordSys * pCoordObj)
- {
- //if(!m_pObjLabel) return; // 假如标住对象不存在就退出
- //默认为靠下
- if(nVAllignFormat<0 || nVAllignFormat>2) nVAllignFormat = 0;
- if(!m_pCoordObj) return;
- if(fRowHeight == 0)
- fRowHeight = m_fCellHeight;
- if(nMergeRows == 0)
- nMergeRows = 1;
- switch(nVAllignFormat)
- {
- case 0: //靠上
- {
- double dy = ptBeign.GetY();
- dy -= pCoordObj->MMtoWL(m_cMoveOff); // 偏移0.8MM
- ptBeign.SetY(dy);
- break;
- }
- case 1: //居中
- {
- CBoundaryRect bRect;
- m_pObjLabel->CalculateBoundary(&bRect);
- double dRectHeight = bRect.Height();
- double dNewY = ptBeign.GetY() - pCoordObj->MMtoWL(fRowHeight*nMergeRows) + (pCoordObj->MMtoWL(fRowHeight*nMergeRows)-dRectHeight)/2.0;
- ptBeign.SetY(dNewY);
- break;
- }
- case 2://靠下
- {
- double dx = ptBeign.GetX();
- double dy = ptBeign.GetY();
- dx -= pCoordObj->MMtoWL(fRowHeight*nMergeRows); //MMToWL
- dy += pCoordObj->MMtoWL(m_cMoveOff);
- ptBeign.SetPoint(dx,dy);
- break;
- }
- }
- }
- // 计算文本的正确起点位置
- // 这也是一个关键的函数
- //靠上、靠下、左对齐、右对齐均设计为0.8毫米 不理想的话可以在调整
- void CQReadExcelCell::CalTextPtCoord()
- {
- if(!m_pObjLabel) return;
- m_ptText = m_ptBegin;
- MakeCellXPt(m_ptText,m_nHAllignFormat,m_fCellWidth,m_nMergeCellCols,m_pCoordObj);
- MakeCellYPt(m_ptText,m_nVAllignFormat,m_fCellHeight,m_nMergeCellRows,m_pCoordObj);
- m_pObjLabel->SetPos(m_ptText);
- }
- //***********************************************************
- //****** QQ添加代码 实现读入Excel ******
- //****** 2007-10-22 ******
- //***********************************************************
- CQReadExcelToView::CQReadExcelToView(CQMapObj * pMap,CQCoordSys * pCoordObj,CQPoint ptBegin)
- {
- if(!pMap) return;
- if(!pCoordObj)return;
- m_pMap = pMap; // 获取工程的当前图幅
- m_pCoordObj = pCoordObj; // 获取当前工程的坐标系统对象
- m_fColWidth = 0.0f; // 指定列的宽
- m_fRowHeight = 0.0f; // 指定行的高
- m_nRowCount = 0; // 记录行数
- m_nColCount = 0; // 记录列数
- m_nStartCol = m_nStartRow = 0;
- m_ptBegin = ptBegin;
- m_pCurNewLayer = NULL;
- }
- CQReadExcelToView::~CQReadExcelToView()
- {
- if(!m_pExcelApp) return;
- m_ExcelBook.Close();
- m_ColRange.ReleaseDispatch();
- m_RowRange.ReleaseDispatch();
- m_myallrange.ReleaseDispatch();
- m_UsedRange.ReleaseDispatch();
- m_workSheet.ReleaseDispatch();
- m_ExcelSheet.ReleaseDispatch();
- m_workBook.ReleaseDispatch();
- m_ExcelBook.ReleaseDispatch();
- m_pExcelApp->ReleaseDispatch();
- m_pExcelApp->Quit();
- delete m_pExcelApp;
- m_pExcelApp = NULL;
- }
- BOOL CQReadExcelToView::InitExcel()
- {
- //初始化COM的动态连接库
- m_pExcelApp = new _Application;
- BOOL bIsSuccess = m_pExcelApp->CreateDispatch("excel.application");
- if (!bIsSuccess)
- {
- AfxMessageBox("系统中未安装office软件, 无法写入EXCEL文件!",MB_OK);
- return FALSE;
- }
- //m_pExcelApp->SetVisible(TRUE);
- //m_pExcelApp->SetUserControl(TRUE);
- return TRUE;
- }
- //计算下一行的起点
- void CQReadExcelToView::MakeNextRowStartPt(CQPoint & ptNext,CQPoint & ptPre,float fRowHeight,long nMergeRows,CQCoordSys * pCoordObj)
- {
- if(!pCoordObj && !m_pCoordObj)
- return;
- if(nMergeRows == 0)
- nMergeRows = 1;
- if(pCoordObj)
- {
- double dx = 0,dy = 0;
- dy = ptPre.GetY() - pCoordObj->MMtoWL(fRowHeight*nMergeRows);
- dx = ptPre.GetX();
- ptNext.SetPoint(dx,dy);
- }
- else
- {
- double dx = 0,dy = 0;
- dy = ptPre.GetY() - m_pCoordObj->MMtoWL(fRowHeight*nMergeRows);
- dx = ptPre.GetX();
- ptNext.SetPoint(dx,dy);
- }
- }
- //获取指定单元格的值
- CString CQReadExcelToView::GetExcelCellValue(Range & geoRange,long nRowIndex,long nColIndex)
- {
- CString szResult = "";
- try
- {
- Range cellRange,IndexCell;
- cellRange.AttachDispatch(geoRange.GetCells());
- IndexCell.AttachDispatch(cellRange.GetItem(COleVariant(nRowIndex),COleVariant(nColIndex)).pdispVal);
- COleVariant vResult = IndexCell.GetValue();
- CString str;
- if(vResult.vt == VT_BSTR) //字符串
- {
- str=vResult.bstrVal;
- }
- else if(vResult.vt == VT_R8) //8字节的数字
- {
- str.Format("%.2f",vResult.dblVal);
- }
- else if(vResult.vt == VT_DATE) //时间格式
- {
- str.Format("%.2f",vResult.date);
- }
- else if(vResult.vt == VT_EMPTY) //单元格空的
- {
- }
- szResult = str;
- cellRange.ReleaseDispatch();
- IndexCell.ReleaseDispatch();
- }
- catch (_com_error err)
- {
- CString szErr;
- szErr.Format("读取第%l行,第%l列单元格值出现错误!");
- szErr += CString(err.ErrorMessage());
- AfxMessageBox(szErr);
- return CString("");
- }
- return szResult;
- }
- //获取单元格水平对齐格式
- BYTE CQReadExcelToView::GetHAllignFormat(Range & geoRange,long nRowIndex,long nColIndex)
- {
- BYTE nHAllignformat = 0;
- try
- {
- Range cellRange,IndexCell;
- cellRange.AttachDispatch(geoRange.GetCells());
- IndexCell.AttachDispatch(cellRange.GetItem(COleVariant(nRowIndex),COleVariant(nColIndex)).pdispVal);
- COleVariant vResult;
- vResult.lVal = 0;
- vResult = IndexCell.GetHorizontalAlignment(); //获取水平对齐方式
- if(vResult.lVal!=0)
- {
- switch(vResult.lVal)
- {
- case 1: //默认
- {
- nHAllignformat = 1; //默认为居中
- break;
- }
- case -4108: //居中
- {
- nHAllignformat = 1;
- break;
- }
- case -4131: //靠左
- {
- nHAllignformat = 0;
- break;
- }
- case -4152: //靠右
- {
- nHAllignformat = 2;
- break;
- }
- }
- }
- cellRange.ReleaseDispatch();
- IndexCell.ReleaseDispatch();
- }
- catch (_com_error err)
- {
- CString szErr;
- szErr.Format("读取第%l行,第%l列单元格值出现错误!");
- szErr += CString(err.ErrorMessage());
- AfxMessageBox(szErr);
- return 0;
- }
- return nHAllignformat;
- }
- //获取单元格垂直对齐格式
- BYTE CQReadExcelToView::GetVAllignFormat(Range & geoRange,long nRowIndex,long nColIndex)
- {
- BYTE nVAllignFormat = 1;
- try
- {
- Range cellRange,IndexCell;
- cellRange.AttachDispatch(geoRange.GetCells());
- IndexCell.AttachDispatch(cellRange.GetItem(COleVariant(nRowIndex),COleVariant(nColIndex)).pdispVal);
- COleVariant vResult;
- vResult.lVal = 0;
- vResult = IndexCell.GetVerticalAlignment(); //获取垂直对齐方式
- if(vResult.lVal!=0)
- {
- switch(vResult.lVal)
- {
- case -4160: //靠上
- {
- nVAllignFormat = 0;
- break;
- }
- case -4108: //居中
- {
- nVAllignFormat = 1;
- break;
- }
- case -4107: //靠下
- {
- nVAllignFormat = 2;
- break;
- }
- default:
- {
- nVAllignFormat = 1;
- break;
- }
- }
- }
- cellRange.ReleaseDispatch();
- IndexCell.ReleaseDispatch();
- }
- catch (_com_error err)
- {
- CString szErr;
- szErr.Format("读取第%l行,第%l列单元格值出现错误!");
- szErr += CString(err.ErrorMessage());
- AfxMessageBox(szErr);
- return 0;
- }
- return nVAllignFormat;
- }
- //获取合并单元格的情况
- //long & nMergeRows,long & nMergeCols是出口参数 获取合并单元格的行列数
- //long & nMergeStartRow,long & nMergeStartCol 返回合并单元格的起始行、列号
- //合并单元格的函数要重写
- BOOL CQReadExcelToView::GetMergeCellFormat(Range & geoRange,long nRowIndex,long nColIndex,long & nMergerows,long & nMergeCols,long & nMergeStartRow,long &nMergeStartCol)
- {
- BOOL bMerge = FALSE;
- try
- {
- Range cellRange,IndexCell,UnionRange;
- cellRange.AttachDispatch(geoRange.GetCells());
- IndexCell.AttachDispatch(cellRange.GetItem(COleVariant(nRowIndex)
- ,COleVariant(nColIndex)).pdispVal);
- UnionRange.AttachDispatch(IndexCell.GetMergeArea());
- int nColTimes = 0;
- Range rangeRow,rangeCol;
- COleVariant vResult = UnionRange.GetMergeCells(); //获取合并单元格属性
- if(vResult.boolVal == 0)
- {
- bMerge = FALSE;
- nMergeCols = nMergerows = 0;
- nMergeStartRow = nMergeStartCol = -1;
- return FALSE;
- }
- else
- {
- long nColMerges = 0,nRowMerges = 0;
- rangeRow.AttachDispatch(UnionRange.GetRows());
- rangeCol.AttachDispatch(UnionRange.GetColumns());
- nMergeStartRow = rangeRow.GetRow(); //获取合并单元格的起始行
- nMergeStartCol = rangeCol.GetColumn(); //获取合并单元格的起始列
- nRowMerges = rangeRow.GetCount(); //获取合并单元格的行数
- nColMerges = rangeCol.GetCount(); //获取合并单元格的列数
- int nMergeVtSize = m_stMergeVector.size();
- //判断该信息单元格是否处于其他合并单元格中
- //因为列合并已经跳过去了
- //所以在这里对行情况进行处理
- BOOL BInMergeCell = FALSE;
- for(int mm=0;mm<nMergeVtSize;mm++)
- {
- if((nRowIndex>= m_stMergeVector[mm].st_StartRow &&
- nRowIndex<(m_stMergeVector[mm].st_StartRow +
- m_stMergeVector[mm].st_MergeRows)) &&
- (nColIndex>= m_stMergeVector[mm].st_StartCol &&
- nColIndex<(m_stMergeVector[mm].st_StartCol+
- m_stMergeVector[mm].st_MergeCols)))
- {
- BInMergeCell = TRUE;
- nColMerges = m_stMergeVector[mm].st_MergeCols;
- nRowMerges = 1;
- m_stPt.st_bMerge = TRUE;
- if(nColIndex == 1)
- {
- m_stEachFirRow.st_lMergeCols = nColMerges;
- m_stEachFirRow.st_lMergeRows = 1;
- }
- break;
- }
- }
- if(!BInMergeCell)
- {
- m_stMergeInfo.st_StartRow = nMergeStartRow; //记录合并单元格的开始的行号
- m_stMergeInfo.st_StartCol = nMergeStartCol;
- m_stMergeInfo.st_MergeRows = nRowMerges;
- m_stMergeInfo.st_MergeCols = nColMerges;
- m_stMergeVector.push_back(m_stMergeInfo);
- }
- if(nColIndex == 1) //假如是第一列
- {
- m_stEachFirRow.st_lMergeCols = nColMerges;
- m_stEachFirRow.st_lMergeRows = nRowMerges;
- }
- nMergerows = nRowMerges;
- nMergeCols = nColMerges;
- bMerge = TRUE;
- rangeRow.ReleaseDispatch();
- rangeCol.ReleaseDispatch();
- }
- rangeRow.ReleaseDispatch();
- rangeCol.ReleaseDispatch();
- UnionRange.ReleaseDispatch();
- cellRange.ReleaseDispatch();
- IndexCell.ReleaseDispatch();
- }
- catch (_com_error err)
- {
- CString szErr;
- szErr.Format("读取第%l行,第%l列单元格值出现错误!");
- szErr += CString(err.ErrorMessage());
- AfxMessageBox(szErr);
- return FALSE;
- }
- return bMerge;
- }
- //行高: 1Uint = 0.351MM
- double CQReadExcelToView::ExcelRowUintToMM(float fRowHeight)//将Excel的行高单位转换为毫米
- {
- return fRowHeight*ROWEXCELTOMM;
- }
- //1个单位=0.3612毫米
- //列宽: Excel 1Uint = 2.387MM
- double CQReadExcelToView::ExcelColUintToMM(float fColWidth) //将Excel的列宽单位转换为毫米
- {
- return fColWidth*COLEXCELTOMM;
- }
- //获取单元格的字体、行宽、列高
- void CQReadExcelToView::GetCellFont(Range & geoRange,long nRowIndex,long nColIndex,float & fColWidth,float & fRowHeight)
- {
- try
- {
- Range cellRange,IndexCell;
- cellRange.AttachDispatch(geoRange.GetCells());
- IndexCell.AttachDispatch(cellRange.GetItem(COleVariant(nRowIndex),COleVariant(nColIndex)).pdispVal);
- COleVariant vResult;
- vResult.fltVal = 0.0f;
- vResult = IndexCell.GetColumnWidth(); //获取列宽
- fColWidth = static_cast<float>(vResult.dblVal);
- vResult = IndexCell.GetRowHeight(); //获取行高
- fRowHeight = static_cast<float>(vResult.dblVal); //获取的是磅值
- cellRange.ReleaseDispatch();
- IndexCell.ReleaseDispatch();
- }
- catch (_com_error err)
- {
- CString szErr;
- szErr.Format("读取第%l行,第%l列单元格值出现错误!");
- szErr += CString(err.ErrorMessage());
- AfxMessageBox(szErr);
- return;
- }
- }
- //从电脑磁盘读取指定的Excel文件,并对对应的数据结构进行
- BOOL CQReadExcelToView::ReadExcelFromPC()
- {
- if(InitExcel()==FALSE) return FALSE;
- COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,
- VT_ERROR),vtTrue((short)TRUE),vtFalse((short)FALSE);
- LPDISPATCH lpDisp; //OLE变量用于读取Excel文件
- //在磁盘找出要导入的Excel文件
- CFileDialog fd(TRUE,"xls","XLS",OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
- "EXCEL FILES (*.xls)|*.xls|ALL FILES (*.*)|(*.*)||",NULL);
- if (fd.DoModal()!=IDOK) return FALSE;
- CString szExcelFileName = fd.GetPathName();
- if(szExcelFileName.IsEmpty())
- {
- AfxMessageBox("获取的Excel文件名为空");
- return FALSE;
- }
- //打开Excel文件
- try
- {
- m_ExcelBook = m_pExcelApp->GetWorkbooks();
- lpDisp = m_ExcelBook.Open(LPCTSTR(szExcelFileName),vtOptional,vtOptional,vtOptional,vtOptional
- ,vtOptional,vtOptional,vtOptional,vtOptional,vtOptional,vtOptional,vtOptional,vtOptional);
- }
- catch(_com_error comerr)
- {
- CString szEror = CString(comerr.ErrorMessage());
- AfxMessageBox(szEror);
- m_pExcelApp->ReleaseDispatch();
- m_pExcelApp->Quit();
- delete m_pExcelApp;
- m_pExcelApp = NULL;
- return FALSE;
- }
- static int nLayerNum = 0;
- m_pCurNewLayer = new CQLayerObj;
- CString szLayerName = "导入Excel数据表层";
- CString szTimes;
- szTimes.Format("%d",nLayerNum++);
- szLayerName+=szTimes;
- m_pCurNewLayer->SetLayerName(szLayerName); //设置图层名称
- //设置图层描述
- m_pCurNewLayer->SetLayerDis("导入Excel数据表层");
- try
- {
- m_workBook.AttachDispatch(lpDisp);
- //m_workBook.AttachDispatch(m_ExcelBook.GetItem(COleVariant((long)1)));
- //m_ExcelSheet.AttachDispatch(m_workBook.GetWorksheets()); // 需要手动释放
- lpDisp = m_workBook.GetSheets();
- m_workSheet.AttachDispatch(lpDisp);
- //lpDisp = m_ExcelSheet.GetItem(COleVariant((long)1));
- lpDisp = m_workBook.GetActiveSheet(); // 获取活跃的单元格
- m_workSheet.AttachDispatch(lpDisp); // 连接上
- //读取已经使用区域的信息,包括已经使用的行数、列数、起始行、起始列
- //m_UsedRange.AttachDispatch(m_workSheet.GetUsedRange()); //获取已经使用的区域信息
- m_UsedRange.AttachDispatch(m_workSheet.GetUsedRange());
- m_RowRange.AttachDispatch(m_UsedRange.GetRows());
- m_nRowCount = m_RowRange.GetCount(); // 获取已经使用过的单元格的行数
- m_ColRange.AttachDispatch(m_UsedRange.GetColumns());
- m_nColCount = m_ColRange.GetCount(); // 获取已经使用过的单元格的列数
- if((m_nColCount == m_nRowCount) && (m_nColCount == 0))return FALSE;
- long lStartRow = m_UsedRange.GetRow();
- long lStartCol = m_UsedRange.GetColumn(); //获取起始的行号和列号
- CQPoint ptText,ptRow = m_ptBegin,ptNext = m_ptBegin;
- long nMergeColNum = 0; //合并单元格后
- BOOL BInMergeCell = FALSE;
- //每行首列元素的初始化
- m_stEachFirRow.st_ptBeginFirst = m_ptBegin;
- m_stEachFirRow.st_lMergeCols = 0;
- m_stEachFirRow.st_lMergeRows = 0;
- //现在开始获取整个使用过的表单中的单元格的值
- for(long i=lStartRow;i<m_nRowCount+lStartRow;i++) // 行循环过程
- {
- long nMergeRows = 0,nMergeCols = 0,nMergeStartRow,nMergetStartCol;
- float fRowHeight = 0,fColWidth = 0;
- ptRow.SetY(ptNext.GetY());
- for(long j=lStartCol;j<m_nColCount+lStartCol;) // 列循环过程
- {
- CString szText = _T("");
- szText = GetExcelCellValue(m_UsedRange,i,j); //分别获取单元格内的值
- BYTE hAllign = 0; //默认给它个左对齐
- hAllign = GetHAllignFormat(m_UsedRange,i,j); //获取单元格的水平对齐方式
- BYTE vAllign = 1;
- vAllign = GetVAllignFormat(m_UsedRange,i,j); //获取单元格的垂直对齐方式
- //获取了行宽和列高 用来计算每个单元格的起点(在世界坐标下面)
- GetCellFont(m_UsedRange,i,j,fColWidth,fRowHeight);
- if(j == 1)
- {
- m_stEachFirRow.st_lFirRowHeight = fRowHeight; //行高设置
- m_stEachFirRow.st_lFirColWidth = fColWidth; //列宽设置
- }
- fColWidth = static_cast<float>(ExcelColUintToMM(fColWidth)); //列宽
- fRowHeight = static_cast<float>(ExcelRowUintToMM(fRowHeight)); //行高的单位转换
- BOOL bMerge = FALSE;
- bMerge = GetMergeCellFormat(m_UsedRange,i,j,nMergeRows,nMergeCols,nMergeStartRow,nMergetStartCol);
- //在这里获取了合并合并单元格的信息
- LOGFONT lfont;
- strcpy((char *)lfont.lfFaceName,"黑体");
- m_curCell.SetCoordSys(m_pCoordObj);
- if(i == 1 && j == 1)
- {
- m_curCell.InitCell(m_ptBegin,szText,fRowHeight,fColWidth,bMerge,nMergeRows,nMergeCols
- ,hAllign,vAllign,lfont,RGB(76,76,76),12,m_pCoordObj);
- m_curCell.CalTextPtCoord();
- }
- else
- {
- ptText = ptNext;
- m_curCell.InitCell(ptText,szText,fRowHeight,fColWidth,bMerge,nMergeRows,nMergeCols
- ,hAllign,vAllign,lfont,RGB(76,76,76),12,m_pCoordObj); //字高暂时只能设置为12
- m_curCell.CalTextPtCoord(); //计算文本的输出点的坐标
- }
- //m_ReadExcel.Add(m_curCell); //将每一行的元素都存入行向量里
- CQTextObj * pLabel = NULL;
- pLabel = m_curCell.GetCellLabelObj();
- m_curCell.CalCellEndPt(fRowHeight,fColWidth,nMergeRows,nMergeCols,m_pCoordObj);
- if(pLabel != NULL)
- {
- pLabel->CalculateBoundary();
- m_pCurNewLayer->AddObject(pLabel);
- //将网格线的坐标加入到结构变量中
- m_stPt.st_ptBegin = m_curCell.m_ptBegin;
- m_stPt.st_ptEnd = m_curCell.m_ptEnd;
- m_stPt.st_RowNum = i; //记录行号
- m_stPt.st_ColNum = j; //记录列号
- m_stPt.st_ColWidth = fColWidth;
- m_stPt.st_RowHeight = fRowHeight;
- //这里要重新计算合并了的单元格的终点坐标
- m_stVector.push_back(m_stPt);
- m_stPt.Clear();
- }
- CQPoint ptpt;
- ReCalMergedCellEnPt(ptpt,i,j,fRowHeight,fColWidth);
- m_curCell.MakeCellNextColPt(ptNext,fColWidth,nMergeCols,m_pCoordObj);
- //得到合并的单元格的列数
- if(nMergeCols>0)
- {
- j+=nMergeCols;
- }
- else
- {
- j++;
- }
- }
- //ptRow = CalEachRowFirstColPt(fRowHeight);
- //m_vecReadExcel.push_back(m_vecReadExcelRow);//将行向量存入到Excel的映射向量里面
- MakeNextRowStartPt(ptNext,ptRow,fRowHeight,1,m_pCoordObj);
- //ptNext = ptRow;
- }
- if(m_pCurNewLayer->GetObjCount()>0)
- {
- m_pMap->AddLayer(m_pCurNewLayer);
- m_pMap->SetCurLayer(m_pCurNewLayer);
- }
- else
- {
- return FALSE;
- }
- DrawExcelGridLine(m_pMap,m_pCoordObj,m_nRowCount,m_nColCount);
- return TRUE;
- }
- catch (_com_error err)
- {
- CString szEror = "获取Excel数据错误: " + CString(err.ErrorMessage());
- AfxMessageBox(szEror);
- m_pExcelApp->ReleaseDispatch();
- m_pExcelApp->Quit();
- delete m_pExcelApp;
- m_pExcelApp = NULL;
- return FALSE;
- }
- return FALSE;
- }
- //绘制边框线
- //包括内部的网格线和外边框线 并且对外边框线进行加粗设置
- void CQReadExcelToView::DrawExcelGridLine(CQMapObj * pMap,CQCoordSys * pCoordObj,long nExcelRows,long nExcelCols)
- {
- if(!pMap || !pCoordObj)return;
- //传入的表格的总行数和总列数为0则不进行绘制
- if(nExcelCols == 0 && nExcelRows == 0) return;
- if(m_stVector.size() == 0) //点阵中没有数据 则返回
- return;
- int nFlag = 0;
- //绘制边框线的时候有几种特殊的情况
- //1、绘制第1行第1列的时候完整的绘制整个单元格
- //2、绘制第一列的时候要比其他列多绘制左边的一条竖线
- //3、绘制第一行的时候要多绘制一条上方的横线
- CQLineObj * pNewLineLeft = NULL;
- CQLineObj * pNewLineTop = NULL;
- CQLineObj * pNewLineRight = NULL;
- CQLineObj * pNewLineBottom = NULL;
- int nVtcount = m_stVector.size();
- for(int ii=0;ii<nVtcount;ii++)
- {
- if(m_stVector[ii].st_bMerge == TRUE)
- continue; //假如是被合并的单元格 就不画了
- //第一行,第一列的处理
- //只画右边框和下边框线
- if(m_stVector[ii].st_RowNum == 1 && m_stVector[ii].st_ColNum == 1)
- {
- CQPoint pt1 = m_stVector[ii].st_ptBegin;
- CQPoint pt2;
- pt2.SetX((m_stVector[ii].st_ptEnd).GetX());
- pt2.SetY(pt1.GetY());
- CQPoint pt3 = m_stVector[ii].st_ptEnd;
- CQPoint pt4;
- pt4.SetX(pt1.GetX());
- pt4.SetY(pt3.GetY());
- CQPoint ptEndLast = m_stVector[nVtcount-1].st_ptEnd;
- double fLastY = ptEndLast.GetY();
- // 如果第一行合并单元格后右下角点为最后的边框点
- if(pt3.GetY() == fLastY)
- {
- pNewLineRight = new CQLineObj;
- pNewLineRight->m_PtList.AddPoint(pt2.GetX(),pt2.GetY());
- pNewLineRight->m_PtList.AddPoint(pt3.GetX(),pt3.GetY());
- pNewLineRight->CalculateBoundary();
- pMap->AddObject(pNewLineRight);
- }
- else
- {
- pNewLineRight = new CQLineObj;
- pNewLineBottom = new CQLineObj;
- pNewLineRight->m_PtList.AddPoint(pt2.GetX(),pt2.GetY());
- pNewLineRight->m_PtList.AddPoint(pt3.GetX(),pt3.GetY());
- pNewLineRight->CalculateBoundary();
- pMap->AddObject(pNewLineRight);
- pNewLineBottom->m_PtList.AddPoint(pt3.GetX(),pt3.GetY());
- pNewLineBottom->m_PtList.AddPoint(pt4.GetX(),pt4.GetY());
- pNewLineBottom->CalculateBoundary();
- pMap->AddObject(pNewLineBottom);
- }
- }
- //非最后一行的其余行的第一列
- ////只画右边框和下边框线
- else if(m_stVector[ii].st_RowNum>1 && m_stVector[ii].st_ColNum==1 && m_stVector[ii].st_RowNum < nExcelRows)
- {
- CQPoint pt1 = m_stVector[ii].st_ptBegin;
- CQPoint pt2;
- pt2.SetY(pt1.GetY());
- pt2.SetX((m_stVector[ii].st_ptEnd).GetY());
- CQPoint pt3 = m_stVector[ii].st_ptEnd;
- CQPoint pt4;
- pt4.SetX(pt1.GetX());
- pt4.SetY(pt3.GetY());
- pNewLineRight = new CQLineObj;
- pNewLineBottom = new CQLineObj;
- pNewLineRight->m_PtList.AddPoint(pt2.GetX(),pt2.GetY());
- pNewLineRight->m_PtList.AddPoint(pt3.GetX(),pt3.GetY());
- pNewLineRight->CalculateBoundary();
- pMap->AddObject(pNewLineRight);
- pNewLineBottom->m_PtList.AddPoint(pt3.GetX(),pt3.GetY());
- pNewLineBottom->m_PtList.AddPoint(pt4.GetX(),pt4.GetY());
- pNewLineBottom->CalculateBoundary();
- pMap->AddObject(pNewLineBottom);
- }
- //对第一行非最后一列的其他列的处理
- ////只画右边框和下边框线
- else if(m_stVector[ii].st_RowNum == 1 && m_stVector[ii].st_ColNum>1 && m_stVector[ii].st_ColNum < nExcelCols)
- {
- CQPoint pt1 = m_stVector[ii].st_ptBegin;
- CQPoint pt2;
- pt2.SetY(pt1.GetY());
- pt2.SetX((m_stVector[ii].st_ptEnd).GetX());
- CQPoint pt3 = m_stVector[ii].st_ptEnd;
- CQPoint pt4;
- pt4.SetX(pt1.GetX());
- pt4.SetY(pt3.GetY());
- pNewLineRight = new CQLineObj;
- pNewLineBottom = new CQLineObj;
- pNewLineRight->m_PtList.AddPoint(pt2.GetX(),pt2.GetY());
- pNewLineRight->m_PtList.AddPoint(pt3.GetX(),pt3.GetY());
- pNewLineRight->CalculateBoundary();
- pMap->AddObject(pNewLineRight);
- pNewLineBottom->m_PtList.AddPoint(pt3.GetX(),pt3.GetY());
- pNewLineBottom->m_PtList.AddPoint(pt4.GetX(),pt4.GetY());
- pNewLineBottom->CalculateBoundary();
- pMap->AddObject(pNewLineBottom);
- }
- //最后一行非最后一列的处理
- //只绘制右边框线2
- else if(m_stVector[ii].st_RowNum == nExcelRows && m_stVector[ii].st_ColNum < nExcelCols)
- {
- CQPoint pt1 = m_stVector[ii].st_ptBegin;
- CQPoint pt2;
- pt2.SetY(pt1.GetY());
- pt2.SetX((m_stVector[ii].st_ptEnd).GetX());
- CQPoint pt3 = m_stVector[ii].st_ptEnd;
- pNewLineRight = new CQLineObj;
- pNewLineRight->m_PtList.AddPoint(pt2.GetX(),pt2.GetY());
- pNewLineRight->m_PtList.AddPoint(pt3.GetX(),pt3.GetY());
- pNewLineRight->CalculateBoundary();
- pMap->AddObject(pNewLineRight);
- }
- //最后一列非最后一行的处理
- //只画下面的边框线
- else if(m_stVector[ii].st_RowNum < nExcelRows && m_stVector[ii].st_ColNum == nExcelCols)
- {
- CQPoint pt1 = m_stVector[ii].st_ptBegin;
- CQPoint pt2;
- pt2.SetY(pt1.GetY());
- pt2.SetX((m_stVector[ii].st_ptEnd).GetX());
- CQPoint pt3 = m_stVector[ii].st_ptEnd;
- CQPoint pt4;
- pt4.SetX(pt1.GetX());
- pt4.SetY(pt3.GetY());
- pNewLineBottom = new CQLineObj;
- pNewLineBottom->m_PtList.AddPoint(pt3.GetX(),pt3.GetY());
- pNewLineBottom->m_PtList.AddPoint(pt4.GetX(),pt4.GetY());
- pNewLineBottom->CalculateBoundary();
- pMap->AddObject(pNewLineBottom);
- }
- //其余情况的处理
- ////只画右边框和下边框线
- else if(m_stVector[ii].st_ColNum < nExcelCols && m_stVector[ii].st_RowNum < nExcelRows &&
- m_stVector[ii].st_ColNum != 1 && m_stVector[ii].st_RowNum != 1)
- {
- CQPoint pt2;
- pt2.SetY((m_stVector[ii].st_ptBegin).GetY());
- pt2.SetX((m_stVector[ii].st_ptEnd).GetX());
- CQPoint pt3 = m_stVector[ii].st_ptEnd;
- CQPoint pt4;
- pt4.SetX((m_stVector[ii].st_ptBegin).GetX());
- pt4.SetY(pt3.GetY());
- CQPoint ptEndLast = m_stVector[nVtcount-1].st_ptEnd;
- double fLastY = ptEndLast.GetY();
- // 如果第一行合并单元格后右下角点为最后的边框点
- if(pt3.GetY() == fLastY)
- {
- pNewLineRight = new CQLineObj;
- pNewLineRight->m_PtList.AddPoint(pt2.GetX(),pt2.GetY());
- pNewLineRight->m_PtList.AddPoint(pt3.GetX(),pt3.GetY());
- pNewLineRight->CalculateBoundary();
- pMap->AddObject(pNewLineRight);
- }
- else
- {
- pNewLineRight = new CQLineObj;
- pNewLineBottom = new CQLineObj;
- pNewLineRight->m_PtList.AddPoint(pt2.GetX(),pt2.GetY());
- pNewLineRight->m_PtList.AddPoint(pt3.GetX(),pt3.GetY());
- pNewLineRight->CalculateBoundary();
- pMap->AddObject(pNewLineRight);
- pNewLineBottom->m_PtList.AddPoint(pt3.GetX(),pt3.GetY());
- pNewLineBottom->m_PtList.AddPoint(pt4.GetX(),pt4.GetY());
- pNewLineBottom->CalculateBoundary();
- pMap->AddObject(pNewLineBottom);
- }
- }
- }
- //最后绘制外边框线
- CQPoint ptLeftTop = m_stVector[0].st_ptBegin; //获取左上角的坐标
- CQPoint ptRightBottom = m_stVector[nVtcount-1].st_ptEnd;//获取右下角的坐标
- CQPoint ptRightTop;
- ptRightTop.SetX(ptRightBottom.GetX());
- ptRightTop.SetY(ptLeftTop.GetY());
- CQPoint ptLeftBottom;
- ptLeftBottom.SetX(ptLeftTop.GetX());
- ptLeftBottom.SetY(ptRightBottom.GetY());
- pNewLineTop = new CQLineObj;
- pNewLineTop->m_PtList.AddPoint(ptLeftTop.GetX(),ptLeftTop.GetY());
- pNewLineTop->m_PtList.AddPoint(ptRightTop.GetX(),ptRightTop.GetY());
- pNewLineTop->CalculateBoundary();
- pMap->AddObject(pNewLineTop);
- pNewLineRight = new CQLineObj;
- pNewLineRight->m_PtList.AddPoint(ptRightTop.GetX(),ptRightTop.GetY());
- pNewLineRight->m_PtList.AddPoint(ptRightBottom.GetX(),ptRightBottom.GetY());
- pNewLineRight->CalculateBoundary();
- pMap->AddObject(pNewLineRight);
- pNewLineBottom = new CQLineObj;
- pNewLineBottom->m_PtList.AddPoint(ptRightBottom.GetX(),ptRightBottom.GetY());
- pNewLineBottom->m_PtList.AddPoint(ptLeftBottom.GetX(),ptLeftBottom.GetY());
- pNewLineBottom->CalculateBoundary();
- pMap->AddObject(pNewLineBottom);
- pNewLineLeft = new CQLineObj;
- pNewLineLeft->m_PtList.AddPoint(ptLeftBottom.GetX(),ptLeftBottom.GetY());
- pNewLineLeft->m_PtList.AddPoint(ptLeftTop.GetX(),ptLeftTop.GetY());
- pNewLineLeft->CalculateBoundary();
- pMap->AddObject(pNewLineLeft);
- }
- //计算首列元素的起点
- CQPoint CQReadExcelToView::CalEachRowFirstColPt(float fRowHeight)
- {
- CQPoint ptFirst;
- if(m_stEachFirRow.st_lMergeRows == 0)
- m_stEachFirRow.st_lMergeRows = 1;
- ptFirst.SetX((m_stEachFirRow.st_ptBeginFirst).GetX());
- ptFirst.SetY((m_stEachFirRow.st_ptBeginFirst).GetY() - m_pCoordObj->MMtoWL(fRowHeight*m_stEachFirRow.st_lMergeRows));
- m_stEachFirRow.st_ptBeginFirst = ptFirst;
- return ptFirst;
- }
- //重新计算合并单元格的右下角点坐标值
- void CQReadExcelToView::ReCalMergedCellEnPt(CQPoint & ptEnd,long nRowIndex,long nColIndex,float fRowHeight,float fColWidth)
- {
- int nVtSize = m_stVector.size();
- if(nVtSize == 0)return;
- int ii = 0;
- int jj = 0;
- //找到被合并的单元格
- for(ii=0;ii<nVtSize;ii++)
- {
- if(m_stVector[ii].st_RowNum == nRowIndex &&
- m_stVector[ii].st_ColNum == nColIndex && m_stVector[ii].st_bMerge != 0)
- {
- break;
- }
- }
- if(ii>=nVtSize)return;
- //向上倒推找开始合并处的那个单元格
- if(m_stVector[ii].st_bMerge != 0) // 假如这个单元格刚好是被合并的单元格
- {
- for(jj=ii;jj>=0;jj--)
- {
- if(m_stVector[jj].st_bMerge == 0 && m_stVector[jj].st_ColNum == nColIndex)
- break;
- }
- if(jj<0)return;
- //float st_ColWidth; //记录列宽
- //float st_RowHeight; //记录行高
- //st_ptEnd
- //假如初始计算时的行高比现在得到的行高要小就加
- CQPoint ptt;
- if(m_stVector[jj].st_RowHeight<fRowHeight)
- {
- ptt = m_stVector[jj].st_ptEnd;
- float ff = m_stVector[jj].st_RowHeight;
- double dy = ptt.GetY();
- dy -= m_pCoordObj->MMtoWL(fRowHeight - ff);
- ptt.SetY(dy);
- m_stVector[jj].st_ptEnd = ptt;
- }
- else if(m_stVector[jj].st_RowHeight>fRowHeight)
- {
- ptt = m_stVector[jj].st_ptEnd;
- double dy = ptt.GetY();
- dy += m_pCoordObj->MMtoWL(m_stVector[jj].st_RowHeight-fRowHeight);
- ptt.SetY(dy);
- m_stVector[jj].st_ptEnd = ptt;
- }
- if(m_stVector[jj].st_ColWidth<fColWidth)
- {
- ptt = m_stVector[jj].st_ptEnd;
- double dx = ptt.GetX();
- dx += m_pCoordObj->MMtoWL(fColWidth - m_stVector[jj].st_ColWidth);
- ptt.SetX(dx);
- m_stVector[jj].st_ptEnd = ptt;
- }
- else if(m_stVector[jj].st_ColWidth>fColWidth)
- {
- ptt = m_stVector[jj].st_ptEnd;
- double dx = ptt.GetX();
- dx -= m_pCoordObj->MMtoWL(m_stVector[jj].st_ColWidth-fColWidth);
- ptt.SetX(dx);
- m_stVector[jj].st_ptEnd = ptt;
- }
- }
- }
- //设置表格的起点坐标
- void CQReadExcelToView::SetExcelStartPt(CQPoint Pt)
- {
- m_ptBegin.SetX(Pt.GetX());
- m_ptBegin.SetY(Pt.GetY());
- }
- //设置表格的起点坐标
- void CQReadExcelToView::SetExcelStartPt(double dx,double dy)
- {
- m_ptBegin.SetX(dx);
- m_ptBegin.SetY(dy);
- }