Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug]ExcelBase::convertToColName #10

Open
MonostableLSD opened this issue Feb 20, 2023 · 1 comment
Open

[bug]ExcelBase::convertToColName #10

MonostableLSD opened this issue Feb 20, 2023 · 1 comment

Comments

@MonostableLSD
Copy link

当data是26的整数倍时,mode= data % 26 == 0,导致转换出“@”字符,导致表格保存失败。

@MonostableLSD
Copy link
Author

MonostableLSD commented Feb 20, 2023

/// \brief 把列数转换为excel的字母列号
/// \param data 大于0的数
/// \return 字母列号,如1->A 26->Z 27 AA
///
void ExcelBase::convertToColName(int data, QString &res) //keep data 1-26
{
    Q_ASSERT(data>0 && data<65535);
    int a = (data-1) / 26 ;
    if(a > 0)
    {
        int b = (data-1) % 26 +1;
        convertToColName(b,res);
        convertToColName(a,res);
    }
    else
    {
        res=(to26AlphabetString(data)+res);
    }
}

微软的喂饭写法:

Function ConvertToLetter(iCol As Long) As String
   Dim a As Long
   Dim b As Long
   a = iCol
   ConvertToLetter = ""
   Do While iCol > 0
      a = Int((iCol - 1) / 26)
      b = (iCol - 1) Mod 26
      ConvertToLetter = Chr(b + 65) & ConvertToLetter
      iCol = a
   Loop
End Function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant