• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 29,comments - 10,trackbacks - 0
               首先,需要創建兩個類:CCsvFile(派生自CStdioFile,用于從屋里文件中讀取數據)和CCsvRecord(派生自CObject,適合作為CCsvFile類的Object映射成員,包含指定行的數據。
            1)CCsvRecord類的創建
               由于每個CSV記錄都包含很多列,所以增加一個名為m_arrCloumns的CStringArray類型的成員變量(該變量包含記錄的真正數據):
            protected:
                CStringArray m_arrColumns;
               接下來,編寫構造函數,獲得包含以逗號分隔記錄的字符串數值。使用C語言的strtok函數來遍歷字符串尋找逗號,每當找到一個逗號的時候,變量的token就包含逗號前面的值。然后,將這個值添加到m_arrColumns數組中。
            char DELIMITERS[] = ",";

            CCsvRecord::CCsvRecord(LPTSTR lpszRecord)
            {
                
            char *token;
                token 
            = strtok(lpszRecord, DELIMITERS);

                
            while(NULL != token)
                {
                    m_arrColumns.Add(token);
                    token 
            = strtok(NULL, DELIMITERS);    
                }
            }
               最后,添加返回m_arrCloumns數據的大小和檢索與一列相關的數據
            UINT CCsvRecord::GetNbrOfColumns()
            {
                
            return m_arrColumns.GetSize();
            }

            CString CCsvRecord::GetColumn(UINT uiColumn)
            {
                CString strColumn;    
                
            if (((uiColumn >= 0)    && (uiColumn <= (GetNbrOfColumns() - 1))))
                {
                    strColumn 
            = m_arrColumns[uiColumn];
                }    
                
            return strColumn;
            }
            2)CCsvFile類的創建
               首先,定義CCsvFile對象的集合,使用模板化的集合來保證類型的安全,并定義數組m_CsvRecordArray。EnsOfFile值是一個枚舉型的值,用來判斷是否到達文件尾。FileExists判斷文件是否存在。m_dwNumberOfRecords返回數組的大小,即表示文件中包含的記錄數。
            typedef CTypedPtrArray<CObArray, CCsvRecord*> CCsvRecordArray;

            class CCsvFile : public CStdioFile
            {
            public:
                CCsvFile(LPSTR lpszFileName);
                CCsvFile(CString
            & strFileName);
                
            ~CCsvFile();
            protected:
                
            void Initialize(LPSTR lpszFileName);
            public:
                
            enum
                {
                    EndOfFile 
            = -1
                };
                
            public:
                
            static BOOL FileExists(LPCTSTR lpszFileName) 
                {
                    
            return (0 == (_access(lpszFileName, 4)));
                }
                
            protected:
                DWORD LoadRecords();
                
            protected:
                DWORD m_dwNumberOfRecords;
            public:
                DWORD GetNumberOfRecords()
                {
                    
            return m_dwNumberOfRecords;
                }
            protected:
                CCsvRecordArray m_CsvRecordArray;
            public:
                UINT GetStartPosition();
                
            void GetNextAssoc(UINT& rPosition, CCsvRecord** ppCsvRecord);
            };
               利用Initialize對兩個構造函數進行初始化,讀取物理文件并得出包含的記錄數
            CCsvFile::CCsvFile(LPSTR lpszFileName)
            {
                Initialize(lpszFileName);
            }

            CCsvFile::CCsvFile(CString
            & strFileName)
            {
                Initialize(strFileName.GetBuffer(
            0));
            }

            void CCsvFile::Initialize(LPSTR lpszFileName)
            {
                m_dwNumberOfRecords 
            = 0;
                
                ASSERT(lpszFileName 
            != NULL);
                ASSERT(AfxIsValidString(lpszFileName));
                
                
            if (CCsvFile::FileExists(lpszFileName))
                {
                    
            if (Open(lpszFileName, CFile::modeRead))
                    {
                        m_dwNumberOfRecords 
            = LoadRecords();
                    }
                }    
            }

            DWORD CCsvFile::LoadRecords()
            {
                m_dwNumberOfRecords 
            = 0;
                
                CCsvRecord
            * pCsvRecord;
                CString strRecord;
                
            while (ReadString(strRecord))
                {
                    pCsvRecord 
            = new CCsvRecord(strRecord.GetBuffer(0));
                    
                    ASSERT(pCsvRecord);
                    
            if (pCsvRecord)
                    {
                        m_CsvRecordArray.Add(pCsvRecord);
                    }
                    strRecord.ReleaseBuffer();
                }
                
                
            return m_CsvRecordArray.GetSize();
            }
               增加兩個遍歷記錄數組的函數,GetStrarPosition和GetNextAssoc函數
            UINT CCsvFile::GetStartPosition()
            {
                UINT uiPosition;
                
                
            if (0 < m_CsvRecordArray.GetSize())
                {
                    uiPosition 
            = 0;
                }
                
            else
                {
                    uiPosition 
            = CCsvFile::EndOfFile;
                }
                
                
            return uiPosition;
            }

            void CCsvFile::GetNextAssoc(UINT& uiPosition, CCsvRecord** ppCsvRecord)
            {
                UINT uiNewPosition 
            = CCsvFile::EndOfFile;
                
            *ppCsvRecord = NULL;
                
                UINT nRecords 
            = m_CsvRecordArray.GetSize();
                
            if (uiPosition >= 0 && uiPosition <= (nRecords - 1))
                {
                    
            *ppCsvRecord = m_CsvRecordArray[uiPosition];
                    
            if ( (uiPosition + 1<= (nRecords - 1) )
                    {
                        uiNewPosition 
            = uiPosition + 1;
                    }
                }
                
                uiPosition 
            = uiNewPosition;
            }
               最后,編寫西溝函數來清楚前面分配的CCsvRecord對象:
            CCsvFile::~CCsvFile()
            {
                CCsvRecord
            * pCsvRecord;
                
            for (int i = m_CsvRecordArray.GetUpperBound(); i > -1; i--)
                {
                    pCsvRecord 
            = m_CsvRecordArray[i];
                    m_CsvRecordArray.RemoveAt(i);
                    
                    ASSERT(pCsvRecord);
                    
            if (pCsvRecord)
                    {
                        delete pCsvRecord;
                    }
                }
                VERIFY(
            0 == m_CsvRecordArray.GetSize());
            }
               3)打印和顯示CSV文件

               單擊Open事件,在實例化CCsvFile之后,只需在for循環中使用GetStratPosition和GetNextAssoc函數來檢索CCsvRecord對象。并對每條記錄調用InsertDataIntoListView函數。
            void CCViewCSVDlg::OnButton1() 
            {
                CFileDialog dlg(
            true);
                
            if (IDOK == dlg.DoModal())
                {
                    
            try
                    {
                        m_strFileName 
            = dlg.GetPathName();
                        UpdateData(FALSE);
                        
                        CCsvFile file(dlg.GetPathName());
                        
                        CCsvRecord
            * pCsvRecord;
                        
            for (UINT uiRow = file.GetStartPosition();
                        CCsvFile::EndOfFile 
            != uiRow;)
                        {
                            
            // get the actual record
                            file.GetNextAssoc(uiRow, &pCsvRecord);
                            
            if (pCsvRecord)
                            {
                                InsertDataIntoListView(uiRow, 
            *pCsvRecord);
                            }
                        }
                    }
                    
            catch(CFileException* pe)
                    {
                        pe
            ->ReportError();
                    }
                }
                SizeAllColumns();
            }

            void CCViewCSVDlg::InsertDataIntoListView(UINT uiRow, CCsvRecord& csvRecord)
            {
                
            // For each column in the passed record
                for (int iCol = 0; iCol < csvRecord.GetNbrOfColumns(); iCol++)
                {
                    
            // If this is the first row in the listview    
                    if (uiRow == 1)
                    {
                        
            // Create the listview columns.
                        CString strColumnName;
                        strColumnName.Format(
            "Col %ld", iCol);
                        m_lstFileContents.InsertColumn(iCol, strColumnName);
                    }
                    
                    
            if (iCol == 0)
                        m_lstFileContents.InsertItem(m_lstFileContents.GetItemCount(), 
                        csvRecord.GetColumn(iCol));
                    
            else
                        m_lstFileContents.SetItemText(m_lstFileContents.GetItemCount()
            -1,
                        iCol,csvRecord.GetColumn(iCol));
                }
            }
               最后,設置好列表框的每列的寬度
            void CCViewCSVDlg::SizeAllColumns()
            {
                CHeaderCtrl
            * pHeader = m_lstFileContents.GetHeaderCtrl();
                ASSERT(pHeader);
                
            if (pHeader)
                {
                    
            // Turn off redraw until the columns have all
                    
            // been resized
                    m_lstFileContents.SetRedraw(FALSE);
                    
                    
            for (int iCurrCol = 0
                    iCurrCol 
            < pHeader->GetItemCount(); 
                    iCurrCol
            ++)
                    {
                        m_lstFileContents.SetColumnWidth(iCurrCol, LVSCW_AUTOSIZE);
                        
                        
            int nCurrWidth = m_lstFileContents.GetColumnWidth(iCurrCol);
                        
                        m_lstFileContents.SetColumnWidth(iCurrCol,
                            LVSCW_AUTOSIZE_USEHEADER);
                        
                        
            int nColHdrWidth = m_lstFileContents.GetColumnWidth(iCurrCol);
                        
                        m_lstFileContents.SetColumnWidth(iCurrCol, 
                            max(nCurrWidth, nColHdrWidth));
                    }
                    
                    
            // Now that sizing is finished, turn redraw back on and 
                    
            // invalidate so that the control is repainted
                    m_lstFileContents.SetRedraw(TRUE);
                    m_lstFileContents.Invalidate();
                }
            }
             4)相關函數:
            token = strtok(lpszRecord, DELIMITERS);

            typedef CTypedPtrArray<CObArray, CCsvRecord*> CCsvRecordArray;

            return (0 == (_access(lpszFileName, 4)));

            ASSERT(AfxIsValidString(lpszFileName));

            for (int i = m_CsvRecordArray.GetUpperBound(); i > -1; i--)
            m_CsvRecordArray.RemoveAt(i);
            VERIFY(0 == m_CsvRecordArray.GetSize());

            pCsvRecord = new CCsvRecord(strRecord.GetBuffer(0));
            strRecord.ReleaseBuffer();

            m_lstFileContents.InsertColumn(iCol, strColumnName);
            m_lstFileContents.InsertItem(m_lstFileContents.GetItemCount(),csvRecord.GetColumn(iCol));
            m_lstFileContents.SetItemText(m_lstFileContents.GetItemCount()-1,iCol,csvRecord.GetColumn(iCol));

            m_lstFileContents.SetRedraw(FALSE);
            m_lstFileContents.SetColumnWidth(iCurrCol, LVSCW_AUTOSIZE);
            int nCurrWidth = m_lstFileContents.GetColumnWidth(iCurrCol);
            m_lstFileContents.SetRedraw(TRUE);
            m_lstFileContents.Invalidate();
            posted on 2009-07-29 23:06 The_Moment 閱讀(2913) 評論(2)  編輯 收藏 引用 所屬分類: VC實踐

            FeedBack:
            # re: CSV逗號分隔值文件(Comma Separated value)
            2010-05-09 21:44 | dqf
            非常不錯。
            可以發給我一份源碼嗎?
            我的email:dqf88@sohu.com  回復  更多評論
              
            # re: CSV逗號分隔值文件(Comma Separated value)
            2010-10-27 10:49 | mixed_now
            非常好。
            可以發給我一份源碼嗎?
            我的email:mixed_now@163.com  回復  更多評論
              
            无码任你躁久久久久久久| 色综合久久中文字幕综合网| 亚洲日本久久久午夜精品| 久久伊人五月天论坛| 久久夜色精品国产亚洲| 狠狠色综合网站久久久久久久高清| 久久精品国产精品亚洲精品| 国产亚州精品女人久久久久久 | 亚洲日本久久久午夜精品| 亚洲AV无码成人网站久久精品大| 99久久精品免费国产大片| 99久久精品免费看国产一区二区三区| 久久丫忘忧草产品| 99国内精品久久久久久久| 狠狠色综合网站久久久久久久高清| 亚洲综合久久综合激情久久| 久久乐国产综合亚洲精品| 一级做a爰片久久毛片人呢| 久久久亚洲裙底偷窥综合 | 一本色道久久88—综合亚洲精品| www.久久精品| 久久久无码一区二区三区| 亚洲精品国精品久久99热| 精品久久久久久国产免费了| 日本强好片久久久久久AAA| 国内精品综合久久久40p| 国产精品欧美亚洲韩国日本久久 | 久久99精品久久久大学生| 久久AⅤ人妻少妇嫩草影院| www.久久精品| 国产精品99久久免费观看| 97久久久精品综合88久久| 亚洲香蕉网久久综合影视| 久久婷婷午色综合夜啪| 亚洲国产成人精品91久久久| 国产精品99久久久久久www| 久久久综合九色合综国产| 青青热久久综合网伊人| 97久久精品无码一区二区天美| 久久久久亚洲AV无码网站| www.久久热.com|