10.ODBC创建/读取Excel QT4
看到一篇MFC的参考链接:https://blog.csdn.net/u012319493/article/details/50561046
改用QT的函数即可
创建Excel
//创建Excel
void CExcelDlg::OnOK()
{
// TODO: Add extra validation here //创建Excel文件
CDatabase DB; //Excel安装驱动
CString StrDriver = "MICROSOFT EXCEL DRIVER (*.XLS)"; //要建立的Execel文件
CString StrExcelFile = "f:\\Teachers.xls";
CString StrSQL;
StrSQL.Format("DRIVER={%s};DSN='';FIRSTROWHASNameS=1;READONLY=FALSE;CREATE_DB=%s;DBQ=%s",StrDriver,StrExcelFile,StrExcelFile);
TRY
{
//创建Excel表格文件
DB.OpenEx(StrSQL, CDatabase::noOdbcDialog); //创建表结构,字段名不能是Index
StrSQL = "CREATE TABLE Teachers(职工号 TEXT, 姓名 TEXT)";
DB.ExecuteSQL(StrSQL); //插入数值
StrSQL.Format("INSERT INTO Teachers (职工号, 姓名) VALUES ('%s', '%s')", "aa", "bb");
DB.ExecuteSQL(StrSQL); //关闭数据库
DB.Close();
}
CATCH(CDBException, e)
{
AfxMessageBox("创建错误:" + e->m_strError);
}
END_CATCH;
MessageBox("创建成功!"); }
读取Excel
//读取Excel
void CExcelDlg::OnBUTTONin()
{
// TODO: Add your control notification handler code here CDatabase DB;
CString StrSQL;
CString StrDsn; //创建ODBC数据源连接字符串
StrDsn.Format("ODBC;DRIVER={MICROSOFT EXCEL DRIVER (*.XLS)};DSN='';DBQ=f:\\teachers.xls");
TRY
{
//打开Excel文件
DB.Open(NULL, false, false, StrDsn);
CRecordset DBSet(&DB); //设置读取的查询语句
StrSQL = "SELECT * FROM Teachers"; //执行查询语句
DBSet.Open(CRecordset::forwardOnly, StrSQL, CRecordset::readOnly); //获取查询结果
CString StrInfo = "职工号, 姓名\n";
while(!DBSet.IsEOF())
{
//读取Excel内部数值
for(int i=0; i<DBSet.GetODBCFieldCount(); i++)
{
CString Str;
DBSet.GetFieldValue(i, Str);
StrInfo += Str + " ";
}
StrInfo += "\n";
DBSet.MoveNext();
}
MessageBox(StrInfo); //在信息框中显示
DB.Close();
}
CATCH(CDBException, e)
{
AfxMessageBox("数据库错误:" + e->m_strError);
}
END_CATCH;
}
将Excel中的内容导入到数据库
void CDataInputExput::OnButtonIn()
{
// TODO: Add your control notification handler code here CString FilePathName;
CString FileName;
CFileDialog dlg(TRUE, //TRUE为OPEN对话框,FALSE为SAVE AS对话框
NULL,
NULL,
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
(LPCTSTR)_TEXT("Excel Files (*.xls)|*.xls|*.xls|All Files (*.*)|*.*||"),
NULL);
if(dlg.DoModal()==IDOK)
{
FilePathName=dlg.GetPathName();
FileName = dlg.GetFileName();
}
else
{
return;
} ////////////////////////////////////////////////////////////////////////////////////
//读取Excel CDatabase DB;
CString StrSQL;
CString StrDsn;
int Which = FileName.ReverseFind('.');
CString table = FileName.Left(Which); CDataBase DB1; //自己定义的类
DB1.Connect();
CString cellid, traff, thtraff, rate, congsnum, callnum, callcongs, nTCH, DATE, TIME; //创建ODBC数据源连接字符串
int n = FilePathName.Replace("\\","\\\\");;
StrDsn.Format("ODBC;DRIVER={MICROSOFT EXCEL DRIVER (*.xls)};DSN='';DBQ=%s", FilePathName);
AfxMessageBox(StrDsn); TRY
{
//打开Excel文件
DB.Open(NULL, false, false, StrDsn);
CRecordset DBSet(&DB);
//设置读取的查询语句
StrSQL.Format("SELECT * FROM %s", table);
AfxMessageBox(StrSQL); //执行查询语句
DBSet.Open(CRecordset::forwardOnly, StrSQL, CRecordset::readOnly);
//获取查询结果 ;
while(!DBSet.IsEOF())
{
//读取Excel内部数值
DBSet.GetFieldValue((short)0, cellid); AfxMessageBox(cellid);
DBSet.GetFieldValue(1, traff);
DBSet.GetFieldValue(2, thtraff);
DBSet.GetFieldValue(3, rate);
DBSet.GetFieldValue(4, congsnum);
DBSet.GetFieldValue(5, callnum);
DBSet.GetFieldValue(6, callcongs);
DBSet.GetFieldValue(7, nTCH);
DBSet.GetFieldValue(8, DATE);
DBSet.GetFieldValue(9, TIME); //导入数据库
CString sql;
sql.Format("insert into data values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')",cellid, traff, thtraff, rate, congsnum, callnum, callcongs, nTCH, DATE, TIME);
AfxMessageBox(sql);
DB1.ExecuteSQL((_bstr_t)sql); DBSet.MoveNext();
}
AfxMessageBox("导入成功!");
DB.Close();
DB1.ExitConnect();
}
CATCH(CDBException, e)
{
AfxMessageBox("读取失败:" + e->m_strError);
}
END_CATCH; }
将数据库中的内容导出到Excel
void CDataInputExput::OnButtonOut()
{
// TODO: Add your control notification handler code here ////////////////////////////////////////////////////////////////////////////// //创建Excel文件
CDatabase DB; //Excel安装驱动
CString StrDriver = "MICROSOFT EXCEL DRIVER (*.XLS)"; //要建立的Execel文件
CString StrExcelFile = "f:\\dataOutput.xls";
CString StrSQL;
StrSQL.Format("DRIVER={%s};DSN='';FIRSTROWHASNameS=1;READONLY=FALSE;CREATE_DB=%s;DBQ=%s",StrDriver,StrExcelFile,StrExcelFile);
TRY
{
//创建Excel表格文件
DB.OpenEx(StrSQL, CDatabase::noOdbcDialog);
//创建表结构,字段名不能是Index
StrSQL = "CREATE TABLE dataOutput(CELLID TEXT, traff TEXT, thtraff TEXT, rate TEXT, congsnum TEXT, callnum TEXT, callcongs TEXT, nTCH TEXT, DATEs TEXT, TIMEs TEXT)";
DB.ExecuteSQL(StrSQL); CDataBase DB1; //自己定义的类
DB1.Connect();
CString sql = "select * from data where cellid = 3";
DB1.m_Recordset = DB1.GetRecordSet((_bstr_t)sql);
CString cellid, traff, thtraff, rate, congsnum, callnum, callcongs, nTCH, DATE, TIME; while(!DB1.m_Recordset->adoEOF)
{
//获取记录集中的数据
cellid = (char *)(_bstr_t)DB1.m_Recordset->GetCollect("cellid");
traff = (char *)(_bstr_t)DB1.m_Recordset->GetCollect("traff");
thtraff = (char *)(_bstr_t)DB1.m_Recordset->GetCollect("thtraff");
rate = (char *)(_bstr_t)DB1.m_Recordset->GetCollect("rate");
congsnum = (char *)(_bstr_t)DB1.m_Recordset->GetCollect("congsnum");
callnum = (char *)(_bstr_t)DB1.m_Recordset->GetCollect("callnum");
callcongs = (char *)(_bstr_t)DB1.m_Recordset->GetCollect("callcongs");
nTCH = (char *)(_bstr_t)DB1.m_Recordset->GetCollect("nTCH");
DATE = (char *)(_bstr_t)DB1.m_Recordset->GetCollect("DATE");
TIME = (char *)(_bstr_t)DB1.m_Recordset->GetCollect("TIME"); //向Excel插入数值
StrSQL.Format("INSERT INTO dataOutput values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')", cellid, traff, thtraff, rate, congsnum, callnum, callcongs, nTCH, DATE, TIME);
DB.ExecuteSQL(StrSQL); DB1.m_Recordset->MoveNext();
} MessageBox("导出成功!数据已导出到f:\\dataOutput.xls中");
//关闭数据库
DB.Close();
DB1.ExitConnect();
}
CATCH(CDBException, e)
{
AfxMessageBox("导出错误:" + e->m_strError);
}
END_CATCH;
}
10.ODBC创建/读取Excel QT4的更多相关文章
- c#读取excel到dataset
public DataSet TransExcelToDataSet(string fileName, List<string> sheetNames) { OleDbConnection ...
- Qt通过odbc读取excel数据
传统的读取方式是通过Excel.Application,这种方式不仅操作繁琐,而且速度也不快. 通过odbc读取,可以使用select语句直接读取整个工作表,处理excel数据就跟数据库一样方便. 当 ...
- python:创建文件夹:写入文本1:读取txt:读取Excel文件遍历文件夹:
https://blog.csdn.net/u011956147/article/details/80369731 创建文件夹: import osimport shutil def buildfil ...
- node.js、js读取excel、操作excel、创建excel之js-xlsx.js
node.js篇 第一步引入包 npm install xlsx -save 第二步使用 var xl =require('xlsx'); //workbook 对象,指的是整份 Excel 文档.我 ...
- C# 处理Excel公式(一)——创建、读取Excel公式
对于数据量较大的表格,需要计算一些特殊数值时,我们通过运用公式能有效提高我们数据处理的速度和效率,对于后期数据的增删改查等的批量操作也很方便.此外,对于某些数值的信息来源,我们也可以通过读取数据中包含 ...
- C# -- 使用Aspose.Cells创建和读取Excel文件
使用Aspose.Cells创建和读取Excel文件 1. 创建Excel Aspose.Cells.License li = new Aspose.Cells.License(); li.SetLi ...
- 使用Apache下poi创建和读取excel文件
一:使用apache下poi创建excel文档 @Test /* * 使用Apache poi创建excel文件 */ public void testCreateExcel() { // 1:创建一 ...
- java使用poi.3.10读取excel 2003 (xls格式)
最近在做一个Excel导入数据库的案例,整理文档出来供大家参考. 1.下载 最新的 poi http://poi.apache.org/download.html 2.解压 把相关jar包引进项 ...
- POI3.10读取Excel模板填充数据后生成新的Excel文件
private final DecimalFormat df = new DecimalFormat("#0.00"); public void test(){ String fi ...
随机推荐
- [bug] HDFS:hdfs missing blocks. The following files may be corrupted
原因 HDFS数据块丢失,需要删除丢失块的元信息 bin/hadoop fsck / -delete 参考 https://blog.csdn.net/lixgjob/article/details/ ...
- [c++] 细节
\r退格:printf("asdflkj\r111")输出111flkj(https://blog.csdn.net/tyshtang/article/details/436770 ...
- 关于flume中涉及到时间戳的错误解决,Expected timestamp in the Flume even
在搭建flume集群收集日志写入hdfs时发生了下面的错误: java.lang.NullPointerException: Expected timestamp in the Flume event ...
- Linux_配置认证访问FTP服务
[RHEL8]-FTPserver:[Centos8]-FTPclient !!!测试环境我们首关闭防火墙和selinux(FTPserver和FTPclient都需要) [root@localhos ...
- 搭建LNMP环境部署Wordpress博客
!!!首先要做的就是关闭系统的防火墙以及selinux: #systemctl stop firewalld #systemctl disable firewalld #sed -ri 's/^(SE ...
- ceph总结复习
一.ceph概念 Ceph是一种为优秀的性能.可靠性和可扩展性而设计的统一的.分布式文件系统.ceph 的统一体现在可以提供文件系统.块存储和对象存储,分布式体现在可以动态扩展. 什么是块存储/对象存 ...
- 050.Python前端jQuery
一 jQuery是什么? jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. jQuery是继prototype之后又一个优秀的Ja ...
- STM32电路设计注意
以后画STM32的电路板 VDDA一定要接 张JF说 VDDA是给内部的时钟电路供电的 还有一定要留 串口或者 下载调试口(串口) 或者显示器接口 输入输出设备最好都留着 这样才能方便调试
- shell字符截取
shell字符截取 截取文本中以vm开头的单词 grep -o vm.* text | cut -d' ' -f1 截取活动主机
- 图解CSS布局(一)- Grid布局
图解CSS布局(一)- Grid布局 先上图 简介 Grid 布局是将容器划分成"行"和"列",产生单元格,然后指定"项目所在"的单元格,可 ...