如何将数据从列表控件写入MFC中的CSV文件 [英] How to write the data from the list control to a CSV file in MFC

查看:368
本文介绍了如何将数据从列表控件写入MFC中的CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在构建MFC应用程序,我想将列表控件中的数据保存到CSV文件中,任何人都可以给我同样的示例代码.

由于这个原因,我在项目之间介入了.如果有人对此给予帮助,我将非常感激.

Hi

I am building an MFC application, i want to save the data from the list control to a CSV file,Can any one give me a sample code for same.

Iam struck in between the project due to this i will be thankful if some one help me for this.

推荐答案

第一答案
您可以使用本文中的信息:
在MFC中读取和写入CSV文件 [ ^ ]

此链接:
http://stackoverflow.com/questions/1120140/csv-parser-in-c/ [ ^ ]

第二答案
看一下这个问题:以附加模式创建文件 [ ^ ]
看一下解决方案1.

可能会解决您的问题.

希望这对您有帮助...
FIRST ANSWER
You can use the information in this article:
Reading and Writing CSV Files in MFC[^]

This link:
http://stackoverflow.com/questions/1120140/csv-parser-in-c/[^]

SECOND ANSWER
Take a look at this question: create Files in appending mode[^]
Take a look at the solution 1.

Probably it will fix your issue.

Hope this helps...


只需遍历列表控件的行和列即可:
Just loop through the rows and columns of the list control:
// Get column label text.
void CMyListCtrl::GetHeaderItemText(CString& str, int nItem) const
{
    TCHAR lpBuffer[256] = _T("");
    LVCOLUMN lvc;
    memset(&lvc, 0, sizeof(LVCOLUMN));
    lvc.mask = LVCF_TEXT;
    lvc.pszText = lpBuffer;
    lvc.cchTextMax = 256;
    VERIFY(GetColumn(nItem, &lvc));	// asserts if string too long
    str = lpBuffer;
}

// Write report style list control content to CSV file.
bool CMyListCtrl::WriteCSV(LPCTSTR lpszFile, bool bHeader) const
{
    FILE *f = _tfopen(lpszFile, _T("wb"));
    if (NULL == f)
        return false;
    int nRows = GetItemCount();
    int nColumns = GetHeaderCtrl()->GetItemCount();
    if (bHeader)
    {
        for (int j = 0; j < nColumns; j++)
        {
            int k = GetHeaderCtrl()->OrderToIndex(j);
            if (j)
                fprintf(f, ",");
            CString strItem;
            GetHeaderItemText(strItem, k);
#ifdef _UNICODE
            CStringA strItemA(strItem.GetString());
            fprintf(f, "\"%s\"", strItemA.GetString());
#else
            fprintf(f, "\"%s\"", strItem.GetString());
#endif
        }
        fprintf(f, "\r\n");
    }
    for (int i = 0; i < nRows; i++)
    {
        for (int j = 0; j < nColumns; j++)
        {
            int k = GetHeaderCtrl()->OrderToIndex(j);
            if (j)
                fprintf(f, ",");
            CString strItem = GetItemText(i, k);
#ifdef _UNICODE
            CStringA strItemA(strItem.GetString());
            fprintf(f, "\"%s\"", strItemA.GetString());
#else
            fprintf(f, "\"%s\"", strItem.GetString());
#endif
        }
        fprintf(f, "\r\n");
    }
    fclose(f);
    return true;
}


这将导出带有Unicode构建的ANSI文本.多数应用程序在导入CSV时都需要ANSI文件.

该代码可能会得到增强:


  • 更新:在单元格中出现引号字符时,必须对其进行转义.
  • 可选使用单引号.
  • 可选使用其他定界符(例如'';'').
  • li>
  • 仅引用包含引号或定界符的单元格.
  • 可选地排除隐藏的列(宽度为0的列).
  • 可选地仅导出选定的行.
  • li>


    This exports ANSI text with Unicode builds. Most applications expect ANSI files when importing CSV.

    The code may be enhanced:


    • UPDATE: Must escape quote chars when they occur in cells,
    • Optional use single quotes.
    • Optional use other delimiter character (e.g. '';'').
    • Quote only cells that contain the quote or the delimiter character.
    • Optional exclude hidden columns (columns with width 0).
    • Optional export only selected rows.

    • 这篇关于如何将数据从列表控件写入MFC中的CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