• <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 閱讀(2903) 評論(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  回復  更多評論
              
            久久亚洲精品人成综合网| 狠狠色丁香婷综合久久| 亚洲国产成人精品91久久久 | 久久青青草原精品国产不卡| 久久99久久无码毛片一区二区| 亚洲&#228;v永久无码精品天堂久久 | 久久美女人爽女人爽| 岛国搬运www久久| 国产亚洲精品久久久久秋霞| 97久久精品无码一区二区天美| 久久精品国产福利国产琪琪| 99蜜桃臀久久久欧美精品网站 | 国产精品99久久99久久久| 99久久精品费精品国产| 97精品依人久久久大香线蕉97| 亚洲欧美精品伊人久久| 国产成人精品三上悠亚久久| 伊人久久综合成人网| 久久91精品国产91久久户| 久久久精品人妻一区二区三区蜜桃| 久久不见久久见免费视频7| 色99久久久久高潮综合影院 | 国产日产久久高清欧美一区| 四虎久久影院| 久久无码国产| 久久99精品久久久久久噜噜| 久久国产热精品波多野结衣AV| 国产一区二区久久久| 国产91久久综合| 97久久精品人人做人人爽| 久久一日本道色综合久久| 久久天天躁狠狠躁夜夜2020一| 欧美激情精品久久久久久久| 精品熟女少妇aⅴ免费久久| 久久久久一区二区三区| 久久99久久99精品免视看动漫| 色综合久久中文字幕无码| 亚洲va久久久噜噜噜久久天堂| 亚洲伊人久久精品影院| 无码人妻久久久一区二区三区 | 亚洲人AV永久一区二区三区久久|