C++读写excel文件(二)—— 用CSpreadSheet读写(by yukin_xue)

CSpreadSheet类提供了一个方便的方式来读写Excel文件,通过ODBC接口实现。该类可以添加、删除行和列,以及读取数据。源代码中包含了用于读取、写入和管理Excel文件的方法,并提供了错误处理和事务管理功能。
摘要由CSDN通过智能技术生成

     CSpreadSheet类封装了对excel文件的操作,这是网上一个高手写的,其实本质还是通过odbc存取,提供了更多的接口。使用了下很好用,但还是那个问题,我仅用来读excel文件时操作不成功,但是以后有写、读的需求时,用这个很好,功能强大,代码留着了,也试着调通了。

代码摘抄自:http://www.newxing.com/S76C7EF1F1141/CSpreadSheet.h

.h文件:

//#pragma once

// Class to read and write to Excel and text delimited spreadsheet  
//  
// Created by Yap Chun Wei  
// December 2001  
//   
// Version 1.1  
// Updates: Fix bug in ReadRow() which prevent reading of single column spreadsheet  
// Modified by jingzhou xu  
#ifndef CSPREADSHEET_H  
#define CSPREADSHEET_H  
#include <odbcinst.h>  
#include <afxdb.h>  

class CSpreadSheet  
{  
public:  
    CSpreadSheet(CString File, CString SheetOrSeparator, bool Backup = true); // Open spreadsheet for reading and writing  
    ~CSpreadSheet(); // Perform some cleanup functions  
    bool AddHeaders(CStringArray &FieldNames, bool replace = false); // Add header row to spreadsheet  
    bool DeleteSheet(); // Clear text delimited file content  
    bool DeleteSheet(CString SheetName); // Clear entire Excel spreadsheet content. The sheet itself is not deleted  
    bool AddRow(CStringArray &RowValues, long row = 0, bool replace = false); // Insert or replace a row into spreadsheet. Default is add new row.   
    bool AddCell(CString CellValue, CString column, long row = 0, bool Auto = true); // Replace or add a cell into Excel spreadsheet using header row or column alphabet. Default is add cell into new row. Set Auto to false if want to force column to be used as header name  
    bool AddCell(CString CellValue, short column, long row = 0); // Replace or add a cell into spreadsheet using column number. Default is add cell into new row.   
    bool ReplaceRows(CStringArray &NewRowValues, CStringArray &OldRowValues); // Search and replace rows in Excel spreadsheet  
    bool ReadRow(CStringArray &RowValues, long row = 0); // Read a row from spreadsheet. Default is read the next row  
    bool ReadColumn(CStringArray &ColumnValues, CString column, bool Auto = true); // Read a column from Excel spreadsheet using header row or column alphabet. Set Auto to false if want to force column to be used as header name  
    bool ReadColumn(CStringArray &ColumnValues, short column); // Read a column from spreadsheet using column number  
    bool ReadCell (CString &CellValue, CString column, long row = 0, bool Auto = true); // Read a cell from Excel spreadsheet using header row or column alphabet. Default is read the next cell in next row. Set Auto to false if want to force column to be used as header name  
    bool ReadCell (CString &CellValue, short column, long row = 0); // Read a cell from spreadsheet using column number. Default is read the next cell in next row.  
    void BeginTransaction(); // Begin transaction  
    bool Commit(); // Save changes to spreadsheet  
    bool RollBack(); // Undo changes to spreadsheet  
    bool Convert(CString SheetOrSeparator);  
    inline void GetFieldNames (CStringArray &FieldNames) {FieldNames.RemoveAll(); FieldNames.Copy(m_aFieldNames);} // Get the header row from spreadsheet  
    inline long GetTotalRows() {return m_dTotalRows;} // Get total number of rows in  spreadsheet  
    inline short GetTotalColumns() {return m_dTotalColumns;} // Get total number of columns in  spreadsheet  
    inline long GetCurrentRow() {return m_dCurrentRow;} // Get the currently selected row in  spreadsheet  
    inline bool GetBackupStatus() {return m_bBackup;} // Get status of backup. True if backup is successful, False if spreadsheet is not backup  
    inline bool GetTransactionStatus() {return m_bTransaction;} // Get status of Transaction. True if Transaction is started, False if Transaction is not started or has error in starting  
    inline CString GetLastError() {return m_sLastError;} // Get last error message  

private:  
    bool Open(); // Open a text delimited file for reading or writing  
    void GetExcelDriver(); // Get the name of the Excel-ODBC driver  
    short CalculateColumnNumber(CString column, bool Auto); // Convert Excel column in alphabet into column number  

    bool m_bAppend; // Internal flag to denote newly created spreadsheet or previously created spreadsheet  
    bool m_bBackup; // Internal flag to denote status of Backup  
    bool m_bExcel; // Internal flag to denote whether file is Excel spreadsheet or text delimited spreadsheet  
    bool m_bTransaction; // Internal flag to denote status of Transaction  

    long m_dCurrentRow; // Index of current row, starting from 1  
    long m_dTotalRows; // Total number of rows in spreadsheet  
    short m_dTotalColumns; // Total number of columns in Excel spreadsheet. Largest number of columns in text delimited spreadsheet  

