1395 lines
30 KiB
C
1395 lines
30 KiB
C
|
/////////////////////////////////////////////////////////////////////////
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>CSpreadSheet
|
|||
|
// <20><><EFBFBD>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD>дExcel<65>ļ<EFBFBD><C4BC><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>ļ<EFBFBD><C4BC><EFBFBD>
|
|||
|
// <20><>ע<EFBFBD><D7A2><EFBFBD><EFBFBD><EFBFBD>롢<EFBFBD><EBA1A2><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>Excel<65>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>ļ<EFBFBD>
|
|||
|
// <20>ģ<DEB8><C4A3>쾰<EFBFBD><ECBEB0>(jingzhou_xu@163.com)
|
|||
|
// <20><>֯<EFBFBD><D6AF>Future Studio
|
|||
|
// <20><><EFBFBD>ڣ<EFBFBD>2003.1.8
|
|||
|
////////////////////////////////////////////////////////////////////////
|
|||
|
#ifndef CSPREADSHEET_H
|
|||
|
#define CSPREADSHEET_H
|
|||
|
|
|||
|
#include <odbcinst.h>
|
|||
|
#include <afxdb.h>
|
|||
|
|
|||
|
class CSpreadSheet
|
|||
|
{
|
|||
|
public:
|
|||
|
CSpreadSheet(CString File, CString SheetOrSeparator, bool Backup = false); // Ϊ<><CEAA><EFBFBD>ж<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>Ԥ<EFBFBD><D4A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
~CSpreadSheet(); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
void Close();
|
|||
|
|
|||
|
bool AddHeaders(CStringArray &FieldNames, bool replace = false); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ
|
|||
|
bool DeleteSheet(); // ɾ<><C9BE><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
bool DeleteSheet(CString SheetName); // ɾ<><C9BE>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE>
|
|||
|
bool AddRow(CStringArray &RowValues, long row = 0, bool replace = false); // <20><><EFBFBD>Ż<EFBFBD><C5BB>滻һ<E6BBBB><D2BB>
|
|||
|
bool AddCell(CString CellValue, CString column, long row = 0, bool Auto = true); // ʹ<><CAB9><EFBFBD>кŻ<D0BA><C5BB><EFBFBD>ĸ<EFBFBD>к<EFBFBD><D0BA>滻<EFBFBD><E6BBBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>Ԫ<EFBFBD><D4AA>
|
|||
|
bool AddCell(CString CellValue, short column, long row = 0); // ʹ<><CAB9><EFBFBD>С<EFBFBD><D0A1>к<EFBFBD><D0BA>滻<EFBFBD><E6BBBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>Ԫ<EFBFBD><D4AA>
|
|||
|
bool ReplaceRows(CStringArray &NewRowValues, CStringArray &OldRowValues); // <20><>Excel<65><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>滻<EFBFBD><E6BBBB>
|
|||
|
bool ReadRow(CStringArray &RowValues, long row = 0); // <20><>ȡһ<C8A1><D2BB>
|
|||
|
bool ReadColumn(CStringArray &ColumnValues, CString column, bool Auto = true); // ʹ<><CAB9><EFBFBD>кš<D0BA><C5A1><EFBFBD>ĸ<EFBFBD>кŶ<D0BA>ȡһ<C8A1><D2BB>
|
|||
|
bool ReadColumn(CStringArray &ColumnValues, short column); // ʹ<><CAB9><EFBFBD>кŶ<D0BA>ȡһ<C8A1><D2BB>
|
|||
|
bool ReadCell (CString &CellValue, CString column, long row = 0, bool Auto = true); // ʹ<><CAB9><EFBFBD>кš<D0BA><C5A1><EFBFBD>ĸ<EFBFBD>кŶ<D0BA>ȡ<EFBFBD><C8A1>Ԫ<EFBFBD><D4AA>
|
|||
|
bool ReadCell (CString &CellValue, short column, long row = 0); // ʹ<><CAB9><EFBFBD>кŶ<D0BA>ȡ<EFBFBD><C8A1>Ԫ<EFBFBD><D4AA>
|
|||
|
void BeginTransaction(); // <20><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
|||
|
bool Commit(); // <20><><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD>״̬
|
|||
|
bool RollBack(); // <20>ع<EFBFBD><D8B9>ı<EFBFBD>״̬
|
|||
|
bool Convert(CString SheetOrSeparator);
|
|||
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>
|
|||
|
inline void GetFieldNames (CStringArray &FieldNames) {FieldNames.RemoveAll(); FieldNames.Copy(m_aFieldNames);}
|
|||
|
inline long GetTotalRows() {return m_dTotalRows;} // <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
inline short GetTotalColumns() {return m_dTotalColumns;} // <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
inline long GetCurrentRow() {return m_dCurrentRow;} // <20><>ȡ<EFBFBD><C8A1>ǰѡ<C7B0><D1A1><EFBFBD><EFBFBD>
|
|||
|
inline bool GetBackupStatus() {return m_bBackup;} // <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>״̬. True<75><65>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>, False<73><65><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD>
|
|||
|
inline bool GetTransactionStatus() {return m_bTransaction;} // <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>״̬. True<75><65>ʾ<EFBFBD><CABE>ʼ, False<73><65>ʾû<CABE>п<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ʼʱ<CABC><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
inline CString GetLastError() {return m_sLastError;} // <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|||
|
|
|||
|
private:
|
|||
|
bool Open(); // Ϊ<><CEAA><EFBFBD>ж<EFBFBD>д<EFBFBD><EFBFBD><F2BFAAB4><EFBFBD>
|
|||
|
void GetExcelDriver(); // <20><>ȡExcel-ODBC<42><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
short CalculateColumnNumber(CString column, bool Auto); // ת<><D7AA>Excel<65><6C>ĸ<EFBFBD>к<EFBFBD>Ϊ<EFBFBD><CEAA>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>к<EFBFBD>
|
|||
|
|
|||
|
bool m_bAppend; // <20>½<EFBFBD><C2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD>־
|
|||
|
bool m_bBackup; // <20><><EFBFBD><EFBFBD>״̬<D7B4>ڲ<EFBFBD><DAB2><EFBFBD>־
|
|||
|
bool m_bExcel; // <20><>Excel<65><6C><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>Ƿָ<C7B7><D6B8><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>ļ<EFBFBD><C4BC>ڲ<EFBFBD><DAB2><EFBFBD>־
|
|||
|
bool m_bTransaction; // <20><><EFBFBD><EFBFBD>״̬<D7B4>ڲ<EFBFBD><DAB2><EFBFBD>־
|
|||
|
|
|||
|
long m_dCurrentRow; // <20><>ǰ<EFBFBD>к<EFBFBD><D0BA><EFBFBD><EFBFBD><EFBFBD>, <20><>ʼΪ1
|
|||
|
long m_dTotalRows; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
short m_dTotalColumns; // Excel<65>ļ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
CString m_sSql; // <20><><EFBFBD><EFBFBD>Excel<65><6C><EFBFBD>ж<EFBFBD>дSQL<51><4C><EFBFBD><EFBFBD>
|
|||
|
CString m_sDsn; // <20><><EFBFBD><EFBFBD>Excel<65><6C><EFBFBD>ж<EFBFBD>д<EFBFBD><D0B4>DSN<53><4E>
|
|||
|
CString m_stempSql; // <20><>ʱSQL<51>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
CString m_stempString; // <20><>ʱ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
CString m_sSheetName; // Excel<65><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
CString m_sExcelDriver; // Excel<65><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
CString m_sFile; // <20>ļ<EFBFBD><C4BC><EFBFBD>
|
|||
|
CString m_sSeparator; // <20>ı<EFBFBD><C4B1>ļ<EFBFBD><C4BC>ָ<EFBFBD><D6B8><EFBFBD>
|
|||
|
CString m_sLastError; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|||
|
|
|||
|
CStringArray m_atempArray; // <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
|
|||
|
CStringArray m_aFieldNames; // <20>ֶ<EFBFBD><D6B6><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
CStringArray m_aRows; // <20>д洢
|
|||
|
|
|||
|
CDatabase *m_Database; // <20><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
CRecordset *m_rSheet; // <20><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
};
|
|||
|
|
|||
|
// Ϊ<><CEAA>д<EFBFBD><D0B4><EFBFBD>д<D0B4><F2BFAAB4><EFBFBD>(File -- <20>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>SheetOrSeparator -- Excel<65><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>Backup -- <20>Ƿ<C7B7><F1B4B4BD><EFBFBD><EFBFBD>ݱ<EFBFBD>)
|
|||
|
CSpreadSheet::CSpreadSheet(CString File, CString SheetOrSeparator, bool Backup /* = false */) :
|
|||
|
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)
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>Excel<65><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿָ<C7B7><D6B8><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>ļ<EFBFBD>
|
|||
|
m_stempString = m_sFile.Right(4);
|
|||
|
m_stempString.MakeLower();
|
|||
|
if (m_stempString == ".xls") // <20><><EFBFBD><EFBFBD>Excel<65><6C><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>ʱ
|
|||
|
{
|
|||
|
m_bExcel = true;
|
|||
|
m_sSheetName = SheetOrSeparator;
|
|||
|
m_sSeparator = " ,;.?";
|
|||
|
}
|
|||
|
else // <20><><EFBFBD>Ƿָ<C7B7><D6B8><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>ļ<EFBFBD>ʱ
|
|||
|
{
|
|||
|
m_bExcel = false;
|
|||
|
m_sSeparator = SheetOrSeparator;
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>Excel<65>ļ<EFBFBD>ʱ
|
|||
|
if (m_bExcel)
|
|||
|
{
|
|||
|
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()) // <20>Ƿ<EFBFBD><C7B7>Ѵ<EFBFBD><D1B4><EFBFBD>Excel<65>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD>
|
|||
|
{
|
|||
|
if (m_bBackup) // Excel<65>б<EFBFBD><D0B1><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD>ԴĻ<F2BFAAB5><C4BB><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>ԭʼ<D4AD><CABC><EFBFBD>ݱ<EFBFBD>
|
|||
|
{
|
|||
|
if ((m_bBackup) && (m_bAppend))
|
|||
|
{
|
|||
|
CString tempSheetName = m_sSheetName;
|
|||
|
m_sSheetName = m_sSheetName + "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
|
|||
|
m_bAppend = false;
|
|||
|
|
|||
|
// <20>ڴ˴<DAB4><CBB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݱ<EFBFBD>
|
|||
|
if (!Commit())
|
|||
|
{
|
|||
|
m_bBackup = false;
|
|||
|
}
|
|||
|
m_bAppend = true;
|
|||
|
m_sSheetName = tempSheetName;
|
|||
|
m_dCurrentRow = 1;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
else // <20><><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>ļ<EFBFBD>ʱ
|
|||
|
{
|
|||
|
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;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
CSpreadSheet::~CSpreadSheet()
|
|||
|
{
|
|||
|
Close();
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
void CSpreadSheet::Close()
|
|||
|
{
|
|||
|
if ( NULL != m_rSheet )
|
|||
|
{
|
|||
|
m_rSheet->Close();
|
|||
|
delete m_rSheet;
|
|||
|
m_rSheet = NULL;
|
|||
|
}
|
|||
|
if (NULL != m_Database)
|
|||
|
{
|
|||
|
m_Database->Close();
|
|||
|
delete m_Database;
|
|||
|
m_Database=NULL;
|
|||
|
}
|
|||
|
}
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
bool CSpreadSheet::AddHeaders(CStringArray &FieldNames, bool replace)
|
|||
|
{
|
|||
|
if (m_bAppend) // ԭ<><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><D7B7>
|
|||
|
{
|
|||
|
if (replace) // <20>滻<EFBFBD><E6BBBB><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
if (!AddRow(FieldNames, 1, true))
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return true;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (ReadRow(m_atempArray, 1)) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
if (m_bExcel)
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>ֶ<EFBFBD><D6B6>Ƿ<EFBFBD><C7B7>ظ<EFBFBD>
|
|||
|
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("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>ֶ<EFBFBD><EFBFBD>ظ<EFBFBD>:%s\n", FieldNames.GetAt(i));
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
m_atempArray.Append(FieldNames);
|
|||
|
if (!AddRow(m_atempArray, 1, true))
|
|||
|
{
|
|||
|
m_sLastError = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n";
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (m_atempArray.GetSize() > m_dTotalColumns)
|
|||
|
{
|
|||
|
m_dTotalColumns = m_atempArray.GetSize();
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
else // <20>±<EFBFBD><C2B1><EFBFBD>
|
|||
|
{
|
|||
|
m_dTotalColumns = FieldNames.GetSize();
|
|||
|
if (!AddRow(FieldNames, 1, true))
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
m_dTotalRows = 1;
|
|||
|
return true;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// ɾ<><C9BE><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
bool CSpreadSheet::DeleteSheet()
|
|||
|
{
|
|||
|
if (m_bExcel)
|
|||
|
{
|
|||
|
if (DeleteSheet(m_sSheetName))
|
|||
|
{
|
|||
|
return true;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
m_sLastError = "ɾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n";
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
m_aRows.RemoveAll();
|
|||
|
m_aFieldNames.RemoveAll();
|
|||
|
m_dTotalColumns = 0;
|
|||
|
m_dTotalRows = 0;
|
|||
|
if (!m_bTransaction)
|
|||
|
{
|
|||
|
Commit();
|
|||
|
}
|
|||
|
m_bAppend = false; // <20><><EFBFBD><EFBFBD><EFBFBD>±<EFBFBD><C2B1><EFBFBD><EFBFBD><EFBFBD>־
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// ɾ<><C9BE>ָ<EFBFBD><D6B8>Excel<65><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE>
|
|||
|
bool CSpreadSheet::DeleteSheet(CString SheetName)
|
|||
|
{
|
|||
|
if (m_bExcel) // <20><><EFBFBD><EFBFBD>Excel<65><6C><EFBFBD><EFBFBD>ʱ
|
|||
|
{
|
|||
|
// ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
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 *pEx)
|
|||
|
{
|
|||
|
m_sLastError = pEx->m_strError;
|
|||
|
m_Database->Close();
|
|||
|
pEx->Delete();
|
|||
|
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
else // <20><><EFBFBD>Ƿָ<C7B7><D6B8><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>ļ<EFBFBD>ʱ
|
|||
|
{
|
|||
|
return DeleteSheet();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD>Ż<EFBFBD><C5BB>滻һ<E6BBBB>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>Ĭ<EFBFBD><C4AC>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
|
|||
|
bool CSpreadSheet::AddRow(CStringArray &RowValues, long row, bool replace)
|
|||
|
{
|
|||
|
long tempRow;
|
|||
|
|
|||
|
if (row == 1)
|
|||
|
{
|
|||
|
if (m_bExcel)
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD>Excel<65><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>ֶ<EFBFBD><D6B6>Ƿ<EFBFBD><C7B7>ظ<EFBFBD>
|
|||
|
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("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>ֶ<EFBFBD><EFBFBD>ظ<EFBFBD>:%s\n", RowValues.GetAt(i));
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>С
|
|||
|
if (RowValues.GetSize() < m_dTotalColumns)
|
|||
|
{
|
|||
|
m_sLastError = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><EFBFBD>ԭ<EFBFBD><EFBFBD>ͷ<EFBFBD><EFBFBD>";
|
|||
|
return false;
|
|||
|
}
|
|||
|
m_dTotalColumns = RowValues.GetSize();
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><>ͷ)
|
|||
|
m_aFieldNames.RemoveAll();
|
|||
|
m_aFieldNames.Copy(RowValues);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
if (m_bExcel)
|
|||
|
{
|
|||
|
if (m_dTotalColumns == 0)
|
|||
|
{
|
|||
|
m_sLastError = "û<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ\n";
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (m_bExcel) // Excel<65><6C><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>ʱ
|
|||
|
{
|
|||
|
if (RowValues.GetSize() > m_aFieldNames.GetSize())
|
|||
|
{
|
|||
|
m_sLastError = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n";
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
else // <20>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>ļ<EFBFBD>ʱ
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (RowValues.GetSize() > m_dTotalColumns)
|
|||
|
{
|
|||
|
m_dTotalColumns = RowValues.GetSize();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// ת<><D7AA><EFBFBD><EFBFBD>ֵ
|
|||
|
m_stempString.Empty();
|
|||
|
for (int i = 0; i < RowValues.GetSize(); i++)
|
|||
|
{
|
|||
|
if (i != RowValues.GetSize()-1) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
|||
|
{
|
|||
|
m_stempSql.Format("\"%s\"%s", RowValues.GetAt(i), m_sSeparator);
|
|||
|
m_stempString += m_stempSql;
|
|||
|
}
|
|||
|
else // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
|||
|
{
|
|||
|
m_stempSql.Format("\"%s\"", RowValues.GetAt(i));
|
|||
|
m_stempString += m_stempSql;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (row)
|
|||
|
{
|
|||
|
if (row <= m_dTotalRows) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
if (replace) // <20>滻<EFBFBD><E6BBBB>
|
|||
|
{
|
|||
|
m_aRows.SetAt(row-1, m_stempString);
|
|||
|
}
|
|||
|
else // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
m_aRows.InsertAt(row-1, m_stempString);
|
|||
|
m_dTotalRows++;
|
|||
|
}
|
|||
|
|
|||
|
if (!m_bTransaction)
|
|||
|
{
|
|||
|
Commit();
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
else // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1>ָ<EFBFBD><D6B8><EFBFBD>к<EFBFBD>
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
// ʹ<><CAB9><EFBFBD>кš<D0BA><C5A1><EFBFBD>ĸ<EFBFBD>к<EFBFBD><D0BA>滻<EFBFBD><E6BBBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>Excel<65><6C><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>Ĭ<EFBFBD><C4AC>Ϊ<EFBFBD><CEAA><EFBFBD>ӵ<EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>ĸ<EFBFBD>к<EFBFBD><D0BA><EFBFBD>Ϊ<EFBFBD><CEAA>ͷ<EFBFBD><CDB7><EFBFBD>ɽ<EFBFBD>Auto<74><6F>Ϊfalse
|
|||
|
bool CSpreadSheet::AddCell(CString CellValue, CString column, long row, bool Auto)
|
|||
|
{
|
|||
|
short columnIndex = CalculateColumnNumber(column, Auto);
|
|||
|
if (0 == columnIndex)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
if (AddCell(CellValue, columnIndex, row))
|
|||
|
{
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
// ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>кš<D0BA><C5A1>к<EFBFBD><D0BA>滻<EFBFBD><E6BBBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD><F1B5BDB1><EFBFBD><EFBFBD>У<EFBFBD>Ĭ<EFBFBD><C4AC>Ϊ<EFBFBD><CEAA><EFBFBD>ӵ<EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
|
|||
|
bool CSpreadSheet::AddCell(CString CellValue, short column, long row)
|
|||
|
{
|
|||
|
if (0 == column)
|
|||
|
{
|
|||
|
m_sLastError = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ0\n";
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
long tempRow;
|
|||
|
|
|||
|
if (m_bExcel) // <20><><EFBFBD><EFBFBD>Excel<65><6C><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>ʱ
|
|||
|
{
|
|||
|
if (column > m_aFieldNames.GetSize() + 1)
|
|||
|
{
|
|||
|
m_sLastError = "ָ<EFBFBD><EFBFBD><EFBFBD>кŴ<EFBFBD><EFBFBD>ڱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n";
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
else // <20><><EFBFBD>Ƿָ<C7B7><D6B8><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>ļ<EFBFBD>ʱ
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (column > m_dTotalColumns)
|
|||
|
{
|
|||
|
m_dTotalColumns = column;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (row)
|
|||
|
{
|
|||
|
if (row <= m_dTotalRows)
|
|||
|
{
|
|||
|
ReadRow(m_atempArray, row);
|
|||
|
|
|||
|
// <20>ı<EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>
|
|||
|
m_atempArray.SetAtGrow(column-1, CellValue);
|
|||
|
|
|||
|
if (row == 1)
|
|||
|
{
|
|||
|
if (m_bExcel) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD>ֶ<EFBFBD><D6B6>Ƿ<EFBFBD><C7B7>ظ<EFBFBD>
|
|||
|
{
|
|||
|
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("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD><EFBFBD>ظ<EFBFBD>(<28><>ͷ):%s\n", m_atempArray.GetAt(i));
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><>ͷ)
|
|||
|
m_aFieldNames.RemoveAll();
|
|||
|
m_aFieldNames.Copy(m_atempArray);
|
|||
|
}
|
|||
|
|
|||
|
if (!AddRow(m_atempArray, row, true))
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
if (!m_bTransaction)
|
|||
|
{
|
|||
|
Commit();
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֱ<EFBFBD><D6B1>ָ<EFBFBD><D6B8><EFBFBD>к<EFBFBD>
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD>뵥Ԫ<EBB5A5><D4AA>
|
|||
|
m_dCurrentRow++;
|
|||
|
m_stempString.Empty();
|
|||
|
for (int j = 1; j <= m_dTotalColumns; j++)
|
|||
|
{
|
|||
|
if (j != m_dTotalColumns) // û<><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
|||
|
{
|
|||
|
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 // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
|||
|
{
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>滻Excel<65><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
bool CSpreadSheet::ReplaceRows(CStringArray &NewRowValues, CStringArray &OldRowValues)
|
|||
|
{
|
|||
|
if (m_bExcel) // <20><><EFBFBD><EFBFBD>Excel<65><6C><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>ʱ
|
|||
|
{
|
|||
|
// <20><F2BFAABB>Excel<65>ļ<EFBFBD>
|
|||
|
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));
|
|||
|
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));
|
|||
|
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 *pEx)
|
|||
|
{
|
|||
|
m_sLastError = pEx->m_strError;
|
|||
|
m_Database->Close();
|
|||
|
pEx->Delete();
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
else // <20><><EFBFBD>Ƿָ<C7B7><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>ļ<EFBFBD>ʱ
|
|||
|
{
|
|||
|
m_sLastError = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Էָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>Ч\n";
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>У<EFBFBD>Ĭ<EFBFBD><C4AC>Ϊ<EFBFBD><CEAA>ȡ<EFBFBD><C8A1>һ<EFBFBD><D2BB>
|
|||
|
bool CSpreadSheet::ReadRow(CStringArray &RowValues, long row)
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>к<EFBFBD><D0BA>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (row <= m_aRows.GetSize())
|
|||
|
{
|
|||
|
if (0 != row)
|
|||
|
{
|
|||
|
m_dCurrentRow = row;
|
|||
|
}
|
|||
|
else if (m_dCurrentRow > m_aRows.GetSize())
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
// <20><>ȡָ<C8A1><D6B8><EFBFBD><EFBFBD>
|
|||
|
RowValues.RemoveAll();
|
|||
|
m_stempString = m_aRows.GetAt(m_dCurrentRow-1);
|
|||
|
m_dCurrentRow++;
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>еķָ<C4B7><D6B8><EFBFBD>
|
|||
|
int separatorPosition;
|
|||
|
m_stempSql.Format("\"%s\"", m_sSeparator);
|
|||
|
separatorPosition = m_stempString.Find(m_stempSql); // <20><><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>"?"
|
|||
|
if (separatorPosition != -1)
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
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));
|
|||
|
|
|||
|
// ɾ<><C9BE><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>÷<EFBFBD><C3B7><EFBFBD>
|
|||
|
m_stempString = RowValues.GetAt(0);
|
|||
|
m_stempString.Delete(0, 1);
|
|||
|
RowValues.SetAt(0, m_stempString);
|
|||
|
|
|||
|
// ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>÷<EFBFBD><C3B7><EFBFBD>
|
|||
|
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
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
separatorPosition = m_stempString.Find(m_sSeparator); // <20><><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>"?"
|
|||
|
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 // <20><><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB>һ<EFBFBD><D2BB>ʱ
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD>ʼ<EFBFBD>ͽ<EFBFBD><CDBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>÷<EFBFBD><C3B7><EFBFBD>
|
|||
|
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 = "ָ<EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD>ڱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n";
|
|||
|
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
// ʹ<><CAB9><EFBFBD><EFBFBD>ĸ<EFBFBD>кŴ<D0BA>Excel<65><6C><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ȡһ<C8A1>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>뽫<EFBFBD><EBBDAB>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD>Ϊ<EFBFBD>кŵĻ<C5B5><C4BB><EFBFBD>Auto<74><6F>Ϊfalse
|
|||
|
bool CSpreadSheet::ReadColumn(CStringArray &ColumnValues, CString column, bool Auto)
|
|||
|
{
|
|||
|
short columnIndex = CalculateColumnNumber(column, Auto);
|
|||
|
if (0 == columnIndex)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
if (ReadColumn(ColumnValues, columnIndex))
|
|||
|
{
|
|||
|
return true;
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
// ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>кŴӱ<C5B4><D3B1><EFBFBD><EFBFBD>ж<EFBFBD>ȡһ<C8A1><D2BB>
|
|||
|
bool CSpreadSheet::ReadColumn(CStringArray &ColumnValues, short column)
|
|||
|
{
|
|||
|
if (0 == column)
|
|||
|
{
|
|||
|
m_sLastError = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ0\n";
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
int tempRow = m_dCurrentRow;
|
|||
|
m_dCurrentRow = 1;
|
|||
|
ColumnValues.RemoveAll();
|
|||
|
for (int i = 1; i <= m_aRows.GetSize(); i++)
|
|||
|
{
|
|||
|
// <20><>ȡÿһ<C3BF><D2BB>
|
|||
|
if (ReadRow(m_atempArray, i))
|
|||
|
{
|
|||
|
// <20><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>л<EFBFBD>ȡ<EFBFBD><C8A1>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (column <= m_atempArray.GetSize())
|
|||
|
{
|
|||
|
ColumnValues.Add(m_atempArray.GetAt(column-1));
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
ColumnValues.Add("");
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
m_dCurrentRow = tempRow;
|
|||
|
m_sLastError = "<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n";
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
m_dCurrentRow = tempRow;
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
// <20><>Excel<65><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><>ͷ)<29><><EFBFBD><EFBFBD>ĸ<EFBFBD>кŶ<D0BA>ȡ<EFBFBD><C8A1>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>Ĭ<EFBFBD><C4AC>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ж<EFBFBD>ȡ<EFBFBD><C8A1>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>뽫<EFBFBD><EBBDAB>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>ͷ<EFBFBD><CDB7><EFBFBD>Ļ<EFBFBD><C4BB>ɽ<EFBFBD>Auto<74><6F>Ϊfalse
|
|||
|
bool CSpreadSheet::ReadCell (CString &CellValue, CString column, long row, bool Auto)
|
|||
|
{
|
|||
|
short columnIndex = CalculateColumnNumber(column, Auto);
|
|||
|
if (0 == columnIndex)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
if (ReadCell(CellValue, columnIndex, row))
|
|||
|
{
|
|||
|
return true;
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
// <20>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD>С<EFBFBD><D0A1>кŶ<D0BA>ȡ<EFBFBD><C8A1>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>Ĭ<EFBFBD><C4AC>(row<6F><77>Ч)Ϊ<><CEAA>һ<EFBFBD><D2BB><EFBFBD>ж<EFBFBD>ȡ<EFBFBD><C8A1>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
|
|||
|
bool CSpreadSheet::ReadCell (CString &CellValue, short column, long row)
|
|||
|
{
|
|||
|
if (0 == column)
|
|||
|
{
|
|||
|
m_sLastError = "<EFBFBD>в<EFBFBD><EFBFBD><EFBFBD>Ϊ0\n";
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
int tempRow = m_dCurrentRow;
|
|||
|
if (row)
|
|||
|
{
|
|||
|
m_dCurrentRow = row;
|
|||
|
}
|
|||
|
if (ReadRow(m_atempArray, m_dCurrentRow))
|
|||
|
{
|
|||
|
// <20><>ȡָ<C8A1><D6B8><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
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 = "<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n";
|
|||
|
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ
|
|||
|
void CSpreadSheet::BeginTransaction()
|
|||
|
{
|
|||
|
m_bTransaction = true;
|
|||
|
}
|
|||
|
|
|||
|
// <20>ύ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>ļ<EFBFBD><C4BC>У<EFBFBD><D0A3>ڴ˴<DAB4><CBB4><EFBFBD>Excel<65>б<EFBFBD>
|
|||
|
bool CSpreadSheet::Commit()
|
|||
|
{
|
|||
|
if (m_bExcel) // <20><><EFBFBD><EFBFBD>Excel<65><6C><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>ʱ
|
|||
|
{
|
|||
|
// <20><F2BFAABB>Excel<65>ļ<EFBFBD>
|
|||
|
m_Database->OpenEx(m_sDsn, CDatabase::noOdbcDialog);
|
|||
|
|
|||
|
if (m_bAppend)
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><D1BE><EFBFBD><EFBFBD>ڱ<EFBFBD><DAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD>
|
|||
|
m_stempString= "[" + m_sSheetName + "$A1:IV65536]";
|
|||
|
m_stempSql.Format ("DROP TABLE %s", m_stempString);
|
|||
|
try
|
|||
|
{
|
|||
|
m_Database->ExecuteSQL(m_stempSql);
|
|||
|
}
|
|||
|
catch(CDBException *pEx)
|
|||
|
{
|
|||
|
m_sLastError = pEx->m_strError;
|
|||
|
m_Database->Close();
|
|||
|
pEx->Delete();
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>µı<C2B5><C4B1><EFBFBD>
|
|||
|
m_stempSql.Format("CREATE TABLE [%s$A1:IV65536] (", m_sSheetName);
|
|||
|
for (int j = 0; j < m_aFieldNames.GetSize(); j++)
|
|||
|
{
|
|||
|
m_stempSql = m_stempSql + "[" + m_aFieldNames.GetAt(j) +"]" + " char(255), ";
|
|||
|
}
|
|||
|
m_stempSql.Delete(m_stempSql.GetLength()-2, 2);
|
|||
|
m_stempSql += ")";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>µı<C2B5><C4B1><EFBFBD>
|
|||
|
m_stempSql.Format("CREATE TABLE [%s] (", m_sSheetName);
|
|||
|
for (int i = 0; i < m_aFieldNames.GetSize(); i++)
|
|||
|
{
|
|||
|
m_stempSql = m_stempSql + "[" + m_aFieldNames.GetAt(i) +"]" + " char(255), ";
|
|||
|
}
|
|||
|
m_stempSql.Delete(m_stempSql.GetLength()-2, 2);
|
|||
|
m_stempSql += ")";
|
|||
|
}
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
m_Database->ExecuteSQL(m_stempSql);
|
|||
|
if (!m_bAppend)
|
|||
|
{
|
|||
|
m_dTotalColumns = m_aFieldNames.GetSize();
|
|||
|
m_bAppend = true;
|
|||
|
}
|
|||
|
}
|
|||
|
catch(CDBException *pEx)
|
|||
|
{
|
|||
|
m_sLastError = pEx->m_strError;
|
|||
|
m_Database->Close();
|
|||
|
pEx->Delete();
|
|||
|
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>Ѹı<D1B8><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
for (int k = 1; k < m_dTotalRows; k++)
|
|||
|
{
|
|||
|
ReadRow(m_atempArray, k+1);
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SQL<51><4C><EFBFBD><EFBFBD>
|
|||
|
m_stempSql.Format("INSERT INTO [%s$A1:IV%d] (", m_sSheetName, k);
|
|||
|
for (int i = 0; i < m_atempArray.GetSize(); i++)
|
|||
|
{
|
|||
|
m_stempString.Format("[%s], ", m_aFieldNames.GetAt(i));
|
|||
|
m_stempSql = m_stempSql + m_stempString;
|
|||
|
}
|
|||
|
m_stempSql.Delete(m_stempSql.GetLength()-2, 2);
|
|||
|
m_stempSql += ") VALUES (";
|
|||
|
for (int j = 0; j < m_atempArray.GetSize(); j++)
|
|||
|
{
|
|||
|
m_stempString.Format("'%s', ", m_atempArray.GetAt(j));
|
|||
|
m_stempSql = m_stempSql + m_stempString;
|
|||
|
}
|
|||
|
m_stempSql.Delete(m_stempSql.GetLength()-2, 2);
|
|||
|
m_stempSql += ")";
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
try
|
|||
|
{
|
|||
|
m_Database->ExecuteSQL(m_stempSql);
|
|||
|
}
|
|||
|
catch(CDBException *pEx)
|
|||
|
{
|
|||
|
m_sLastError = pEx->m_strError;
|
|||
|
m_Database->Close();
|
|||
|
pEx->Delete();
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
m_Database->Close();
|
|||
|
m_bTransaction = false;
|
|||
|
return true;
|
|||
|
}
|
|||
|
else // <20><><EFBFBD>ļ<EFBFBD><C4BC>Ƿָ<C7B7><D6B8><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>ļ<EFBFBD>ʱ
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
CFile *File = NULL;
|
|||
|
File = new CFile(m_sFile, CFile::modeCreate | CFile::modeWrite | CFile::shareDenyNone);
|
|||
|
if (File != NULL)
|
|||
|
{
|
|||
|
CArchive *Archive = NULL;
|
|||
|
Archive = new CArchive(File, CArchive::store);
|
|||
|
if (Archive != NULL)
|
|||
|
{
|
|||
|
for (int i = 0; i < m_aRows.GetSize(); i++)
|
|||
|
{
|
|||
|
Archive->WriteString(m_aRows.GetAt(i));
|
|||
|
Archive->WriteString("\r\n");
|
|||
|
}
|
|||
|
delete Archive;
|
|||
|
delete File;
|
|||
|
m_bTransaction = false;
|
|||
|
return true;
|
|||
|
}
|
|||
|
delete File;
|
|||
|
}
|
|||
|
}
|
|||
|
catch(...)
|
|||
|
{
|
|||
|
}
|
|||
|
m_sLastError = "д<EFBFBD>ļ<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n";
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// <20>ع<EFBFBD><D8B9>ļ<EFBFBD><C4BC>ı<EFBFBD>Ϊ<EFBFBD>ı<EFBFBD>ǰ״̬
|
|||
|
bool CSpreadSheet::RollBack()
|
|||
|
{
|
|||
|
if (Open())
|
|||
|
{
|
|||
|
m_bTransaction = false;
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
m_sLastError = "<EFBFBD>ڻع<EFBFBD><EFBFBD><EFBFBD>ǰ״̬ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n";
|
|||
|
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
// ת<><D7AA>Ϊָ<CEAA><D6B8><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
|
|||
|
bool CSpreadSheet::Convert(CString SheetOrSeparator)
|
|||
|
{
|
|||
|
// <20>ļ<EFBFBD><C4BC><EFBFBD>
|
|||
|
m_stempString = m_sFile;
|
|||
|
m_stempString.Delete(m_stempString.GetLength()-4, 4);
|
|||
|
if (m_bExcel) // <20><><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>Excel<65>ļ<EFBFBD>ʱ
|
|||
|
{
|
|||
|
m_stempString += ".csv";
|
|||
|
CSpreadSheet tempSheet(m_stempString, SheetOrSeparator, false);
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>ı<EFBFBD><C4B1>ļ<EFBFBD><C4BC>Ѵ<EFBFBD><D1B4>ڣ<EFBFBD><DAA3>治ת<E6B2BB><D7AA>ֱ<EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD>
|
|||
|
if (0 != tempSheet.GetTotalColumns())
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
tempSheet.BeginTransaction();
|
|||
|
|
|||
|
for (int i = 1; i <= m_dTotalRows; i++)
|
|||
|
{
|
|||
|
if (!ReadRow(m_atempArray, i))
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
if (!tempSheet.AddRow(m_atempArray, i))
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
if (!tempSheet.Commit())
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
else // <20><><EFBFBD>ļ<EFBFBD><C4BC>Ƿָ<C7B7><D6B8><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>ļ<EFBFBD>ʱ
|
|||
|
{
|
|||
|
m_stempString += ".xls";
|
|||
|
CSpreadSheet tempSheet(m_stempString, SheetOrSeparator, false);
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>Excel<65>ļ<EFBFBD><C4BC>Ѵ<EFBFBD><D1B4>ڣ<EFBFBD><DAA3><EFBFBD>ֱ<EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD>
|
|||
|
if (0 != tempSheet.GetTotalColumns())
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
GetFieldNames(m_atempArray);
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><>ͷ)<29>ֶ<EFBFBD><D6B6>Ƿ<EFBFBD><C7B7>ظ<EFBFBD>
|
|||
|
bool duplicate = false;
|
|||
|
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("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><>ͷ)<29>ֶ<EFBFBD><D6B6>ظ<EFBFBD>:%s\n", m_atempArray.GetAt(i));
|
|||
|
duplicate = true;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (duplicate) // <20>ظ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>(<28><>ͷ)
|
|||
|
{
|
|||
|
m_atempArray.RemoveAll();
|
|||
|
for (int k = 1; k <= m_dTotalColumns; k++)
|
|||
|
{
|
|||
|
m_stempString.Format("%d", k);
|
|||
|
m_atempArray.Add(m_stempString);
|
|||
|
}
|
|||
|
|
|||
|
if (!tempSheet.AddHeaders(m_atempArray))
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
for (int l = 1; l <= m_dTotalRows; l++)
|
|||
|
{
|
|||
|
if (!ReadRow(m_atempArray, l))
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
if (!tempSheet.AddRow(m_atempArray, l+1))
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
if (!tempSheet.AddHeaders(m_atempArray))
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
for (int l = 2; l <= m_dTotalRows; l++)
|
|||
|
{
|
|||
|
if (!ReadRow(m_atempArray, l))
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
if (!tempSheet.AddRow(m_atempArray, l))
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// Ϊ<><CEAA><EFBFBD>ж<EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD>Excel<65><6C><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8>ı<EFBFBD><C4B1>ļ<EFBFBD>
|
|||
|
bool CSpreadSheet::Open()
|
|||
|
{
|
|||
|
if (m_bExcel) // <20><><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>Excel<65><6C><EFBFBD>ӱ<EFBFBD><D3B1><EFBFBD><EFBFBD>ļ<EFBFBD>ʱ
|
|||
|
{
|
|||
|
// <20><F2BFAABB>Excel<65>ļ<EFBFBD>
|
|||
|
m_Database->OpenEx(m_sDsn, CDatabase::noOdbcDialog);
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѯ<EFBFBD><D1AF>
|
|||
|
m_rSheet = new CRecordset( m_Database );
|
|||
|
m_sSql.Format("SELECT * FROM [%s$A1:IV65536]", m_sSheetName);
|
|||
|
try
|
|||
|
{
|
|||
|
// <20><>ѯ<EFBFBD><D1AF>
|
|||
|
m_rSheet->Open(CRecordset::forwardOnly, m_sSql, CRecordset::readOnly);
|
|||
|
}
|
|||
|
catch(...)
|
|||
|
{
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
delete m_rSheet;
|
|||
|
m_rSheet = NULL;
|
|||
|
m_Database->Close();
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
// <20>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
m_dTotalColumns = m_rSheet->m_nResultCols;
|
|||
|
|
|||
|
if (m_dTotalColumns != 0)
|
|||
|
{
|
|||
|
m_aRows.RemoveAll();
|
|||
|
m_stempString.Empty();
|
|||
|
m_bAppend = true;
|
|||
|
m_dTotalRows++; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>(<28><>ͷ)<29>ֶ<EFBFBD><D6B6><EFBFBD>
|
|||
|
for (int i = 0; i < m_dTotalColumns; i++)
|
|||
|
{
|
|||
|
m_stempSql = m_rSheet->m_rgODBCFieldInfos[i].m_strName;
|
|||
|
m_aFieldNames.Add(m_stempSql);
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>кϲ<D0BA><CFB2><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (i != m_dTotalColumns-1) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
|||
|
{
|
|||
|
m_stempString = m_stempString + "\"" + m_stempSql + "\"" + m_sSeparator;
|
|||
|
}
|
|||
|
else // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
|||
|
{
|
|||
|
m_stempString = m_stempString + "\"" + m_stempSql + "\"";
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// <20>洢<EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><>ͷ)
|
|||
|
m_aRows.Add(m_stempString);
|
|||
|
|
|||
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>洢<EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
while (!m_rSheet->IsEOF())
|
|||
|
{
|
|||
|
m_dTotalRows++; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
// <20><>ȡһ<C8A1><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
m_stempString.Empty();
|
|||
|
for (short column = 0; column < m_dTotalColumns; column++)
|
|||
|
{
|
|||
|
m_rSheet->GetFieldValue(column, m_stempSql);
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>кϲ<D0BA><CFB2><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (column != m_dTotalColumns-1) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ
|
|||
|
{
|
|||
|
m_stempString = m_stempString + "\"" + m_stempSql + "\"" + m_sSeparator;
|
|||
|
}
|
|||
|
else // <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ʱ
|
|||
|
{
|
|||
|
m_stempString = m_stempString + "\"" + m_stempSql + "\"";
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// <20>洢<EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD>Ѱ<EFBFBD><D1B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
m_aRows.Add(m_stempString);
|
|||
|
m_rSheet->MoveNext();
|
|||
|
}
|
|||
|
catch (...)
|
|||
|
{
|
|||
|
m_sLastError = "<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n";
|
|||
|
delete m_rSheet;
|
|||
|
m_rSheet = NULL;
|
|||
|
m_Database->Close();
|
|||
|
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
m_rSheet->Close();
|
|||
|
delete m_rSheet;
|
|||
|
m_rSheet = NULL;
|
|||
|
m_Database->Close();
|
|||
|
m_dCurrentRow = 1;
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
else // <20><><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>ļ<EFBFBD>ʱ
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
CFile *File = NULL;
|
|||
|
File = new CFile(m_sFile, CFile::modeRead | CFile::shareDenyNone);
|
|||
|
if (File != NULL)
|
|||
|
{
|
|||
|
CArchive *Archive = NULL;
|
|||
|
Archive = new CArchive(File, CArchive::load);
|
|||
|
if (Archive != NULL)
|
|||
|
{
|
|||
|
m_aRows.RemoveAll();
|
|||
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD>洢<EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
while(Archive->ReadString(m_stempString))
|
|||
|
{
|
|||
|
m_aRows.Add(m_stempString);
|
|||
|
}
|
|||
|
ReadRow(m_aFieldNames, 1); // <20>õ<EFBFBD><C3B5><EFBFBD><EFBFBD>е<EFBFBD><D0B5>ֶ<EFBFBD><D6B6><EFBFBD>(<28><>ͷ)
|
|||
|
delete Archive;
|
|||
|
delete File;
|
|||
|
|
|||
|
// <20>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
m_dTotalRows = m_aRows.GetSize();
|
|||
|
|
|||
|
// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
for (int i = 0; i < m_aRows.GetSize(); i++)
|
|||
|
{
|
|||
|
ReadRow(m_atempArray, i);
|
|||
|
if (m_atempArray.GetSize() > m_dTotalColumns)
|
|||
|
{
|
|||
|
m_dTotalColumns = m_atempArray.GetSize();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (m_dTotalColumns != 0)
|
|||
|
{
|
|||
|
m_bAppend = true;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
delete File;
|
|||
|
}
|
|||
|
}
|
|||
|
catch(...)
|
|||
|
{
|
|||
|
}
|
|||
|
|
|||
|
m_sLastError = "<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n";
|
|||
|
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// ת<><D7AA>Excle<6C><65><EFBFBD><EFBFBD>ĸ<EFBFBD>к<EFBFBD>Ϊ<EFBFBD><CEAA>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>к<EFBFBD>
|
|||
|
short CSpreadSheet::CalculateColumnNumber(CString column, bool Auto)
|
|||
|
{
|
|||
|
if (Auto)
|
|||
|
{
|
|||
|
column.MakeUpper();
|
|||
|
|
|||
|
int firstLetter, secondLetter;
|
|||
|
if (1 == column.GetLength())
|
|||
|
{
|
|||
|
firstLetter = column.GetAt(0);
|
|||
|
return (firstLetter - 65 + 1); // ASCII<49><49><EFBFBD>д<EFBFBD>д<EFBFBD><D0B4>ĸAΪ65
|
|||
|
}
|
|||
|
else if (2 == column.GetLength())
|
|||
|
{
|
|||
|
firstLetter = column.GetAt(0);
|
|||
|
secondLetter = column.GetAt(1);
|
|||
|
return ((firstLetter - 65 + 1)*26 + (secondLetter - 65 + 1)); // ASCII<49><49><EFBFBD>д<EFBFBD>д<EFBFBD><D0B4>ĸAΪ65
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7><EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Чֱ<D0A7>ӷ<EFBFBD><D3B7><EFBFBD>
|
|||
|
for (int i = 0; i < m_aFieldNames.GetSize(); i++)
|
|||
|
{
|
|||
|
if (!column.Compare(m_aFieldNames.GetAt(i)))
|
|||
|
{
|
|||
|
return (i + 1);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
m_sLastError = "<EFBFBD><EFBFBD>Ч<EFBFBD>ֶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD>к<EFBFBD>\n";
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
// <20><>ȡExcel-ODBC<42><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
void CSpreadSheet::GetExcelDriver()
|
|||
|
{
|
|||
|
char szBuf[2001];
|
|||
|
WORD cbBufMax = 2000;
|
|||
|
WORD cbBufOut;
|
|||
|
char *pszBuf = szBuf;
|
|||
|
|
|||
|
// <20><>ȡ<EFBFBD>Ѱ<EFBFBD>װ<EFBFBD><D7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (<28><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>ͷ<EFBFBD>ļ<EFBFBD>"odbcinst.h")
|
|||
|
if(!SQLGetInstalledDrivers(szBuf,cbBufMax,& cbBufOut))
|
|||
|
{
|
|||
|
m_sExcelDriver = "";
|
|||
|
}
|
|||
|
|
|||
|
// Ѱ<><D1B0>Excel<65><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>...
|
|||
|
do
|
|||
|
{
|
|||
|
if( NULL != strstr( pszBuf, "Excel" ) )
|
|||
|
{
|
|||
|
// <20>ҵ<EFBFBD>!
|
|||
|
m_sExcelDriver = CString( pszBuf );
|
|||
|
break;
|
|||
|
}
|
|||
|
pszBuf = strchr( pszBuf, '\0' ) + 1;
|
|||
|
}
|
|||
|
while( '\0' != pszBuf[1] );
|
|||
|
}
|
|||
|
|
|||
|
#endif
|