当前位置:首页 » 操作系统 » 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文件,就可以将之打开了。

热点内容
安卓游戏机在哪里下载 发布:2024-10-10 21:23:48 浏览:461
网上买了服务器怎么在电脑连接失败怎么办 发布:2024-10-10 21:18:35 浏览:962
安卓息屏壁纸在哪个文件夹 发布:2024-10-10 21:11:58 浏览:407
eer数据库 发布:2024-10-10 21:06:08 浏览:709
ftp匿名用户上传 发布:2024-10-10 21:06:06 浏览:754
凯越原始密码多少 发布:2024-10-10 21:06:04 浏览:193
捏脆泥解压 发布:2024-10-10 20:56:25 浏览:705
开浏览器显示脚本错误信息 发布:2024-10-10 20:48:14 浏览:11
wii安卓模拟器怎么用 发布:2024-10-10 20:47:35 浏览:501
制度宣传视频脚本 发布:2024-10-10 20:34:05 浏览:576