    CString m_sSql; // SQL statement to open Excel spreadsheet for reading  
    CString m_sDsn; // DSN string to open Excel spreadsheet for reading and writing  
    CString m_stempSql; // Temporary string for SQL statements or for use by functions  
    CString m_stempString; // Temporary string for use by functions  
    CString m_sSheetName; // Sheet name of Excel spreadsheet  
    CString m_sExcelDriver; // Name of Excel Driver  
    CString m_sFile; // Spreadsheet file name  
    CString m_sSeparator; // Separator in text delimited spreadsheet  
    CString m_sLastError; // Last error message  

    CStringArray m_atempArray; // Temporary array for use by functions  
    CStringArray m_aFieldNames; // Header row in spreadsheet  
    CStringArray m_aRows; // Content of all the rows in spreadsheet  

    CDatabase *m_Database; // Database variable for Excel spreadsheet  
    CRecordset *m_rSheet; // Recordset for Excel spreadsheet  
};  

#endif  



.cpp文件:


#include "StdAfx.h"
#include "SpreadSheet.h"


// Open spreadsheet for reading and writing  
CSpreadSheet::CSpreadSheet(CString File, CString SheetOrSeparator, bool Backup) :  
m_Database(NULL), m_rSheet(NULL), m_sFile(File),  
m_dTotalRows(0), m_dTotalColumns(0), m_dCurrentRow(1),  
m_bAppend(false), m_bBackup(Backup), m_bTransaction(false)  
{  
    // Detect whether file is an Excel spreadsheet or a text delimited file  
    m_stempString = m_sFile.Right(4);  
    m_stempString.MakeLower();  
    if (m_stempString == ".xls") // File is an Excel spreadsheet  
    {  
        m_bExcel = true;  
        m_sSheetName = SheetOrSeparator;  
        m_sSeparator = ",;.?";  
    }  
    else // File is a text delimited file  
    {  
        m_bExcel = false;  
        m_sSeparator = SheetOrSeparator;  
    }  

    if (m_bExcel) // If file is an Excel spreadsheet  
    {  
        m_Database = new CDatabase;  
        GetExcelDriver();  
        m_sDsn.Format("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s", m_sExcelDriver, m_sFile, m_sFile);  

        if (Open())  
        {  
            if (m_bBackup)  
            {  
                if ((m_bBackup) && (m_bAppend))  
                {  
                    CString tempSheetName = m_sSheetName;  
                    m_sSheetName = "CSpreadSheetBackup";  
                    m_bAppend = false;  
                    if (!Commit())  
                    {  
                        m_bBackup = false;  
                    }  
                    m_bAppend = true;  
                    m_sSheetName = tempSheetName;  
                    m_dCurrentRow = 1;  
                }  
            }  
        }  
    }  
    else // if file is a text delimited file  
    {  
        if (Open())  
        {  
            if ((m_bBackup) && (m_bAppend))  
            {  
                m_stempString = m_sFile;  
                m_stempSql.Format("%s.bak", m_sFile);  
                m_sFile = m_stempSql;  
                if (!Commit())  
                {  
                    m_bBackup = false;  
                }  
                m_sFile = m_stempString;  
            }  
        }  
    }  
}  

// Perform some cleanup functions  
CSpreadSheet::~CSpreadSheet()  
{  
    if (m_Database != NULL)  
    {  
        m_Database->Close();  
        delete m_Database;  
    }  
}  

// Add header row to spreadsheet  
bool CSpreadSheet::AddHeaders(CStringArray &FieldNames, bool replace)  
{  
    if (m_bAppend) // Append to old Sheet  
    {  
        if (replace) // Replacing header row rather than adding new columns  
        {  
            if (!AddRow(FieldNames, 1, true))  
            {  
                return false;  
            }  
            else  
            {  
                return true;  
            }  
        }  

        if (ReadRow(m_atempArray, 1)) // Add new columns  
        {  
            if (m_bExcel)  
            {  
                // Check for duplicate header row field  
                for (int i = 0; i < FieldNames.GetSize(); i++)  
                {  
                    for (int j = 0; j < m_atempArray.GetSize(); j++)  
                    {  
                        if (FieldNames.GetAt(i) == m_atempArray.GetAt(j))  
                        {  
                            m_sLastError.Format("Duplicate header row field:%s/n", FieldNames.GetAt(i));  
                            return false;  
                        }  
                    }  
                }     
            }  

            m_atempArray.Append(FieldNames);  
            if (!AddRow(m_atempArray, 1, true))  
            {  
                m_sLastError = "Problems with adding headers/n";  
                return false;  
            }  

            // Update largest number of columns if necessary  
            if (m_atempArray.GetSize() > m_dTotalColumns)  
            {  
                m_dTotalColumns = m_atempArray.GetSize();  
            }  
            return true;  
        }  
        return false;                 
    }  
    else // New Sheet  
    {  
        m_dTotalColumns = FieldNames.GetSize();  
        if (!AddRow(FieldNames, 1, true))  
        {  
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值