VC 讀寫開啟Excel文件資料

2021-06-19 06:15:58 字數 2519 閱讀 5016

一:寫excel文件。

//

獲取程式所在目錄

getcurrentdirectory(max_path,g_cspathbuff);

//建立並寫入excel檔案

cdatabase database;

char *sdriver = "

microsoft excel driver (*.xls)

";//

excel安裝驅動

char

***celfile[max_path];

sprintf(***celfile,

"%s\\data.xls

",g_cspathbuff);//

要建立的excel檔案

char ssql[500

]; deletefile(***celfile);

try

;dsn='''';firstrowhasnames=1;readonly=false;create_db=\"%s\";dbq=%s",

sdriver, ***celfile, ***celfile);

//建立資料庫 (既excel**檔案)

database.openex(ssql,cdatabase::noodbcdialog);

//建立表結構(雷管數量、爆破座標、爆破日期、、、)

sprintf(ssql,

"create table 批次%d ( 雷管數量 text,爆破日期 text,經度 text,緯度 text,爆破人員編號 text,起爆器編號 text,資訊狀態 text,雷管編號 text,雷管id text,延遲時間 text)",

i);//執行sql語句

database.executesql(ssql);

sprintf(ssql,

"insert into 批次%d(雷管數量,爆破日期,經度,緯度,爆破人員編號,起爆器編號,資訊狀態) values(%s,%s,%s,%s,%s,%d,%s)",

i,g_dbdarray[i].ucdetonatorlength,

g_dbdarray[i].ucblastingdate,

csjingdu,

csweidu,

g_dbdarray[i].ucoperatorid,

g_noperatordev,

g_dbdarray[i].ucisread

);//執行sql語句

database.executesql(ssql);

}catch (cmemoryexception, e)

end_catch

二:讀excel文件。

1、首先要填如下支援類。

2、怎麼新增(即新增excel標頭檔案)

4、上二步做好後就可以寫開啟.xls文件的**了。

//

todo: add your control notification handler code here

判斷是否存在excel應用程式

workbooks objbooks;

_workbook objbook;

worksheets objsheets;

_worksheet objsheet;

range objrange,usedrange;

//單元格獲取

font font;

"");//

判斷是否存在excel應用程式

colevariant voptional((long

)disp_e_paramnotfound,vt_error);

objbooks.attachdispatch(pwkbooks,true);

objbook=objbooks.add(voptional);

objsheets=objbook.getsheets();

char

***celfile[max_path];

sprintf(***celfile,

"%s\\data.xls

",g_cspathbuff);//

要建立的excel檔案

objbooks.open(***celfile,voptional,voptional,voptional,voptional,voptional,voptional,

voptional,voptional,voptional,voptional,voptional,voptional,voptional,voptional);

5、效果如下。

分類:

0:vc++

VC讀寫Excel 速度問題

上篇文章 vc讀寫excel mfc讀寫excel 考資料 中的考勤資料處理軟體當excel行數多的時候效率特別地,跟蹤一下發現 時間主要花費在cspreadsheet類的addrow 方法中。通過odbc來平凡的寫excel資料的時候都會出現這樣的問題,資料行數少的時候還可以接受,當行數多 303...

VC讀寫EXCEL檔案類(已安裝excel)

以下是.h檔案 pragma once class creadwriteexcel 以下是.cpp檔案 include stdafx.h include readwriteexcel.h if failed hr cstring strname excel表完整路徑 cstring strtmp 臨...

讀寫Excel資料

獲取odbc中excel驅動 include include cstring cxxdlg getexceldriver pszbuf strchr pszbuf,0 1 while pszbuf 1 0 return sdriver 寫入excel void cxxdlg onwriteexcel...