|
針對(duì)Excel表格文件操作的編程實(shí)現(xiàn) 下載本文示例源代碼 具體實(shí)現(xiàn) #include "CSpreadSheet.h" // 新建Excel文件名及路徑,TestSheet為內(nèi)部表名
CSpreadSheet SS("c:\\Test.xls", "TestSheet");
CStringArray sampleArray, testRow;
SS.BeginTransaction();
// 加入標(biāo)題
sampleArray.RemoveAll();
sampleArray.Add("姓名");
sampleArray.Add("年齡");
SS.AddHeaders(sampleArray);
// 加入數(shù)據(jù)
CString strName[] = {"徐景周","徐志慧","郭徽","牛英俊","朱小鵬"};
CString strAge[] = {"27","23","28","27","26"};
for(int i = 0; i < sizeof(strName)/sizeof(CString); i++)
{
sampleArray.RemoveAll();
sampleArray.Add(strName[i]);
sampleArray.Add(strAge[i]);
SS.AddRow(sampleArray);
}
SS.Commit();
CSpreadSheet SS("c:\\Test.xls", "TestSheet");
CStringArray Rows, Column;
//清空列表框
m_AccessList.ResetContent();
for (int i = 1; i <= SS.GetTotalRows(); i++)
{
// 讀取一行
SS.ReadRow(Rows, i);
CString strContents = "";
for (int j = 1; j <= Rows.GetSize(); j++)
{
if(j == 1)
strContents = Rows.GetAt(j-1);
else
strContents = strContents + " --> " + Rows.GetAt(j-1);
}
m_AccessList.AddString(strContents);
}
// 初始化測(cè)試行數(shù)據(jù),進(jìn)行添加、插入及替換數(shù)據(jù)操作演示
for (int k = 1; k <= 2; k++)
{
testRow.Add("Test");
}
SS.AddRow(testRow); // 添加到尾部
SS.AddRow(testRow, 2); // 插入新行到第二行
SS.AddRow(testRow, 6, true); // 替換原第四行來(lái)新的內(nèi)容
SS.AddCell("徐景周", 1,2); // 添加(不存在)或替換(存在)第二行,第一列單元格內(nèi)容
SS.Commit();
五、 對(duì)已存在Excel表格數(shù)據(jù)進(jìn)行行、列、單元格查詢
void CExcelAccessDlg::OnQuery()
{
CSpreadSheet SS("c:\\Test.xls", "TestSheet");
CStringArray Rows, Column;
CString tempString = "";
UpdateData();
if(m_strRow == "" && m_strColumn == "") // 查詢?yōu)榭?
{
AfxMessageBox("行號(hào)、列號(hào)不能同時(shí)為空!");
return;
}
else if(m_strRow == "" && m_strColumn != "") // 查詢指定列數(shù)據(jù)
{
int iColumn = atoi(m_strColumn);
int iCols = SS.GetTotalColumns();
if(iColumn > iCols) // 超出表范圍查詢時(shí)
{
CString str;
str.Format("表中總列數(shù)為: %d, ", iCols);
AfxMessageBox(str + " 查詢列數(shù)大于Excel表中總列數(shù),請(qǐng)重新輸入!");
return;
}
// 讀取一列數(shù)據(jù),并按行讀出
if(!SS.ReadColumn(Column, iColumn))
{
AfxMessageBox(SS.GetLastError());
return;
}
CString tmpStr;
for (int i = 0; i < Column.GetSize(); i++)
{
tmpStr.Format("行號(hào): %d, 列號(hào): %d ,內(nèi)容: %s\n", i+1,iColumn,Column.GetAt(i));
tempString += tmpStr;
}
AfxMessageBox(tempString);
}
else if(m_strRow != "" && m_strColumn == "") // 查詢指定行數(shù)數(shù)據(jù)
{
int iRow = atoi(m_strRow);
int iRows = SS.GetTotalRows();
if(iRow > iRows) // 超出表范圍查詢時(shí)
{
CString str;
str.Format("表中總行數(shù)為: %d, ", iRows);
AfxMessageBox(str + " 查詢行數(shù)大于Excel表中總行數(shù),請(qǐng)重新輸入!");
return;
}
// 讀取指定行數(shù)據(jù)
if(!SS.ReadRow(Rows, iRow))
{
AfxMessageBox(SS.GetLastError());
return;
}
CString tmpStr;
for (int i = 0; i < Rows.GetSize(); i++)
{
tmpStr.Format("行號(hào): %d, 列號(hào): %d ,內(nèi)容: %s\n", iRow, i+1, Rows.GetAt(i));
tempString += tmpStr;
}
AfxMessageBox(tempString);
}
else if(m_strRow != "" && m_strColumn != "") // 查詢指定單元格數(shù)據(jù)
{
int iRow = atoi(m_strRow), iColumn = atoi(m_strColumn);
int iRows = SS.GetTotalRows(), iCols = SS.GetTotalColumns();
if(iColumn > iCols) // 超出表范圍查詢時(shí)
{
CString str;
str.Format("表中總列數(shù)為: %d, ", iCols);
AfxMessageBox(str + " 查詢列數(shù)大于Excel表中總列數(shù),請(qǐng)重新輸入!");
return;
}
else if(iRow > iRows)
{
CString str;
str.Format("表中總行數(shù)為: %d, ", iRows);
AfxMessageBox(str + " 查詢行數(shù)大于Excel表中總行數(shù),請(qǐng)重新輸入!");
return;
}
// 讀取指定行、列單元格數(shù)據(jù)
if(!SS.ReadCell(tempString, iColumn, iRow))
{
AfxMessageBox(SS.GetLastError());
return;
}
CString str;
str.Format("行號(hào): %d, 列號(hào): %d ,內(nèi)容: %s", iRow,iColumn,tempString);
AfxMessageBox(str);
}
}
// 將原Excel文件轉(zhuǎn)換為用分號(hào)分隔的文本,并另存為同名文本文件
SS.Convert(";");
SS. DeleteSheet(); // 刪除Excel文件中所有表格
SS. DeleteSheet(" TestSheet "); // 刪除Excel中TextSheet表格
int iCols = SS.GetTotalColumns(); // 總列數(shù) int iRows = SS.GetTotalRows(); // 總行數(shù) int iCurRow = SS.GetCurrentRow(); // 當(dāng)前所在行號(hào) CStringArray rowHeader;
SS.GetFieldNames(rowHeader);
CString tmpStr;
for (int i = 0; i < rowHeader.GetSize(); i++)
{
tmpStr.Format("行號(hào): %d, 列號(hào): %d ,內(nèi)容: %s\n", 1, i+1, rowHeader.GetAt(i));
tempString += tmpStr;
}
AfxMessageBox(tempString);
最后,如果想知道詳細(xì)實(shí)現(xiàn)細(xì)節(jié)的話,可以在下載示例源碼后,仔細(xì)查看源碼既可(內(nèi)有詳細(xì)注釋)。 |
|
|