查看完整版本: [-- v0.0.1中MSExcel类“单元格合并与设值”代码 --]

QTCN开发网 -> 天池项目 -> v0.0.1中MSExcel类“单元格合并与设值”代码 [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

lkldiy 2013-08-15 11:56

v0.0.1中MSExcel类“单元格合并与设值”代码


最近在做一个报表项目,其间涉及到单元格合并与写入数据的现实需求,故在天池MSExcel基础上做了一点儿扩展。
用的是V0.0.1 ,有用V0.0.2的朋友稍作修改即可使用(自己改吧,本人比较懒,嘿嘿~)

/// @brief 返回指定行、列的标识名称,如第10行第A列返回A10
QVariant MSExcel::CellName(const int iRow, const int iCol)
{
#if defined(Q_OS_WIN)
//Excel的格子定位有点儿特殊,先定列,再定行(一般都先行后列)
//Excel的列标题是字母组合,行标题是数字
//130列已经远远足够了
if(iRow < 0 || iCol < 0 || iCol >= 130)
  return QVariant() ;
char szCell[64];
char szCol[3];

if ( iCol < 26 ) {
  szCol[0] = 'A' + iCol;
  szCol[1] = 0;
}

if ( iCol >= 26 && iCol < 52 ) {
  szCol[0] = 'A';
  szCol[1] = 'A' + iCol - 26;
  szCol[2] = 0;
}

if ( iCol >= 52 && iCol < 78 ) {
  szCol[0] = 'B';
  szCol[1] = 'A' + iCol - 52;
  szCol[2] = 0;
}

if ( iCol >= 78 && iCol < 104 ) {
  szCol[0] = 'C';
  szCol[1] = 'A' + iCol - 78;
  szCol[2] = 0;
}

if ( iCol >= 104 && iCol < 130 ) {
  szCol[0] = 'D';
  szCol[1] = 'A' + iCol - 104;
  szCol[2] = 0;
}

//Excel的第一行行标为1,不是0
sprintf ( szCell, "%s%d", szCol, iRow + 1 );  //因为项目中的表格起始行为0,所以这里+1 ,用的朋友注意下

return QVariant(szCell);
#endif
return QVariant() ;
}

/// @brief 返回指定区域
QAxObject* MSExcel::GetRange(const int iBeginRow,const int iEndRow,
  const int iBeginCol,const int iEndCol)
{
#if defined(Q_OS_WIN)
//  if(iBeginRow > iEndRow || iBeginCol > iEndCol)
//   return NULL ;
if(NULL != m_sheet)
{
  QVariant vBegin = CellName(iBeginRow,iBeginCol) ;
  QVariant vEnd   = CellName(iEndRow,iEndCol) ;
  
  if(vBegin.isValid() && vEnd.isValid())
   return m_sheet->querySubObject("Range(const QVariant&,const QVariant&)",vBegin,vEnd) ;
  else if(vBegin.isValid())
   return m_sheet->querySubObject("Range(const QVariant&)",vBegin) ;
  else
   return NULL ;
}

#endif
return NULL ;
}

//@brief 合并单元格
void MSExcel::MergeCell(QAxObject *pRange,const bool bAcross /* = false */)
{
#if defined(Q_OS_WIN)
if(NULL != pRange)
{
  pRange->dynamicCall("Select") ;
  pRange->dynamicCall("Merge(const bool&)",bAcross) ;
}
#endif
}


//@brief 合并单元格,返回单元格区域
QAxObject* MSExcel::MergeCell(const int iBeginRow,const int iEndRow,
  const int iBeginCol,const int iEndCol)
{
#if defined(Q_OS_WIN)
QAxObject *pRange = GetRange(iBeginRow,iEndRow,iBeginCol,iEndCol) ;
if(NULL != pRange)
{
  pRange->dynamicCall("Select") ;
  pRange->dynamicCall("Merge(const bool&)",false) ;
  return pRange ;
}
#endif
return NULL ;
}

//@brief 设置单元格内容
void MSExcel::SetCellValue(QAxObject *pRange,const QVariant& val)
{
#if defined(Q_OS_WIN)
if(NULL != pRange)
  pRange->dynamicCall("SetValue2(const QVariant&)",val) ;
#endif
}

///////////////////////////////////////////////////////测试代码///////////////////////////////////
......
QString qsFileName("D:\\test.xls") ;

if(!m_pExcel->Create(qsFileName))
  return -1 ;
bool b = m_pExcel->UsedRange() ;
m_pExcel->SetVisible(false) ;
m_pExcel->SetCaption(m_strFileName) ;

  QAxObject *pRange = m_pExcel->MergeCell(2,3,3,4) ;
  if(NULL != pRange)
       m_pExcel->SetCellValue(pRange,"test2") ;
  pRange = m_pExcel->GetRange(10,10,10,10) ;
  if(NULL != pRange)
       m_pExcel->SetCellValue(pRange,"test1") ;
  m_pExcel->Save("") ;
  m_pExcel->Close() ;

return 0;

lkldiy 2013-08-15 12:04
漏了两个,赶紧补上

//@brief 设置单元格文本字体及字体颜色,fontColor有效时设置字体颜色,否则不设置字体颜色
void MSExcel::SetCellFont(QAxObject *pRange,const QFont& font,const QColor fontColor /* = QColor */)
{
#if defined(Q_OS_WIN)
    if(NULL != pRange)
    {
        QAxObject *pFont = pRange->querySubObject("Font") ;
        if(NULL == pFont)
            return ;

        pFont->dynamicCall("SetName(const QVariant&)",font.family()) ;
        pFont->dynamicCall("SetSize(const QVariant&)",font.pointSize()) ;

        if(font.bold())
            pFont->dynamicCall("SetBold(const QVariant&)",font.weight()) ;

        if(font.italic())
            pFont->dynamicCall("SetItalic(const QVariant&)",font.italic()) ;

        //下划线画不出来,为什么?
        if(font.underline())
            pFont->dynamicCall("SetUnderline(const QVariant&)",font.underline()) ;  

        if(font.strikeOut())
            pFont->dynamicCall("SetStrikethrough",font.strikeOut()) ;

        if(fontColor.isValid())
            pFont->dynamicCall("SetColor(const QVariant&)",fontColor) ;

        delete pFont ;
        pFont = NULL ;
    }
#endif
}

//@brief 设置单元格背景颜色
void MSExcel::SetCellBgColor(QAxObject *pRange,const QColor& clBg)
{
#if defined(Q_OS_WIN)
    if(NULL != pRange && clBg.isValid())
    {
        QAxObject *pInterior = pRange->querySubObject("Interior") ;
        if(NULL != pInterior)
        {
            pInterior->dynamicCall("SetColor(const QVariant&)",clBg) ;

            delete pInterior ;
            pInterior = NULL ;
        }
    }
#endif
}

XChinux 2013-08-15 15:20
然后可以合并到tianchi中去。

XChinux 2014-01-22 13:33
支持,顶!

内容来自[手机版]


查看完整版本: [-- v0.0.1中MSExcel类“单元格合并与设值”代码 --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled