c++builder 6.0 将DBGrid中的数据导出到Word和Excel
来源:互联网 发布:淘宝找客服吗? 编辑:程序博客网 时间:2024/05/10 08:34
因为需求,所以自学了c++,没有人带着还是有点慢!遇到很多问题网上很难找到资料,幸好在这网站遇到好多热心人!!
我我也在网上找到了几个不错的资料,需要的朋友可以 去我的资源下载!
今天这代码是从网上看到的,我正好需要,我把他整理了一下下!!
代码来自:点击打开链接
主要方法
// 导出excelvoid __fastcall DBGrid2Excel(TDBGrid *dbg, String strXlsFile){ if(!dbg->DataSource->DataSet->Active) // 数据集没有打开就返回 return; Variant vExcelApp, vSheet; try { vExcelApp = Variant::CreateObject("Excel.Application"); } catch(...) { MessageBox(0, "启动 Excel 出错, 可能是没有安装Excel.", "DBGrid2Excel", MB_OK | MB_ICONERROR); return; } // 隐藏Excel界面 vExcelApp.OlePropertySet("Visible", false); // 新建一个工作表 vExcelApp.OlePropertyGet("Workbooks").OleFunction("Add", 1); // 工作表 // 操作这个工作表 vSheet = vExcelApp.OlePropertyGet("ActiveWorkbook") .OlePropertyGet("Sheets", 1); // 设置Excel文档的字体 vSheet.OleProcedure("Select"); vSheet.OlePropertyGet("Cells").OleProcedure("Select"); vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font") .OlePropertySet("Size", dbg->Font->Size); vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font") .OlePropertySet("Name", dbg->Font->Name.c_str()); vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font") .OlePropertySet("FontStyle", "常规"); vSheet.OlePropertyGet("Cells", 1, 1).OleProcedure("Select"); // 表格的行数 int nRowCount(dbg->DataSource->DataSet->RecordCount + 1); nRowCount = nRowCount < 2? 2: nRowCount; // 表格的列数 int nColCount(dbg->Columns->Count); nColCount = nColCount < 1? 1: nColCount; // 设置单元格的宽度 for(int i=0; i<nColCount; i++) { int nColWidth = dbg->Columns->Items[i]->Width; vExcelApp.OlePropertyGet("Columns", i + 1) .OlePropertySet("ColumnWidth", nColWidth / 7); }//--------------------------------------------------------------------------- // 先将列名写入Excel表格 for(int j=0; j<dbg->Columns->Count; j++) { // 标题行的行高 vExcelApp.OlePropertyGet("Rows", 1).OlePropertySet("RowHeight", 20); // vSheet.OlePropertyGet("Cells", 1, j + 1) .OlePropertySet("Value", dbg->Columns->Items[j]->FieldName.c_str()); // 设置列名单元格的背景色 Variant vInter = vSheet.OlePropertyGet( "Cells", 1, j + 1).OlePropertyGet("Interior"); vInter.OlePropertySet("ColorIndex", 15); // 灰色 vInter.OlePropertySet("Pattern", 1); // xlSolid vInter.OlePropertySet("PatternColorIndex", -4105); // xlAutomatic } // 将DBGrid中的数据写入Excel表格 dbg->DataSource->DataSet->First(); for(int i=0; i<nRowCount; i++) { // 普通数据行的行高16 vExcelApp.OlePropertyGet("Rows", i + 2).OlePropertySet("RowHeight", 16); // 63 63 72 75 6E 2E 63 6F 6D for(int j=0; j<dbg->Columns->Count; j++) { vSheet.OlePropertyGet("Cells", i + 2, j + 1) .OlePropertySet("Value", dbg->DataSource->DataSet->FieldByName( dbg->Columns->Items[j]->FieldName)->AsString.c_str()); } dbg->DataSource->DataSet->Next(); } // 保存Excel文档并退出 vExcelApp.OlePropertyGet("ActiveWorkbook") .OleFunction("SaveAs", strXlsFile.c_str()); vExcelApp.OleFunction("Quit"); vSheet = Unassigned; vExcelApp = Unassigned; // 工作结束 MessageBox(0, "DBGrid2Excel 转换结束!", "DBGrid2Excel", MB_OK | MB_ICONINFORMATION);}
//点击导出按钮事件代码
void __fastcall TUserInfoForm::btnExcelClick(TObject *Sender){ if(MessageDlg("确认要导出数据吗?", mtConfirmation, TMsgDlgButtons() << mbYes << mbNo, 0) ==mrYes) { if(SaveDialog1->Execute()) { String fileName=SaveDialog1->FileName; DBGrid2Excel(DBGridUser,fileName); } }}
0 0
- c++builder 6.0 将DBGrid中的数据导出到Word和Excel
- 将DBGrid中的数据导出到Word和Excel
- 将DBGrid的数据导出到word和excel中
- 将DBGrid的数据导出到word和excel中
- 将DBGrid中的数据导出到Word
- DBGrid导出Excel和Word
- 将DBGrid中的内容导出到Word文档
- c++builder数据导出到excel中的问题
- 如何将DBGrid中的数据原样导出到Excel表中
- 将DBGrid的数据导出到Excel文件保存
- 将DBGrid的数据导出到Excel文件保存
- 将DBGrid中的内容导出到Excel文档
- 将DBGrid中的内容导出到Excel文档
- 将GridView中的数据导出到Excel,Word,Html
- 将GridView中的数据导出到Excel / Word中
- 将GridView中的数据导出到Excel / Word中
- ASP.NET(C#)将数据导出到Word或Excel
- 将DataGrid数据导出到Excel和Word文档
- ORACLE SELECT基本查询
- AlarmManager延迟生效
- 理解A*寻路算法具体过程
- ubuntu14.04下安装samba共享服务器
- CodeForces 550C
- c++builder 6.0 将DBGrid中的数据导出到Word和Excel
- C++中两种创建对象的方法
- MathType 6.7 For Mac 简体中文版发布啦~
- css基础
- OAuth的授权步骤
- 清除浮动的几种方法
- 1439 - Exclusive Access 2
- Android之断点续传(一)
- 下拉刷新动画研究