當前位置:首頁 » 操作系統 » mfc資料庫excel

mfc資料庫excel

發布時間: 2022-05-15 07:58:45

① 如何將mfc中的數據導入到Excel中

  • 1、首先,打開媒介工具「記事本」,將word文件里需要導入的數據,復制粘貼到記事本當中,然後保存成為txt文件,本例中將txt文件取名為「數據源.txt」。

  • 6

    6、列數據格式選擇」常規「,」常規「選項更為智能。

    完成設定後,點擊完成。

② MFC中關於保存數據到Excel

MFC訪問EXCEL,那可是比較麻煩的了。給個以前做過的項目。是訪問資料庫的 你自己整理著看下 希望有幫助。
if(m_bDataBase) //有資料庫
{
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///創建Connection對象
if(SUCCEEDED(hr))
{
m_pConnection->ConnectionTimeout=3;///設置超時時間為3秒
CString szOpen = theApp.m_szDataMisDir;
if(szOpen.Right(1) != "\\")
szOpen += "\\";
//hr = m_pConnection->Open(Filepath,"","",adModeUnknown);
#ifndef _OFFICE97
szOpen = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+szOpen+"BiocaDatabase.mdb;";
#else
szOpen = "Provider=Microsoft.Jet.OLEDB.3.51;Data Source="+szOpen+"BiocaDatabase.mdb;";
#endif

hr = m_pConnection->Open((_bstr_t)szOpen,"","",adModeUnknown);
}
}
catch(_com_error e)//捕捉異常
{
CString temp;
if(m_bChinese) temp.Format("連接資料庫錯誤信息:%s",e.ErrorMessage());
else temp.Format("Connecting database failure:%s",e.ErrorMessage());
AfxMessageBox(temp);
m_pConnection = NULL;
}

//如果當前資料庫為空,則導入最近使用的資料庫
if(m_pConnection!=NULL)
{
CString szsql = "SELECT * FROM TestData";
_RecordsetPtr pSearchRecordSet;
try
{
pSearchRecordSet.CreateInstance("ADODB.Recordset");
pSearchRecordSet->Open((_variant_t)szSQL,_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
if(pSearchRecordSet->adoEOF)
{

if(!CopyFile(m_szDesFileName1,m_szDataMisDir+"\\BiocaDatabase.mdb",FALSE))
{
//判斷是否是裝完軟體後第一次運行
int RunNo=GetProfileInt("RunNo","No",0); //第一次運行
if(RunNo==0) WriteProfileInt("RunNo","No",1); //已經運行過了
else
{
if(m_bChinese) AfxMessageBox("導入資料庫失敗!");
else AfxMessageBox("Input database failure!");
}
}

}
}
catch(_com_error e)///捕捉異常
{
CString temp;
if(m_bChinese) temp.Format("導入資料庫出錯:%s",e.ErrorMessage());
else temp.Format("Input database failure:%s",e.ErrorMessage());
AfxMessageBox(temp);
}
} //if(!m_pConnection=NULL)

} //if(m_bDataBase)

③ MFC怎麼讀寫EXCEL文件

可以用多種方法,比如說用ODBC對excel資料庫進行操作,或者說用excel 的COM介面進行編程。我找到一個例子關鍵代碼如下:
//創建並寫入Excel文件
void CRWExcel::WriteToExcel()
{
CDatabase database;
CString sDriver = "MICROSOFT EXCEL DRIVER (*.XLS)"; // Excel安裝驅動
CString sExcelFile = "c:\\demo.xls"; // 要建立的Excel文件
CString sSql;

TRY
{
// 創建進行存取的字元串
sSql.Format("DRIVER={%s};DSN='''';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s",
sDriver, sExcelFile, sExcelFile);

// 創建資料庫 (既Excel表格文件)
if( database.OpenEx(sSql,CDatabase::noOdbcDialog) )
{
// 創建表結構(姓名、年齡)
sSql = "CREATE TABLE demo (Name TEXT,Age NUMBER)";
database.ExecuteSQL(sSql);

// 插入數值
sSql = "INSERT INTO demo (Name,Age) VALUES (''徐景周'',26)";
database.ExecuteSQL(sSql);

sSql = "INSERT INTO demo (Name,Age) VALUES (''徐志慧'',22)";
database.ExecuteSQL(sSql);

sSql = "INSERT INTO demo (Name,Age) VALUES (''郭徽'',27)";
database.ExecuteSQL(sSql);
}

// 關閉資料庫
database.Close();
}
CATCH_ALL(e)
{
TRACE1("Excel驅動沒有安裝: %s",sDriver);
}
END_CATCH_ALL;
}

