修改CSpreadSheet commit函数,让其插入新行时不再重复删除 创建 写数据

来源:互联网 发布:淘宝代销不是长久之计 编辑:程序博客网 时间:2024/04/28 16:04
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
// {
// m_Database->ExecuteSQL(m_stempSql);
// }
// catch(CDBException *e)
// {
// m_sLastError = e->m_strError;
// m_Database->Close();
// return false;
// }
//
// // Create new sheet
// 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 += ")";


// try
// {
// m_Database->ExecuteSQL(m_stempSql);
// if (!m_bAppend)
// {
// m_dTotalColumns = m_aFieldNames.GetSize();
// m_bAppend = true;
// }
// }
// catch(CDBException *e)
// {
// m_sLastError = e->m_strError;
// m_Database->Close();
// return false;
// }
}
else
{
// Create new sheet
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 *e)
{
m_sLastError = e->m_strError;
m_Database->Close();
return false;
}
}


 


// Save changed data
// for (int k = 1; k < m_dTotalRows; k++)
//   {
  if (m_dTotalRows>1)
  {
 
   int k = m_dTotalRows-1;
ReadRow(m_atempArray, k+1);


// Create Insert SQL
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 += ")";


// Add row
try
{
m_Database->ExecuteSQL(m_stempSql);
}
catch(CDBException *e)
{
m_sLastError = e->m_strError;
m_Database->Close();
return false;
}
        }
// }
m_Database->Close();
m_bTransaction = false;
return true;
}
else // if file is a text delimited file
{
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 = "Error writing file\n";
return false;
}

}




程序中调用

                CString file1;
file1.Format("%s.xls",config.ZhiLing);
CSpreadSheet SS(file1, "Record");
CStringArray sampleArray;
CString tempString;
SS.BeginTransaction();
sampleArray.RemoveAll();
sampleArray.Add(list1);
sampleArray.Add(CS_SM);
sampleArray.Add(config.ZhiLing);
sampleArray.Add(CS_GD);
sampleArray.Add(list4);
sampleArray.Add(list5);
sampleArray.Add(list6);
sampleArray.Add(list7);
sampleArray.Add(strAbsTime1);
sampleArray.Add(strAbsTime1_old);
sampleArray.Add(TemStr);
SS.AddRow(sampleArray);
SS.Commit();


如果程序中只有行插入操作,就没有问题,因为本人用到的就只有行插入

阅读全文
0 0
原创粉丝点击