Excel读写类release

本文介绍了一个用于操作Excel表格的自定义类的设计与实现,包括如何添加和读取数据,提供了详细的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    为了方便操作Excel表格, 写了这个Excel读写类。
    部分头文件:

 

// ExcelSheet.h: interface for the CExcelSheet class.
// Excel 读写类 Copyright 2007
// programmed by shenhai, email: heatonshen@163.com
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_EXCELSHEET_H__84984803_14F7_4F6E_AB65_A429CA01D4E5__INCLUDED_)
#define AFX_EXCELSHEET_H__84984803_14F7_4F6E_AB65_A429CA01D4E5__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include 
<odbcinst.h>
#include 
<afxdb.h>
#include 
<afxcoll.h>

#ifndef EXCELSHEET_API
#define EXCELSHEET_API __declspec(dllexport)
#endif

class EXCELSHEET_API CExcelSheet  
{
public:
    
//构造函数
    CExcelSheet();
    
virtual ~CExcelSheet();

  
bool OpenExcel(CString File, CString SheetOrSeparator, bool Backup = false);
  inline 
bool CloseExcel()return true;}
    
    
//删除excel中的所有表格
    bool DeleteSheet();
    
//删除excel中指定的表格
    bool DeleteSheet(CString SheetName);
 
    
bool RollBack();

    
//取总行数
    inline long GetTotalRows() {return m_dTotalRows;}
    
//取总列数
    inline short GetTotalColumns() {return m_dTotalColumns;}
    
//取当前所在行数
    inline long GetCurrentRow() {return m_dCurrentRow;}


    
//初始化excel缓存
    void InitBuffer();
    
//添加数据方式一
    int AddStr(LPCTSTR newElement);
    
int AddStr(const CString& newElement);
    
int AddChar(char newElement);
    
int AddDouble(double newElement);
    
int AddLong(long newElement);
    
//添加一行, 缺省尾部添加
    bool AppendRecord(long row = 0);
    
//替换目的行
    bool ReplaceRecord(long row);
    
//添加excel表头
    bool AppendHeaders();
    
//替换excel表头
    bool ReplaceHeaders();
    
    
//添加数据方式二 , 先要InitBuffer(), add完后再AppendRecord或者ReplaceRecord
    
//以char*添加
    int AddStr(char *fieldname, char *destValue);
    
//以CString添加
    int AddStr(char *fieldname, CString &destValue);
    
//以char添加
    int AddChar(char *fieldname, char destValue);
    
//以长整型添加
    int AddLong(char *fieldname, long destValue);
    
//以整型添加
    int AddInt(char *fieldname, int destValue);
    
//以double型添加
    int AddDouble(char *fieldname, double destValue);


   
// 返回值为字段的类型
   
// 读取一行excel数据,缺省读取当前下一行
   bool ReadRecord(long row = 0);
   
// 以char * 返回字段的值
   void GetStr(char *fieldname, char *destValue);
   
// 以CString返回字段的值
   void GetStr(char *fieldname, CString &destValue);
   
// 将字段内容以单字节返回
   void GetChar(char *fieldname, char &destValue);
   
// 以长整型返回
   long GetLong(char *fieldname);
   
// 以整型返回(在windows中和上边一样)
   int GetInt(char *fieldname);
   
// 以double返回字段的值
   double GetDouble(char *fieldname);

   
void BeginTransaction(); 
   inline CString GetLastError()

private:    
    
bool m_bAppend; 
    
bool m_bBackup; 
    
bool m_bExcel; 
    
bool m_bTransaction; 

    
long m_dCurrentRow; 
    
long m_dTotalRows; 
    
short m_dTotalColumns; 

    CString m_sSql; 
    CString m_sDsn; 
    CString m_stempSql; 
    CString m_stempString; 
    CString m_sSheetName; 
    CString m_sExcelDriver; 
    CString m_sFile; 
    CString m_sSeparator; 
    CString m_sLastError; 

    CStringArray m_ExcelRow;
                     CStringArray m_ExcelRowforOut;

    CStringArray m_atempArray; 
    CStringArray m_aFieldNames;
    CStringArray m_aRows; 

    CDatabase 
*m_Database;
    CRecordset 
*m_rSheet; 
}
;

#endif

使用示例:
一,添加数据:

 

    // 新建Excel文件名及路径,SheetTable为内部表名
    CExcelSheet ExcelSheet;
  ExcelSheet.OpenExcel(
"c:/ExcelSheet.xls""SheetTable");
    ExcelSheet.BeginTransaction();
    
    
// 加入标题
    
//添加数据前需要先InitBuffer(),创建输入缓存
    ExcelSheet.InitBuffer();
    ExcelSheet.AddStr(
"姓名");
    ExcelSheet.AddStr(
"年龄");
    ExcelSheet.AddStr(
"身高");
    ExcelSheet.AddStr(
"收入");
    ExcelSheet.AddStr(
"ab1");
    
//调用ReplaceHeaders()将输入缓存写入Excel
    ExcelSheet.ReplaceHeaders();
    
    
// 加入数据
    
//方法一,按照数据字段顺序添加
    ExcelSheet.InitBuffer();
        ExcelSheet.AddStr(
"张三");
        ExcelSheet.AddStr(
"16");
        ExcelSheet.AddLong(
12345);
        ExcelSheet.AddDouble(
12.345);
        ExcelSheet.AddChar(
's');
        ExcelSheet.AppendRecord();

        
//方法二,按照数据字段名称添加
    ExcelSheet.InitBuffer();
        ExcelSheet.AddLong(
"身高"12345);
        ExcelSheet.AddDouble(
"收入"12.345);
        ExcelSheet.AddChar(
"ab1"'s');
        ExcelSheet.AddStr(
"姓名""张三");
        ExcelSheet.AddStr(
"年龄""16");
        ExcelSheet.AppendRecord();

 

 

 二,读取数据

 

    CExcelSheet ExcelSheet;
  ExcelSheet.OpenExcel(
"c:/ExcelSheet.xls""SheetTable");
    
//将当前数据行读入输出buffer  
        ExcelSheet.ReadRecord(i);
        CString strContents 
= "";
        
char abc[1024];
        ExcelSheet.GetStr(
"姓名", abc);
        CString strTemp 
= "";
    
int age = ExcelSheet.GetLong("年龄"));
        
int tall = ExcelSheet.GetInt("身高"));
        
double income = ExcelSheet.GetDouble("收入"));
        
char aaa;
        ExcelSheet.GetChar(
"ab1", aaa);

       

       有需要的xdjms可以给我发email索取最新版本的release,当然
有臭虫或者有什么建议也要积极报告:)

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值