outEXcel.txt
上传用户:yangrryang
上传日期:2022-07-21
资源大小:1k
文件大小:5k
源码类别:

文件操作

开发平台:

C++ Builder

  1.  if(!dbg->DataSource->DataSet->Active)   //   数据集没有打开就返回   
  2.                   return;   
  3.           Variant   vExcelApp,   vSheet;   
  4.           try   
  5.           {   
  6.                   vExcelApp   =   Variant::CreateObject("Excel.Application");   
  7.           }   
  8.           catch(...)   
  9.           {   
  10.                   MessageBox(0,   "启动   Excel   出错,   可能是没有安装Excel.",   
  11.                                   "DBGrid2Excel",   MB_OK   |   MB_ICONERROR);   
  12.                   return;   
  13.           }   
  14.           //   隐藏Excel界面   
  15.           vExcelApp.OlePropertySet("Visible",   false);   
  16.           //   新建一个工作表   
  17.           vExcelApp.OlePropertyGet("Workbooks").OleFunction("Add",   1);   //   工作表   
  18.           //   操作这个工作表   
  19.           vSheet   =   vExcelApp.OlePropertyGet("ActiveWorkbook")   
  20.                           .OlePropertyGet("Sheets",   1);   
  21.           //   设置Excel文档的字体   
  22.           vSheet.OleProcedure("Select");   
  23.           vSheet.OlePropertyGet("Cells").OleProcedure("Select");   
  24.           vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")   
  25.                           .OlePropertySet("Size",   dbg->Font->Size);   
  26.           vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")   
  27.                           .OlePropertySet("Name",   dbg->Font->Name.c_str());   
  28.           vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")   
  29.                           .OlePropertySet("FontStyle",   "常规");   
  30.           vSheet.OlePropertyGet("Cells",   1,   1).OleProcedure("Select");   
  31.           //   表格的行数   
  32.           int   nRowCount(dbg->DataSource->DataSet->RecordCount   +   1);   
  33.           nRowCount   =   nRowCount   <   2?   2:   nRowCount;   
  34.           //   表格的列数   
  35.           int   nColCount(dbg->Columns->Count);   
  36.           nColCount   =   nColCount   <   1?   1:   nColCount;   
  37.           //   设置单元格的宽度   
  38.           for(int   i=0;   i<nColCount;   i++)   
  39.           {   
  40.                   int   nColWidth   =   dbg->Columns->Items[i]->Width;   
  41.                   vExcelApp.OlePropertyGet("Columns",   i   +   1)   
  42.                                   .OlePropertySet("ColumnWidth",   nColWidth   /   7);   
  43.           }   
  44.           //----------------------------------------------------------------------------   
  45.           //   抱歉,这个提示又来了,为了防止不负责任的转载者,只好在此留些信息。   
  46.           //   作者:ccrun(老妖)   info@ccrun.com   
  47.           //   本文转自   C++Builder   研究   -   http://www.ccrun.com/article/go.asp?i=635&d=g75jbn   
  48.           //----------------------------------------------------------------------------               
  49.           //   先将列名写入Excel表格   
  50.           for(int   j=0;   j<dbg->Columns->Count;   j++)   
  51.           {   
  52.                   //   标题行的行高   
  53.                   vExcelApp.OlePropertyGet("Rows",   1).OlePropertySet("RowHeight",   20);   
  54.                   //   
  55.                   vSheet.OlePropertyGet("Cells",   1,   j   +   1)   
  56.                                   .OlePropertySet("Value",   
  57.                                   dbg->Columns->Items[j]->FieldName.c_str());   
  58.                   //   设置列名单元格的背景色   
  59.                   Variant   vInter   =   vSheet.OlePropertyGet(   
  60.                                   "Cells",   1,   j   +   1).OlePropertyGet("Interior");   
  61.                   vInter.OlePropertySet("ColorIndex",   15);   //   灰色   
  62.                   vInter.OlePropertySet("Pattern",   1);   //   xlSolid   
  63.                   vInter.OlePropertySet("PatternColorIndex",   -4105);   //   xlAutomatic   
  64.           }   
  65.           //   将DBGrid中的数据写入Excel表格   
  66.           dbg->DataSource->DataSet->First();   
  67.           for(int   i=0;   i<nRowCount;   i++)   
  68.           {   
  69.                   //   普通数据行的行高16   
  70.                   vExcelApp.OlePropertyGet("Rows",   i   +   2).OlePropertySet("RowHeight",   16);   
  71.                   //   63   63   72   75   6E   2E   63   6F   6D   
  72.                   for(int   j=0;   j<dbg->Columns->Count;   j++)   
  73.                   {   
  74.                           vSheet.OlePropertyGet("Cells",   i   +   2,   j   +   1)   
  75.                                   .OlePropertySet("Value",   
  76.                                   dbg->DataSource->DataSet->FieldByName(   
  77.                                   dbg->Columns->Items[j]->FieldName)->AsString.c_str());   
  78.                   }   
  79.                   dbg->DataSource->DataSet->Next();   
  80.           }   
  81.           //   保存Excel文档并退出   
  82.           vExcelApp.OlePropertyGet("ActiveWorkbook")   
  83.                           .OleFunction("SaveAs",   strXlsFile.c_str());   
  84.           vExcelApp.OleFunction("Quit");   
  85.           vSheet   =   Unassigned;   
  86.           vExcelApp   =   Unassigned;   
  87.           //   工作结束   
  88.           MessageBox(0,   "DBGrid2Excel   转换结束!",   
  89.                           "DBGrid2Excel",   MB_OK   |   MB_ICONINFORMATION);   
  90.   }   
  91.     
  92.   //   测试代码   
  93.   DBGrid2Excel(DBGrid1,   "C:\ccrun\123.xls");