Use odbc to read and write excel files: (1) header files

Source: Internet
Author: User

1 # ifndef _ ODBCEXCEL_H
2 # define _ ODBCEXCEL_H
3
4 /************************************** **************************************** *******************
5 @ brief odbc c ++ class for reading and writing Excel
6 * Author qinqing
7 * Date 2009-10-31
8 * this type depends on MFC. The data in the same column in excel must be of the same type and the cell format is set to regular. Otherwise, the data cannot be read.
9 if you enter a pure value, add the 'symbol before it, that is, when the artificial string type is processed
10 *************************************** **************************************** *******************/
11
12 # include <odbcinst. h>
13 # include <afxdb. h>
14 # include <vector>
15 # include <map>
16
17 class CODBCExcelSheet;
18 class CODBCExcelCell;
19
20 // Excel File
21 class CODBCExcel
22 {
23 friend class CODBCExcelSheet;
24 public:
25 CODBCExcel ();
26 ~ CODBCExcel ();
27
28 bool Open (const CString & strFileName );
29 bool Save ();
30 void Close ();
31
32 CODBCExcelSheet * GetWorkSheet (const CString & strSheetName );
33 CODBCExcelSheet * AddWorkSheet (const CString & strSheetName, const CStringArray & ColHeaders );
34 void DeleteWorkSheet (const CString & strSheetName );
35
36 protected:
37 static CString GetExcelDriver ();
38
39 private:
40 std: map <CString, CODBCExcelSheet *> m_Sheets;
41 std: vector <CString> m_DeleteSheets;
42 CDatabase m_db;
43 };
44
45 // Worksheet
46 class CODBCExcelSheet
47 {
48 friend class CODBCExcel;
49 friend class CODBCExcelCell;
50 enum
51 {
52 Exist,
53 Update,
54 Add,
55 Delete
56 };
57
58 public:
59 CODBCExcelSheet (CODBCExcel & Excel, const CString & strName );
60 CODBCExcelCell * Cell (UINT rowIndex, UINT colIndex );
61 CODBCExcelCell * Cell (UINT rowIndex, const CString & strColName );
62
63 const CStringArray & GetColHeader () const {return m_ColHeaders ;}
64 int GetColHeader (const CString & strColName) const;
65 int GetRow (const CString & strColName, const CString & strCellText );
66 DWORD GetTotalRow () const {return m_dwRows ;}
67 DWORD GetTotalCol () const {return m_dwCols ;}
68 const CString & GetName () const {return m_strName ;}
69
70 protected:
71 bool Init ();
72 void UpdateRowCount ();
73 bool UpdateCells ();
74 void ResetCells ();
75 bool Save ();
76
77 private:
78 CRecordset m_recordset;
79 CStringArray m_ColHeaders;
80
81 private:
82 DWORD m_dwRows;
83 DWORD m_dwCols;
84 CString m_strName;
85 int m_nFlag;
86
87 private:
88 CODBCExcel & m_Excel;
89 std: vector <CODBCExcelCell> m_Cells;
90 };
91
92 // the content of a cell is only in plain text format, and the binary and time and date types are not considered.
93 class CODBCExcelCell
94 {
95 friend class CODBCExcelSheet;
96 public:
97 CODBCExcelCell ();
98
99 void Set (short sVal );
100 void Set (long lVal );
101 void Set (float fVal );
102 void Set (double dVal );
103 void Set (const CString & strVal );
104
105 CString & GetText () {return m_strVal ;}
106 short GetShort () const {return (short) _ ttoi (m_strVal );}
107 long GetLong () const {return _ ttol (m_strVal );}
108 float GetFloat () const {return _ tstof (m_strVal );}
109 double GetDouble () const {return (double) _ tstof (m_strVal );}
110
111 void Empty () {m_strVal.Empty ();}
112
113 protected:
114 void Set (const CDBVariant & dbVal );
115 void SetParent (CODBCExcelSheet * Sheet) {m_Sheet = Sheet ;}
116 CODBCExcelSheet * m_Sheet;
117
118 private:
119 CString m_strVal; // show value as string type
120 };
121
122 # endif

Author: Tiandao rewards"
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.