// 讀取Excel文件
void CRWExcel::ReadFromExcel()
{
CDatabase database;
CString sSql;
CString sItem1, sItem2;
CString sDriver;
CString sDsn;
CString sFile = "Demo.xls"; // 將被讀取的Excel文件名

// 檢索是否安裝有Excel驅動 "Microsoft Excel Driver (*.xls)"
sDriver = GetExcelDriver();
if (sDriver.IsEmpty())
{
// 沒有發現Excel驅動
AfxMessageBox("沒有安裝Excel驅動!");
return;
}

// 創建進行存取的字元串
sDsn.Format("ODBC;DRIVER={%s};DSN='''';DBQ=%s", sDriver, sFile);

TRY
{
// 打開資料庫(既Excel文件)
database.Open(NULL, false, false, sDsn);

CRecordset recset(&database);

// 設置讀取的查詢語句.
sSql = "SELECT Name, Age "
"FROM demo "
"ORDER BY Name ";

// 執行查詢語句
recset.Open(CRecordset::forwardOnly, sSql, CRecordset::readOnly);

// 獲取查詢結果
while (!recset.IsEOF())
{
//讀取Excel內部數值
recset.GetFieldValue("Name ", sItem1);
recset.GetFieldValue("Age", sItem2);

// 移到下一行
recset.MoveNext();
}

// 關閉資料庫
database.Close();

}
CATCH(CDBException, e)
{
// 資料庫操作產生異常時...
AfxMessageBox("資料庫錯誤: " + e->m_strError);
}
END_CATCH;

④ 如何用MFC打開excel文件,實現多excel文件合並、信息匯總分類、並能夠按照要求進行數據篩選導出結果並生成

某些國家少年兒童在校外進行集體活動的場所

⑤ (高分)在MFC工程中用ADO方法連接Access資料庫,如何實現將查詢的記錄集導入Excel文件中

給你一段代碼,我一個程序中用到的通過ADO將記錄寫入EXCEL並保存。
你自己挑挑裡面有用的部分。應該能看懂吧。
UpdateData(TRUE);
SetDlgItemText(IDC_STATIC11, "條件選擇");
if(m_list2.GetCurSel()==-1||m_list4.GetCurSel()==-1||m_list5.GetCurSel()==-1)
{
AfxMessageBox("請將條件選擇完整!");
return;
}
if(m_check1)
{
if(m_list6.GetCurSel()==-1)
{
AfxMessageBox("請選擇B表中某列作為拷貝源!");
return;
}
}
_ConnectionPtr m_pConnection1 = NULL;
_ConnectionPtr m_pConnection2 = NULL;
_RecordsetPtr m_pRecordset = NULL;
// CString m_SinFile;
// CString m_TableName;
long lFiledCount1 = 0;
long lFiledCount2 = 0;
long lRowCount1 = 0; // 表的記錄數目,不包括表格的表頭行
long lRowCount2 = 0; // 表的記錄數目,不包括表格的表頭行
long lRowIndex2 = 0;

CoInitialize(NULL);

m_pConnection1.CreateInstance("ADODB.Connection");
m_pConnection2.CreateInstance("ADODB.Connection");
m_pRecordset.CreateInstance("ADODB.Connection");

CString strText1;
m_list1.GetLBText(m_list1.GetCurSel(),strText1);
CString SQLstr1;
SQLstr1.Format("SELECT * FROM [%s$]",strText1);

CString strText2;
m_list2.GetLBText(m_list2.GetCurSel(),strText2);
CString SQLstr2;
SQLstr2.Format("SELECT * FROM [%s$]",strText2);

try
{

SetDlgItemText(IDC_STATIC12, "正在標記,耐心等候...");
//打開excel文件
m_pConnection1->Open((_bstr_t)path1,"","",adModeUnknown);
m_pConnection2->Open((_bstr_t)path2,"","",adModeUnknown);

_RecordsetPtr pRecordset1;
_RecordsetPtr pRecordset2;
pRecordset1.CreateInstance (__uuidof(Recordset));
pRecordset2.CreateInstance (__uuidof(Recordset));

try
{
// [Feature$]代表Excel表格的某個工作表的名稱
pRecordset1->Open(_variant_t(SQLstr1), // 查詢DemoTable表中所有欄位
m_pConnection1.GetInterfacePtr(), // 獲取庫接庫的IDispatch指針
adOpenStatic,//adOpenDynamic,adOpenKeyset
adLockOptimistic,
adCmdText);
pRecordset2->Open(_variant_t(SQLstr2), // 查詢DemoTable表中所有欄位
m_pConnection2.GetInterfacePtr(), // 獲取庫接庫的IDispatch指針
adOpenStatic,//adOpenDynamic,adOpenKeyset
adLockOptimistic,
adCmdText);
CString lb3; //A表某工作簿子集的全部子欄位標題
CString lb4; //B表某工作簿子集的全部子欄位標題
CString lb5;
CString lb6;
int p1=0; //A表需對比列的索引位置
int p2=0; //B表需對比列的索引位置
int p3=0;
m_list3.GetLBText(m_list3.GetCurSel(),lb3);
m_list4.GetLBText(m_list4.GetCurSel(),lb4);
m_list5.GetLBText(m_list5.GetCurSel(),lb5);
m_list6.GetLBText(m_list5.GetCurSel(),lb6);
//得到欄位數目
lFiledCount1 = pRecordset1->GetFields()->GetCount();
lFiledCount2 = pRecordset2->GetFields()->GetCount();
//得到記錄條數
lRowCount1 = pRecordset1->GetRecordCount();
lRowCount2 = pRecordset2->GetRecordCount();

//獲取欄位名
_bstr_t *filedName1 = new _bstr_t[lFiledCount1]; // 存儲欄位名
_bstr_t *filedName2 = new _bstr_t[lFiledCount2]; // 存儲欄位名
for (int filedIndex1 = 0;filedIndex1 < lFiledCount1;filedIndex1++)
{
filedName1[filedIndex1] = pRecordset1->GetFields()->GetItem(_variant_t(long(filedIndex1)))->GetName();
if(lb3==(LPCSTR)filedName1[filedIndex1])
{
p1=filedIndex1;
}
if(lb6==(LPCSTR)filedName1[filedIndex1])
{
p3=filedIndex1;
}
}

// FieldsPtr fds=pRecordset1->GetFields();
// fds->Append("S_id",adTinyInt,NULL,adFldRowID);

for (int filedIndex2 = 0;filedIndex2 < lFiledCount2;filedIndex2++)
{
filedName2[filedIndex2] = pRecordset2->GetFields()->GetItem(_variant_t(long(filedIndex2)))->GetName();
if(lb4==(LPCSTR)filedName2[filedIndex2])
{
p2=filedIndex2;
}
}

long lRowIndex1 = 0;
CString str1; // 存儲表格中的所有數據
CString str2; // 存儲表格中的所有數據

int flag=m_list5.FindString(-1,m_liststr5);

m_progress1.SetRange(0,lRowCount1);
pRecordset1->MoveFirst();
while(!pRecordset1->adoEOF)
{

// 按行,然後對每條數據的各個欄位進行存儲
str1 = VariantToString(pRecordset1->GetFields()->GetItem(_variant_t(filedName1[p1]))->Value);

pRecordset2->MoveFirst();
while(!pRecordset2->adoEOF)
{

// 按行,然後對每條數據的各個欄位進行存儲
str2 = VariantToString(pRecordset2->GetFields()->GetItem(_variant_t(filedName1[p2]))->Value);

if(str1==str2)
{
_variant_t t = _variant_t(long(flag));
pRecordset1->PutCollect(&t,_variant_t(m_eFlagStr));
lRowIndex2++;
pRecordset1->Update();
}

pRecordset2->MoveNext();
}
pRecordset1->MoveFirst();
lRowIndex1++;
m_progress1.SetPos(lRowIndex1);
pRecordset1->Move(lRowIndex1);

}

//pMyCom->Release();

pRecordset1->Close();
pRecordset1.Release();
pRecordset1 = NULL;
pRecordset2->Close();
pRecordset2.Release();
pRecordset2 = NULL;
m_pConnection1->Close();
m_pConnection2->Close();
if (filedName1 != NULL)
{
delete []filedName1;
filedName1 = NULL;
}
if (filedName2 != NULL)
{
delete []filedName2;
filedName2 = NULL;
}
}
catch(_com_error e)
{
EndWaitCursor();
AfxMessageBox(e.Description());
SetDlgItemText(IDC_STATIC12, "處理失敗!");
}
}
catch(_com_error e)
{
AfxMessageBox(e.Description());

EndWaitCursor();
return;
SetDlgItemText(IDC_STATIC12, "處理失敗!");
}
CString num;
num.Format("完成標記%d條記錄!",lRowIndex2);
SetDlgItemText(IDC_STATIC12, num);

⑥ MFC程序中怎麼把數據以Excel表格的形式導出

使用vc++MFC的ODBC類,將Excel文件當作一般的資料庫表格,利用ODBC技術像訪問資料庫一樣去讀/寫Excel文件。代碼在csdn上找,網上多的是!

⑦ MFC資料庫的問題,想通過一個Button,連接一個Excel資料庫,求大家知道

對啊,定義一個button的響應函數,之後直接把WriteToExcel() ;放在裡面就好了

⑧ VC++(MFC)如何從對話框寫數據到Excel

MFC訪問EXCEL,那可是比較麻煩的了。給個以前做過的項目。是訪問資料庫的 你自己整理著看下 希望有幫助。
if(m_bDataBase) //有資料庫
{
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///創建Connection對象
if(SUCCEEDED(hr))
{
m_pConnection->ConnectionTimeout=3;///設置超時時間為3秒
CString szOpen = theApp.m_szDataMisDir;
if(szOpen.Right(1) != "\\")
szOpen += "\\";
//hr = m_pConnection->Open(Filepath,"","",adModeUnknown);
#ifndef _OFFICE97
szOpen = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+szOpen+"BiocaDatabase.mdb;";
#else
szOpen = "Provider=Microsoft.Jet.OLEDB.3.51;Data Source="+szOpen+"BiocaDatabase.mdb;";
#endif

hr = m_pConnection->Open((_bstr_t)szOpen,"","",adModeUnknown);
}
}
catch(_com_error e)//捕捉異常
{
CString temp;
if(m_bChinese) temp.Format("連接資料庫錯誤信息:%s",e.ErrorMessage());
else temp.Format("Connecting database failure:%s",e.ErrorMessage());
AfxMessageBox(temp);
m_pConnection = NULL;
}

//如果當前資料庫為空,則導入最近使用的資料庫
if(m_pConnection!=NULL)
{
CString szSQL = "SELECT * FROM TestData";
_RecordsetPtr pSearchRecordSet;
try
{
pSearchRecordSet.CreateInstance("ADODB.Recordset");
pSearchRecordSet->Open((_variant_t)szSQL,_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
if(pSearchRecordSet->adoEOF)
{

if(!CopyFile(m_szDesFileName1,m_szDataMisDir+"\\BiocaDatabase.mdb",FALSE))
{
//判斷是否是裝完軟體後第一次運行
int RunNo=GetProfileInt("RunNo","No",0); //第一次運行
if(RunNo==0) WriteProfileInt("RunNo","No",1); //已經運行過了
else
{
if(m_bChinese) AfxMessageBox("導入資料庫失敗!");
else AfxMessageBox("Input database failure!");
}
}

}
}
catch(_com_error e)///捕捉異常
{
CString temp;
if(m_bChinese) temp.Format("導入資料庫出錯:%s",e.ErrorMessage());
else temp.Format("Input database failure:%s",e.ErrorMessage());
AfxMessageBox(temp);
}
} //if(!m_pConnection=NULL)

} //if(m_bDataBase)

⑨ 如何實現,在MFC中把sqlite資料庫查詢結果保存為excel或csv文件

這個實際上跟用什麼資料庫沒有關系,只要使用mfc提供的介面方法進行保存就行了。
你的資料庫只是用來保存數據的,把數據查詢到內存後,利用第三方庫EPPlus實現數據保存到excel

⑩ 用mfc怎麼打開excel表格

方法/步驟


  1. 打開需要插入對象文件的Excel表格,如下圖所示。

  2. 用滑鼠單擊要插入對象文件單元格,然後依次點擊"插入"菜單--"對象",窗口會自動彈出"對象"對話框,如下圖所示。

  3. 在"對象"對話框中選擇"由文件創建"選項卡,單擊"瀏覽",選擇需要插入的對象文件,此處我們先選擇一個Excel文件插入。

  4. 選擇"對象"對話框中的"顯示圖標",點擊"更改圖標",窗口會自動彈出"更改圖標"對話框。如下圖所示。

  5. 將"圖標標題"中的文件路徑及文件擴展名刪掉,只保留文件名稱,如下圖所示。然後點擊"確定"按鈕。

  6. 在"對象"對話框中點擊"確定"按鈕,Excel文件就插入完畢了。在閱讀時,只需雙擊這個Excel文件,就可以將之打開了。

熱點內容
linux圖形系統 發布:2024-10-10 18:45:40 瀏覽:802
農業溯源碼 發布:2024-10-10 18:44:53 瀏覽:841
rec320是哪個安卓版本 發布:2024-10-10 18:44:02 瀏覽:859
無腳本的藝人節目 發布:2024-10-10 18:26:55 瀏覽:396
安卓手機如何變成大屏幕 發布:2024-10-10 18:26:55 瀏覽:675
餐館許可證編號密碼是什麼 發布:2024-10-10 18:17:52 瀏覽:270
我的世界網易電腦版怎麼玩手機伺服器 發布:2024-10-10 18:06:16 瀏覽:29
客戶伺服器怎麼調查 發布:2024-10-10 17:56:05 瀏覽:36
軟體反編譯教程 發布:2024-10-10 17:50:14 瀏覽:13
uc瀏覽器android 發布:2024-10-10 17:50:13 瀏覽:17