-
Notifications
You must be signed in to change notification settings - Fork 4
/
CSpreadSheet.h
1462 lines (1347 loc) · 36.2 KB
/
CSpreadSheet.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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
#ifndef CSPREADSHEET_H
#define CSPREADSHEET_H
#include <odbcinst.h>
#include <afxdb.h>
// Fx add
LPCTSTR dataTypeStringArray[3]={"char","float","date"};
LPCTSTR dataTypePreValueStringArray[3]={"'","","DATE("};
LPCTSTR dataTypePostValueStringArray[3]={"'","",")"};
class CRowArray : public CStringArray
{
CUIntArray typeArray;
public:
enum DataType
{
charType=0,
floatType=1,
dateType=2,
};
public:
// CRowArray(CRowArray &aArray);
INT_PTR Add(LPCTSTR newElement,DataType type=charType);
INT_PTR Add(const CString& newElement,DataType type=charType);
INT_PTR Add(float newElement);
INT_PTR AddFromODBC(const CString& newElement, SWORD aSQLType);
int Append( const CRowArray& src );
void RemoveAll();
void Copy(const CRowArray& src );
CString GetFormatedValueString(int i, DataType type);
LPCTSTR GetFormatString(int i) { return dataTypeStringArray[GetDataType(i)]; }
DataType GetDataType(int i) { return (DataType)typeArray.GetAt(i); }
};
INT_PTR CRowArray::Add(LPCTSTR newElement, DataType type)
{
CStringArray::Add(newElement);
return typeArray.Add(type);
}
INT_PTR CRowArray::Add(const CString& newElement, DataType type)
{
CStringArray::Add(newElement);
return typeArray.Add(type);
}
INT_PTR CRowArray::AddFromODBC(const CString& newElement, SWORD aSQLType)
{
CStringArray::Add(newElement);
DataType type;
switch(aSQLType)
{
case 8:
type=CRowArray::floatType;
break;
default:
type=CRowArray::charType;
}
return typeArray.Add(type);
}
INT_PTR CRowArray::Add(float newElement)
{
CString str;
str.Format("%f",newElement);
CStringArray::Add(str);
return typeArray.Add(CRowArray::floatType);
}
int CRowArray::Append( const CRowArray& src )
{
typeArray.Append(src.typeArray);
return CStringArray::Append(src);
}
void CRowArray::RemoveAll()
{
CStringArray::RemoveAll();
typeArray.RemoveAll();
}
void CRowArray::Copy(const CRowArray& src )
{
CStringArray::Copy(src);
typeArray.Copy(src.typeArray);
}
CString CRowArray::GetFormatedValueString(int i, DataType type)
{
CString val=GetAt(i);
CString str=CString(dataTypePreValueStringArray[type])+GetAt(i)+CString(dataTypePostValueStringArray[type]);
return str;
}
// end of fx add
class CSpreadSheet
{
public:
CSpreadSheet(CString File, CString SheetOrSeparator, bool Backup = true); // Open spreadsheet for reading and writing
~CSpreadSheet(); // Perform some cleanup functions
bool AddHeaders(CRowArray &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(CRowArray &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(CRowArray &NewRowValues, CRowArray &OldRowValues); // Search and replace rows in Excel spreadsheet
bool ReadRow(CRowArray &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
CRowArray m_atempArray; // Temporary array for use by functions
CRowArray 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
};
// 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(CRowArray &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))
{
return false;
}
else
{
m_dTotalRows = 1;
return true;
}
}
}
// Clear text delimited file content
bool CSpreadSheet::DeleteSheet()
{
if (m_bExcel)
{
if (DeleteSheet(m_sSheetName))
{
return true;
}
else
{
m_sLastError = "Error deleting sheet\n";
return false;
}
}
else
{
m_aRows.RemoveAll();
m_aFieldNames.RemoveAll();
m_dTotalColumns = 0;
m_dTotalRows = 0;
if (!m_bTransaction)
{
Commit();
}
m_bAppend = false; // Set flag to new sheet
return true;
}
}
// Clear entire Excel spreadsheet content. The sheet itself is not deleted
bool CSpreadSheet::DeleteSheet(CString SheetName)
{
if (m_bExcel) // If file is an Excel spreadsheet
{
// Delete sheet
m_Database->OpenEx(m_sDsn, CDatabase::noOdbcDialog);
SheetName = "[" + SheetName + "$A1:IV65536]";
m_stempSql.Format ("DROP TABLE %s", SheetName);
try
{
m_Database->ExecuteSQL(m_stempSql);
m_Database->Close();
m_aRows.RemoveAll();
m_aFieldNames.RemoveAll();
m_dTotalColumns = 0;
m_dTotalRows = 0;
}
catch(CDBException *e)
{
m_sLastError = e->m_strError;
m_Database->Close();
return false;
}
return true;
}
else // if file is a text delimited file
{
return DeleteSheet();
}
}
// Insert or replace a row into spreadsheet.
// Default is add new row.
bool CSpreadSheet::AddRow(CRowArray &RowValues, long row, bool replace)
{
long tempRow;
if (row == 1)
{
if (m_bExcel)
{
// Check for duplicate header row field for Excel spreadsheet
for (int i = 0; i < RowValues.GetSize(); i++)
{
for (int j = 0; j < RowValues.GetSize(); j++)
{
if ((i != j) && (RowValues.GetAt(i) == RowValues.GetAt(j)))
{
m_sLastError.Format("Duplicate header row field:%s\n", RowValues.GetAt(i));
return false;
}
}
}
// Check for reduced header row columns
if (RowValues.GetSize() < m_dTotalColumns)
{
m_sLastError = "Number of columns in new header row cannot be less than the number of columns in previous header row";
return false;
}
m_dTotalColumns = RowValues.GetSize();
}
// Update header row
m_aFieldNames.RemoveAll();
m_aFieldNames.Copy(RowValues);
}
else
{
if (m_bExcel)
{
if (m_dTotalColumns == 0)
{
m_sLastError = "No header row. Add header row first\n";
return false;
}
}
}
if (m_bExcel) // For Excel spreadsheet
{
if (RowValues.GetSize() > m_aFieldNames.GetSize())
{
m_sLastError = "Number of columns to be added cannot be greater than the number of fields\n";
return false;
}
}
else // For text delimited spreadsheet
{
// Update largest number of columns if necessary
if (RowValues.GetSize() > m_dTotalColumns)
{
m_dTotalColumns = RowValues.GetSize();
}
}
// Convert row values
m_stempString.Empty();
for (int i = 0; i < RowValues.GetSize(); i++)
{
if (i != RowValues.GetSize()-1) // Not last column
{
m_stempSql.Format("\"%s\"%s", RowValues.GetAt(i), m_sSeparator);
m_stempString += m_stempSql;
}
else // Last column
{
m_stempSql.Format("\"%s\"", RowValues.GetAt(i));
m_stempString += m_stempSql;
}
}
if (row)
{
if (row <= m_dTotalRows) // Not adding new rows
{
if (replace) // Replacing row
{
m_aRows.SetAt(row-1, m_stempString);
}
else // Inserting row
{
m_aRows.InsertAt(row-1, m_stempString);
m_dTotalRows++;
}
if (!m_bTransaction)
{
Commit();
}
return true;
}
else // Adding new rows
{
// Insert null rows until specified row
m_dCurrentRow = m_dTotalRows;
m_stempSql.Empty();
CString nullString;
for (int i = 1; i <= m_dTotalColumns; i++)
{
if (i != m_dTotalColumns)
{
if (m_bExcel)
{
nullString.Format("\" \"%s", m_sSeparator);
}
else
{
nullString.Format("\"\"%s", m_sSeparator);
}
m_stempSql += nullString;
}
else
{
if (m_bExcel)
{
m_stempSql += "\" \"";
}
else
{
m_stempSql += "\"\"";
}
}
}
for (int j = m_dTotalRows + 1; j < row; j++)
{
m_dCurrentRow++;
m_aRows.Add(m_stempSql);
}
}
}
else
{
tempRow = m_dCurrentRow;
m_dCurrentRow = m_dTotalRows;
}
// Insert new row
m_dCurrentRow++;
m_aRows.Add(m_stempString);
if (row > m_dTotalRows)
{
m_dTotalRows = row;
}
else if (!row)
{
m_dTotalRows = m_dCurrentRow;
m_dCurrentRow = tempRow;
}
if (!m_bTransaction)
{
Commit();
}
return 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 CSpreadSheet::AddCell(CString CellValue, CString column, long row, bool Auto)
{
short columnIndex = CalculateColumnNumber(column, Auto);
if (columnIndex == 0)
{
return false;
}
if (AddCell(CellValue, columnIndex, row))
{
return true;
}
return false;
}
// Replace or add a cell into spreadsheet using column number
// Default is add cell into new row.
bool CSpreadSheet::AddCell(CString CellValue, short column, long row)
{
if (column == 0)
{
m_sLastError = "Column cannot be zero\n";
return false;
}
long tempRow;
if (m_bExcel) // For Excel spreadsheet
{
if (column > m_aFieldNames.GetSize() + 1)
{
m_sLastError = "Cell column to be added cannot be greater than the number of fields\n";
return false;
}
}
else // For text delimited spreadsheet
{
// Update largest number of columns if necessary
if (column > m_dTotalColumns)
{
m_dTotalColumns = column;
}
}
if (row)
{
if (row <= m_dTotalRows)
{
ReadRow(m_atempArray, row);
// Change desired row
m_atempArray.SetAtGrow(column-1, CellValue);
if (row == 1)
{
if (m_bExcel) // Check for duplicate header row field
{
for (int i = 0; i < m_atempArray.GetSize(); i++)
{
for (int j = 0; j < m_atempArray.GetSize(); j++)
{
if ((i != j) && (m_atempArray.GetAt(i) == m_atempArray.GetAt(j)))
{
m_sLastError.Format("Duplicate header row field:%s\n", m_atempArray.GetAt(i));
return false;
}
}
}
}
// Update header row
m_aFieldNames.RemoveAll();
m_aFieldNames.Copy(m_atempArray);
}
if (!AddRow(m_atempArray, row, true))
{
return false;
}
if (!m_bTransaction)
{
Commit();
}
return true;
}
else
{
// Insert null rows until specified row
m_dCurrentRow = m_dTotalRows;
m_stempSql.Empty();
CString nullString;
for (int i = 1; i <= m_dTotalColumns; i++)
{
if (i != m_dTotalColumns)
{
if (m_bExcel)
{
nullString.Format("\" \"%s", m_sSeparator);
}
else
{
nullString.Format("\"\"%s", m_sSeparator);
}
m_stempSql += nullString;
}
else
{
if (m_bExcel)
{
m_stempSql += "\" \"";
}
else
{
m_stempSql += "\"\"";
}
}
}
for (int j = m_dTotalRows + 1; j < row; j++)
{
m_dCurrentRow++;
m_aRows.Add(m_stempSql);
}
}
}
else
{
tempRow = m_dCurrentRow;
m_dCurrentRow = m_dTotalRows;
}
// Insert cell
m_dCurrentRow++;
m_stempString.Empty();
for (int j = 1; j <= m_dTotalColumns; j++)
{
if (j != m_dTotalColumns) // Not last column
{
if (j != column)
{
if (m_bExcel)
{
m_stempSql.Format("\" \"%s", m_sSeparator);
}
else
{
m_stempSql.Format("\"\"%s", m_sSeparator);
}
m_stempString += m_stempSql;
}
else
{
m_stempSql.Format("\"%s\"%s", CellValue, m_sSeparator);
m_stempString += m_stempSql;
}
}
else // Last column
{
if (j != column)
{
if (m_bExcel)
{
m_stempString += "\" \"";
}
else
{
m_stempString += "\"\"";
}
}
else
{
m_stempSql.Format("\"%s\"", CellValue);
m_stempString += m_stempSql;
}
}
}
m_aRows.Add(m_stempString);
if (row > m_dTotalRows)
{
m_dTotalRows = row;
}
else if (!row)
{
m_dTotalRows = m_dCurrentRow;
m_dCurrentRow = tempRow;
}
if (!m_bTransaction)
{
Commit();
}
return true;
}
// Search and replace rows in Excel spreadsheet
bool CSpreadSheet::ReplaceRows(CRowArray &NewRowValues, CRowArray &OldRowValues)
{
if (m_bExcel) // If file is an Excel spreadsheet
{
m_Database->OpenEx(m_sDsn, CDatabase::noOdbcDialog);
m_stempSql.Format("UPDATE [%s] SET ", m_sSheetName);
for (int i = 0; i < NewRowValues.GetSize(); i++)
{
// m_stempString.Format("[%s]='%s', ", m_aFieldNames.GetAt(i), NewRowValues.GetAt(i)); fx
m_stempString="["+m_aFieldNames.GetAt(i)+"]="+NewRowValues.GetFormatedValueString(i,m_aFieldNames.GetDataType(i));
m_stempSql = m_stempSql + m_stempString;
}
m_stempSql.Delete(m_stempSql.GetLength()-2, 2);
m_stempSql = m_stempSql + " WHERE (";
for (int j = 0; j < OldRowValues.GetSize()-1; j++)
{
// m_stempString.Format("[%s]='%s' AND ", m_aFieldNames.GetAt(j), OldRowValues.GetAt(j)); fx
m_stempString="["+m_aFieldNames.GetAt(j)+"]="+OldRowValues.GetFormatedValueString(j,m_aFieldNames.GetDataType(j))+" AND ";
m_stempSql = m_stempSql + m_stempString;
}
m_stempSql.Delete(m_stempSql.GetLength()-4, 5);
m_stempSql += ")";
try
{
m_Database->ExecuteSQL(m_stempSql);
m_Database->Close();
Open();
return true;
}
catch(CDBException *e)
{
m_sLastError = e->m_strError;
m_Database->Close();
return false;
}
}
else // if file is a text delimited file
{
m_sLastError = "Function not available for text delimited file\n";
return false;
}
}
// Read a row from spreadsheet.
// Default is read the next row
bool CSpreadSheet::ReadRow(CRowArray &RowValues, long row)
{
// Check if row entered is more than number of rows in sheet
if (row <= m_aRows.GetSize())
{
if (row != 0)
{
m_dCurrentRow = row;
}
else if (m_dCurrentRow > m_aRows.GetSize())
{
return false;
}
// Read the desired row
RowValues.RemoveAll();
m_stempString = m_aRows.GetAt(m_dCurrentRow-1);
m_stempString.Replace("'","''"); // Fx add
m_dCurrentRow++;
// Search for separator to split row
int separatorPosition;
m_stempSql.Format("\"%s\"", m_sSeparator);
separatorPosition = m_stempString.Find(m_stempSql); // If separator is "?"
if (separatorPosition != -1)
{
// Save columns
int nCount = 0;
int stringStartingPosition = 0;
while (separatorPosition != -1)
{
nCount = separatorPosition - stringStartingPosition;
RowValues.Add(m_stempString.Mid(stringStartingPosition, nCount));
stringStartingPosition = separatorPosition + m_stempSql.GetLength();
separatorPosition = m_stempString.Find(m_stempSql, stringStartingPosition);
}
nCount = m_stempString.GetLength() - stringStartingPosition;
RowValues.Add(m_stempString.Mid(stringStartingPosition, nCount));
// Remove quotes from first column
m_stempString = RowValues.GetAt(0);
m_stempString.Delete(0, 1);
RowValues.SetAt(0, m_stempString);
// Remove quotes from last column
m_stempString = RowValues.GetAt(RowValues.GetSize()-1);
m_stempString.Delete(m_stempString.GetLength()-1, 1);
RowValues.SetAt(RowValues.GetSize()-1, m_stempString);
return true;
}
else
{
// Save columns
separatorPosition = m_stempString.Find(m_sSeparator); // if separator is ?
if (separatorPosition != -1)
{
int nCount = 0;
int stringStartingPosition = 0;
while (separatorPosition != -1)
{
nCount = separatorPosition - stringStartingPosition;
RowValues.Add(m_stempString.Mid(stringStartingPosition, nCount));
stringStartingPosition = separatorPosition + m_sSeparator.GetLength();
separatorPosition = m_stempString.Find(m_sSeparator, stringStartingPosition);
}
nCount = m_stempString.GetLength() - stringStartingPosition;
RowValues.Add(m_stempString.Mid(stringStartingPosition, nCount));
return true;
}
else // Treat spreadsheet as having one column
{
// Remove opening and ending quotes if any
int quoteBegPos = m_stempString.Find('\"');
int quoteEndPos = m_stempString.ReverseFind('\"');
if ((quoteBegPos == 0) && (quoteEndPos == m_stempString.GetLength()-1))
{
m_stempString.Delete(0, 1);
m_stempString.Delete(m_stempString.GetLength()-1, 1);
}
RowValues.Add(m_stempString);
}
}
}
m_sLastError = "Desired row is greater than total number of rows in spreadsheet\n";
return false;
}
// 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 CSpreadSheet::ReadColumn(CStringArray &ColumnValues, CString column, bool Auto)
{
short columnIndex = CalculateColumnNumber(column, Auto);
if (columnIndex == 0)
{
return false;
}
if (ReadColumn(ColumnValues, columnIndex))
{
return true;
}
return false;
}
// Read a column from spreadsheet using column number
bool CSpreadSheet::ReadColumn(CStringArray &ColumnValues, short column)
{
if (column == 0)
{
m_sLastError = "Column cannot be zero\n";
return false;
}
int tempRow = m_dCurrentRow;
m_dCurrentRow = 1;
ColumnValues.RemoveAll();
for (int i = 1; i <= m_aRows.GetSize(); i++)
{
// Read each row
if (ReadRow(m_atempArray, i))
{
// Get value of cell in desired column
if (column <= m_atempArray.GetSize())
{
ColumnValues.Add(m_atempArray.GetAt(column-1));
}
else
{
ColumnValues.Add("");
}
}
else
{
m_dCurrentRow = tempRow;
m_sLastError = "Error reading row\n";
return false;
}
}
m_dCurrentRow = tempRow;
return 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 CSpreadSheet::ReadCell (CString &CellValue, CString column, long row, bool Auto)
{
short columnIndex = CalculateColumnNumber(column, Auto);
if (columnIndex == 0)
{
return false;
}
if (ReadCell(CellValue, columnIndex, row))
{
return true;
}
return false;
}
// Read a cell from spreadsheet using column number.
// Default is read the next cell in next row.
bool CSpreadSheet::ReadCell (CString &CellValue, short column, long row)
{
if (column == 0)
{
m_sLastError = "Column cannot be zero\n";
return false;
}
int tempRow = m_dCurrentRow;
if (row)
{
m_dCurrentRow = row;
}
if (ReadRow(m_atempArray, m_dCurrentRow))
{
// Get value of cell in desired column
if (column <= m_atempArray.GetSize())
{
CellValue = m_atempArray.GetAt(column-1);
}
else
{
CellValue.Empty();
m_dCurrentRow = tempRow;
return false;
}
m_dCurrentRow = tempRow;
return true;
}
m_dCurrentRow = tempRow;
m_sLastError = "Error reading row\n";
return false;
}
// Begin transaction
void CSpreadSheet::BeginTransaction()
{
m_bTransaction = true;
}
// Save changes to spreadsheet
bool CSpreadSheet::Commit()
{
if (m_bExcel) // If file is an Excel spreadsheet
{
m_Database->OpenEx(m_sDsn, CDatabase::noOdbcDialog);
if (m_bAppend)
{
// Delete old sheet if it exists
m_stempString= "[" + m_sSheetName + "$A1:IV65536]";
m_stempSql.Format ("DROP TABLE %s", m_stempString);
try