社区
VC.NET
帖子详情
VC++导出数据到Excel实现,最好有例子!谢谢!
heyingss
2015-02-15 10:50:55
VC++导出数据到Excel实现,最好有例子!谢谢!
...全文
519
4
打赏
收藏
VC++导出数据到Excel实现,最好有例子!谢谢!
VC++导出数据到Excel实现,最好有例子!谢谢!
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
4 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
wanjiAri
2015-11-25
打赏
举报
回复
本人常用的一种方式,采用另存为txt的方式,换行用\r\n,要是分开的数据用\t间隔。另存为的txt用excel打开,挺方便的
heyingss
2015-03-02
打赏
举报
回复
我自己又研究了一下,以下代码供大家参考: #include "stdafx.h" #include "RepairData.h" #include "LogInfo.h" #include ".\loginfo.h" #include <odbcinst.h> #include <afxdb.h> #include <comdef.h> #include <io.h> 。。。。。。 //获取ODBC中Excel驱动函数 CString CLogInfo::GetExcelDriver() { char szBuf[2001]; WORD cbBufMax = 2000; WORD cbBufOut; char *pszBuf = szBuf; CString sDriver; // 获取已安装驱动的名称(涵数在odbcinst.h里) if (!SQLGetInstalledDrivers(szBuf, cbBufMax, &cbBufOut)) return ""; // 检索已安装的驱动是否有Excel... do { if (strstr(pszBuf, "Excel") != 0) { //发现 ! sDriver = CString(pszBuf); break; } pszBuf = strchr(pszBuf, '\0') + 1; } while (pszBuf[1] != '\0'); return sDriver; } /////////////////////////////////////////////////////////////////////////////// // BOOL MakeSurePathExists( CString &Path,bool FilenameIncluded) // 参数: // Path 路径 // FilenameIncluded 路径是否包含文件名 // 返回值: // 文件是否存在 // 说明: // 判断Path文件(FilenameIncluded=true)是否存在,存在返回TURE,不存在返回FALSE // 自动创建目录 // /////////////////////////////////////////////////////////////////////////////// BOOL CLogInfo::MakeSurePathExists( CString &Path, bool FilenameIncluded) { int Pos=0; while((Pos=Path.Find('\\',Pos+1))!=-1) CreateDirectory(Path.Left(Pos),NULL); if(!FilenameIncluded) CreateDirectory(Path,NULL); // return ((!FilenameIncluded)?!_access(Path,0): // !_access(Path.Left(Path.ReverseFind('\\')),0)); return !_access(Path,0); } //获得默认的文件名 BOOL CLogInfo::GetDefaultXlsFileName(CString& sExcelFile) { ///默认文件名:yyyymmddhhmmss.xls CString timeStr; CTime day; day=CTime::GetCurrentTime(); int filenameday,filenamemonth,filenameyear,filehour,filemin,filesec; filenameday=day.GetDay();//dd filenamemonth=day.GetMonth();//mm月份 filenameyear=day.GetYear();//yyyy filehour=day.GetHour();//hh filemin=day.GetMinute();//mm分钟 filesec=day.GetSecond();//ss timeStr.Format("%04d%02d%02d%02d%02d%02d",filenameyear,filenamemonth,filenameday,filehour,filemin,filesec); //AfxMessageBox(timeStr); sExcelFile = timeStr + ".xls"; // prompt the user (with all document templates) CFileDialog dlgFile(FALSE,".xls",sExcelFile); CString title; CString strFilter; title = "导出"; strFilter = "Excel文件(*.xls)"; strFilter += (TCHAR)'\0'; // next string please strFilter += _T("*.xls"); strFilter += (TCHAR)'\0'; // last string dlgFile.m_ofn.nMaxCustFilter++; dlgFile.m_ofn.nFilterIndex = 1; // append the "*.*" all files filter CString allFilter; VERIFY(allFilter.LoadString(AFX_IDS_ALLFILTER)); strFilter += allFilter; strFilter += (TCHAR)'\0'; // next string please strFilter += _T("*.*"); strFilter += (TCHAR)'\0'; // last string dlgFile.m_ofn.nMaxCustFilter++; dlgFile.m_ofn.lpstrFilter = strFilter; dlgFile.m_ofn.lpstrTitle = title; if (dlgFile.DoModal()==IDCANCEL) return FALSE; // open cancelled sExcelFile.ReleaseBuffer(); if (MakeSurePathExists(sExcelFile,true)) { if(!DeleteFile(sExcelFile)) { // delete the file AfxMessageBox("覆盖文件时出错!"); return FALSE; } } return TRUE; } /////////////////////////////////////////////////////////////////////////////// // void GetExcelDriver(CListCtrl* pList, CString strTitle) // 参数: // pList 需要导出的List控件指针 // strTitle 导出的数据表标题 // 说明: // 导出CListCtrl控件的全部数据到Excel文件。Excel文件名由用户通过“另存为” // 对话框输入指定。创建名为strTitle的工作表,将List控件内的所有数据(包括 // 列名和数据项)以文本的形式保存到Excel工作表中。保持行列关系。 // // edit by [r]@dotlive.cnblogs.com /////////////////////////////////////////////////////////////////////////////// void CLogInfo::ExportListToExcel(CSortListCtrl &pList, CString strTitle) { CString warningStr; if (pList.GetItemCount()>0) { CDatabase database; CString sDriver; CString sExcelFile; CString sSql; CString tableName = strTitle; // 检索是否安装有Excel驱动 "Microsoft Excel Driver (*.xls)" sDriver = GetExcelDriver(); // AfxMessageBox(sDriver); if (sDriver.IsEmpty()) { // 没有发现Excel驱动 AfxMessageBox("没有安装Excel!\n请先安装Excel软件才能使用导出功能!"); return; } ///默认文件名 if (!GetDefaultXlsFileName(sExcelFile)) return; // 创建进行存取的字符串 sSql.Format("DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s",sDriver, sExcelFile, sExcelFile); //AfxMessageBox(sSql); // 创建数据库 (既Excel表格文件) if( database.OpenEx(sSql,CDatabase::noOdbcDialog) ) { // 创建表结构 int i; LVCOLUMN columnData; CString columnName; int columnNum = 0; CString strH; CString strV; sSql = ""; strH = ""; columnData.mask = LVCF_TEXT; columnData.cchTextMax =100; columnData.pszText = columnName.GetBuffer (100); for(i=0;pList.GetColumn(i,&columnData);i++) { if (i!=0) { sSql = sSql + ", " ; strH = strH + ", " ; } sSql = sSql + " " + columnData.pszText +" TEXT"; strH = strH + " " + columnData.pszText +" "; } columnName.ReleaseBuffer (); columnNum = i; sSql = "CREATE TABLE " + tableName + " ( " + sSql + " ) "; // AfxMessageBox(sSql); database.ExecuteSQL(sSql); // 插入数据项 int nItemIndex; for (nItemIndex=0;nItemIndex<pList.GetItemCount ();nItemIndex++){ strV = ""; for(i=0;i<columnNum;i++) { if (i!=0) { strV = strV + ", " ; } strV = strV + " '" + pList.GetItemText(nItemIndex,i) +"' "; } sSql = "INSERT INTO "+ tableName +" ("+ strH + ")" +" VALUES("+ strV + ")"; // AfxMessageBox(sSql); database.ExecuteSQL(sSql); } } // 关闭数据库 database.Close(); warningStr.Format("导出文件保存于%s!",sExcelFile); AfxMessageBox(warningStr); }else{ AfxMessageBox("目前没有审核操作日志信息!"); } }
蝶恋花雨
2015-02-15
打赏
举报
回复
http://www.360doc.com/content/13/0614/16/3402399_292842196.shtml
VS2010的
蝶恋花雨
2015-02-15
打赏
举报
回复
参考。看看能帮助你不。
http://blog.163.com/zhongguojiliangfgg@126/blog/static/4851001720099219397848/
QT或VC程序中
导出
图片到
EXCEL
指定单元格过程说明
作者项目开发的程序中要
实现
将程序中
数据
,包含图片要
导出
到
EXCEL
表格中,图片要根据不同
数据
对应到不同的单元格中,经过5天研究参看大量网络
例子
及思考,完成了
导出
图片到指定单元格编码
实现
。希望看官5分钟搞定方法,以免再因此耗时并烦恼。 主要介绍了QT程序中
导出
图片到
EXCEL
指定单元格的
实现
过程及源码, VC或其他编程语言同样可参考方法说明通过
Excel
.Application
导出
图片到指定单元格。
VC
数据
导出
到
Excel
在
c++
内将
数据
导出
到
excel
,相信会很有用
V
C++
Excel
文件的导入和
导出
操作程序源代码
V
C++
Excel
文件的导入和
导出
操作程序源代码,包括生成
Excel
,从
Excel
文件导入,
导出
Excel
文件内容到指定文件中等操作,挺实用的一个
Excel
操作VC示例
例子
。 运行环境:Windows/Visual C/
C++
c++
读取
excel
表格并在控件中绘制曲线图等
c++
读取
excel
表格并在控件中绘制曲线,通过命令可以读取
excel
表格中的
数据
,然后在teechart控件中可以绘制出一些曲线图,散点图,饼状图等等
VC_中
实现
Excel
自动化编程
简述V
C++
6.0中对
excel
自动化编程, 即如何将
数据
库中
数据
以打印报表的形式输出到
excel
文档中, 如何将
excel
文档导入到
数据
库中。
VC.NET
7,539
社区成员
27,671
社区内容
发帖
与我相关
我的任务
VC.NET
.NET技术 VC.NET
复制链接
扫一扫
分享
社区描述
.NET技术 VC.NET
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章