小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

針對(duì)Excel表格文件操作的編程實(shí)現(xiàn)...

 禁忌石 2010-11-07

針對(duì)Excel表格文件操作的編程實(shí)現(xiàn)
編譯:徐景周

下載本文示例源代碼

簡(jiǎn)介
通過(guò)本文及配套示例源碼你可以更加靈活的控制Excel表格文件,其中包括創(chuàng)建新Excel文件、寫入表格數(shù)據(jù)、讀取表格數(shù)據(jù)(包括對(duì)原建Excel文件自已手工添加的行、列數(shù)據(jù)的準(zhǔn)確讀取),刪除已有Excel表格,對(duì)表格中指定行、列、單元格進(jìn)行查詢、插入、替換等操作,同時(shí)還可以將生成的Excel文件轉(zhuǎn)換為按指定分隔符分隔的其它文本格式的文件。下面是把此方法用VC6編寫的示例程序運(yùn)行效果:



基本思路
基礎(chǔ)實(shí)現(xiàn)方法同上篇文章《直接通過(guò)ODBC讀、寫Excel表格文件》相同,都是通過(guò)ODBC來(lái)把Excel表格文件當(dāng)成數(shù)據(jù)庫(kù)文件來(lái)進(jìn)行讀、寫等操作,所以在Excel表格文件中寫入的行頭名必須是唯一的(不要重名,相當(dāng)于數(shù)據(jù)庫(kù)中的ID值)。本文中對(duì)Excel文件的操作都被封裝進(jìn)一個(gè)類CSpreadSheet中,通過(guò)它我們可以非常簡(jiǎn)便的實(shí)現(xiàn)各種Excel表格數(shù)據(jù)操作,并且可以對(duì)該類進(jìn)行擴(kuò)充來(lái)滿足自己的需求。

具體實(shí)現(xiàn)

一、 包含Excel文件操作類頭文件

#include "CSpreadSheet.h"
二、 新建Excel文件,并寫入默認(rèn)數(shù)據(jù)
// 新建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();

三、 讀取Excel文件數(shù)據(jù)
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);
}

四、 對(duì)已存在Excel表格數(shù)據(jù)進(jìn)行添加、插入、替換操作
// 初始化測(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)換另存為指定分隔的文本文件
// 將原Excel文件轉(zhuǎn)換為用分號(hào)分隔的文本,并另存為同名文本文件
SS.Convert(";"); 
七、 刪除Excel中表格
SS. DeleteSheet();            // 刪除Excel文件中所有表格
SS. DeleteSheet(" TestSheet ");  // 刪除Excel中TextSheet表格
八、 獲取Excel中總行數(shù)、總列數(shù)、當(dāng)前行
int iCols = SS.GetTotalColumns();   // 總列數(shù)
int iRows = SS.GetTotalRows();    // 總行數(shù)
int iCurRow = SS.GetCurrentRow(); // 當(dāng)前所在行號(hào)
九、 獲取行頭數(shù)據(jù)
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ì)注釋)。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多