C++Builder_Excel打印模式

来源:互联网 发布:苹果下载网络视频软件 编辑:程序博客网 时间:2024/04/29 07:01

 void ExcelPrint(TDBGrid * dbg)
{
          int zj_index=-1;  //add Yanfl 2009-3-17    zj_index 从1开始计数
          long double zj=0.0;        //add Yanfl 2009-3-17
          if(!dbg->DataSource->DataSet->Active)   //   数据集没有打开就返回
          {  
                  Application->MessageBox("数据集没有打开,导出失败!"   ,   "提示",   MB_OK)   ;  
                  return ;  
          }  
          Variant   vExcelApp,   vSheet;  
          try  
          {  
                  vExcelApp   =   Variant::CreateObject("Excel.Application");  
          }  
          catch(...)  
          {  
                  Application->MessageBox(   "启动   Excel   出错,   可能是没有安装Excel.",  
                                  "DBGrid2Excel",   MB_OK   +   MB_ICONERROR);  
                  return;  
          }  
          //   隐藏Excel界面  修改为显示
          vExcelApp.OlePropertySet("Visible",   true);  
          //   新建一个工作表  
          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++)  
          {
                 if(dbg->Columns->Items[j]->Title->Caption=="总价")
                 {
                        zj_index=j+1;
                 }
                  //   标题行的行高
                  vExcelApp.OlePropertyGet("Rows",   1).OlePropertySet("RowHeight",   20);
                  //
                  vSheet.OlePropertyGet("Cells",   1,   j   +   1)
                                  .OlePropertySet("Value",
                                  dbg->Columns->Items[j]->Title->Caption.c_str());

          }  
          //   将DBGrid中的数据写入Excel表格
          dbg->DataSource->DataSet->First();
          int i;
          for(i=0;   i<nRowCount&&!dbg->DataSource->DataSet->Eof;   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());
                          if(j==zj_index-1)     //add Yanfl
                                zj+=dbg->DataSource->DataSet->FieldByName(dbg->Columns->Items[j]->FieldName)->AsFloat;
                  }  
                  dbg->DataSource->DataSet->Next();
          }
          //将合计数据写入Excel 表格    add Yanfl 2009-03-17
          if(zj_index>0)
          {
                vSheet.OlePropertyGet("Cells",   i+2,   1)
                                  .OlePropertySet("Value","费用合计");
                vSheet.OlePropertyGet("Cells",   i+2,   zj_index)
                                  .OlePropertySet("Value",FloatToStr(zj).c_str());
          }
          // 将签字 领导签字写入Excel 表格
                vSheet.OlePropertyGet("Cells",   i+4,   1)
                                  .OlePropertySet("Value","总经理签字:");
                vSheet.OlePropertyGet("Cells",   i+4,   3)
                                  .OlePropertySet("Value","部门经理签字:");
                vSheet.OlePropertyGet("Cells",   i+4,   5)
                                  .OlePropertySet("Value","制表人:");

          //   打印Excel文档并退出
          //excel打印页面设置

          //vExcelApp.OlePropertyGet("ActiveWindow").OlePropertySet("DisplayGridlines",False);   //不显示背景的网格线;

          vSheet.OlePropertyGet("PageSetup").OlePropertySet("CenterHorizontally",2/0.035);//页面水平居中:

          vSheet.OlePropertyGet("PageSetup").OlePropertySet("PrintGridLines",true);//打印表格线;

          vSheet.OlePropertyGet("PageSetup").OlePropertySet("Orientation",2);   //Orientation=poLandscape;1;2为横向;

          vSheet.OleFunction("PrintPreview");//打印预览

          vExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertySet("Saved",true); //不保存直接退出

          vExcelApp.OleFunction("Quit");;
          vSheet   =   Unassigned;  
          vExcelApp   =   Unassigned;
}

原创粉丝点击