Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

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...
 
Share this answer
 
v2
Comments
Member 9274864 5-Sep-12 2:58am    
I have used this artical "Reading and Writing CSV Files in MFC[^]"
but iam linker error after using this.

error LNK2019: unresolved external symbol "class CCSVFile __cdecl csvFile

I have taken the instance of that class and called the WriteData() in my class.
This is how i have done

i want this write function to be done in the callback function.

i have taken the instance of the class with parameters and called the write function.
CCSVFile csvFile(LPCTSTR lpszFilename = "abcd", Mode mode = modeWrite);
csvFile(lpszFilename).WriteData(data);

i dont understand where have i done wrong plz help me
Joan M 5-Sep-12 3:29am    
Have you included the classes from the article?
You should copy both files .h and .cpp and then use them accordingly.
Member 9274864 5-Sep-12 5:40am    
yes i have included the .cpp and .h file
Member 9274864 5-Sep-12 22:36pm    
Now iam able to write in to the file but ,it is writing multiple times in the file ,For example if am having 10 items list control then if am saving those items in the list control,then 1st item is storing 10 times in first column and second one is storing 9 times in the second column it continues till other items ,i have atached my code to this plz check it

LPCTSTR lpszFilename = "XYZ.csv";
enum Mode { modeRead, modeWrite };
CCSVFile csvFile((LPCTSTR) lpszFilename );


int nlistCount = m_CtrlList.GetItemCount();

LPINT pItems = new int[nlistCount];

for(int index=0; index<=nlistCount; index++)
{
CString str1 = m_CtrlList.GetItemText(index,0);
CString DispName = str1 + "," + getDateTime;
CStringArray NameArry.Add(DispName);
csvFile.WriteData(NameArry);
}
Joan M 6-Sep-12 2:18am    
You must move the csvFile.WriteData call out of the loop... ;)
This is happening because in each loop iteration you are writting to the file and you are writting the contents of the array you are constructing inside the loop.
;)
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;
}

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.
 
Share this answer
 
v2
Comments
chienbkdt 21-Feb-13 9:21am    
Thank you,Solution 2 work perfectly with me. But is there the code of ReadCSV to read data from a csv file (same format to the file that written) and display on the List control!please kindly to help!
Jochen Arndt 21-Feb-13 11:37am    
I did not have written code to read CSV into a list control. But it's not too difficult. Just read the file line by line, extract the items, and add them to the list control.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900