国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

C++將DBGrid中數(shù)據(jù)導(dǎo)出到Word和Excel

2019-11-17 05:19:04
字體:
供稿:網(wǎng)友

  經(jīng)常看到有網(wǎng)友發(fā)帖子詢問如何將DBGrid中的內(nèi)容導(dǎo)出到ExcelWord文檔中,于是筆者花了點(diǎn)時(shí)間寫了以下兩個(gè)函數(shù),分別實(shí)現(xiàn)將DBGrid中數(shù)據(jù)導(dǎo)出到Word和Excel文檔。需要注重的是DBGrid中的數(shù)據(jù)并不代表數(shù)據(jù)庫中所有的數(shù)據(jù),因?yàn)閿?shù)據(jù)集在打開的時(shí)候有可能進(jìn)行了篩選,取決于使用者如何打開這個(gè)數(shù)據(jù)集,總之就是DBGrid中顯示多少數(shù)據(jù),就導(dǎo)出多少。

  一、將DBGrid中的內(nèi)容導(dǎo)出到Word文檔

//---------------------------------------------------------------------------
// 將DBGrid中的數(shù)據(jù)導(dǎo)出到Word文檔
//---------------------------------------------------------------------------
void __fastcall DBGrid2Word(TDBGrid *dbg, String strDocFile)
{
 if(!dbg->DataSource->DataSet->Active) // 數(shù)據(jù)集沒有打開就返回
  return;
 Variant vWordApp, vTable, vCell;
 try
 {
  vWordApp = Variant::CreateObject("Word.application");
 }
 catch(...)
 {
  MessageBox(0, "啟動(dòng) Word 出錯(cuò), 可能是沒有安裝Word.","DBGrid2Word", MB_OK MB_ICONERROR);
  vWordApp = Unassigned;
  return;
 }
 // 隱藏Word界面
 vWordApp.Ole // 新建一個(gè)文檔
 vWordApp.OlePropertyGet("Documents").OleFunction("Add");
 Variant vSelect = vWordApp.OlePropertyGet("Selection");
 // 設(shè)置一下
字體,大小
 vSelect.OlePropertyGet("Font").OlePropertySet("Size", dbg->Font->Size);
 vSelect.OlePropertyGet("Font").OlePropertySet("Name", dbg->Font->Name.c_str());
 // 要插入表格的行數(shù)
 int nRowCount(dbg->DataSource->DataSet->RecordCount + 1);
 nRowCount = nRowCount < 2? 2: nRowCount;
 // 要插入表格的列數(shù)
 int nColCount(dbg->Columns->Count);
 nColCount = nColCount < 1? 1: nColCount;
 // 在Word文檔中插入與DBGrid行數(shù)列數(shù)基本相同的一個(gè)表格
 vWordApp.OlePropertyGet("ActiveDocument").OlePropertyGet("Tables").OleProcedure("Add",
   vSelect.OlePropertyGet("Range"),
   nRowCount, // 行數(shù)
   nColCount, // 列數(shù)
   1, // DefaultTableBehavior:=wdWord9TableBehavior
   0); // AutoFitBehavior:=wdAutoFitFixed
 // 操作這個(gè)表格
 vTable = vWordApp.OlePropertyGet("ActiveDocument").
 OleFunction("Range").OlePropertyGet("Tables").OleFunction("Item", 1);
 // 設(shè)置單元格的寬度
 for(int i=0; i<nColCount; i++)
 {
  int nColWidth = dbg->Columns->Items[i]->Width;
  vTable.OlePropertyGet("Columns").OleFunction("Item", i + 1)
    .OlePropertySet("PreferredWidthType", 3); // wdPreferredWidthPoints
  vTable.OlePropertyGet("Columns").OleFunction("Item", i + 1)
    .OlePropertySet("PreferredWidth", nColWidth);
 }
 // 先將列名寫入Word表格
 for(int j=0; j<dbg->Columns->Count; j++)
 {
  vCell = vTable.OleFunction("Cell", 1, j + 1);
  vCell.OlePropertySet("Range", dbg->Columns->Items[j]->FieldName.c_str());
  // 列名單元格背景顏色 // wdColorGray125
  vCell.OlePropertyGet("Shading").OlePropertySet("BackgroundPatternColor", 14737632);
 }
 // 將DBGrid中的數(shù)據(jù)寫入Word表格
 dbg->DataSource->DataSet->First();
 for(int i=0; i<nRowCount; i++)
 {
  // 63 63 72 75 6E 2E 63 6F 6D
  for(int j=0; j<dbg->Columns->Count; j++)
  {
   vCell = vTable.OleFunction("Cell", i + 2, j + 1);
   vCell.OlePropertySet("Range",
    dbg->DataSource->DataSet->FieldByName(
     dbg->Columns->Items[j]->FieldName)->AsString.c_str());
  }
  dbg->DataSource->DataSet->Next();
 }
 // 保存Word文檔并退出
 vWordApp.OlePropertyGet("ActiveDocument").OleProcedure("SaveAs", strDocFile.c_str());
 vWordApp.OlePropertyGet("ActiveDocument").OleProcedure("Close");
 Application->ProcessMessages();
 vWordApp.OleProcedure("Quit");
 Application->ProcessMessages();
 vWordApp = Unassigned;
 // 工作結(jié)束
 MessageBox(0, "DBGrid2Word 轉(zhuǎn)換結(jié)束!","DBGrid2Word", MB_OK MB_ICONINFORMATION);
}

  二、將DBGrid中的內(nèi)容導(dǎo)出到Excel文檔


//---------------------------------------------------------------------------
// 將DBGrid中的數(shù)據(jù)導(dǎo)出到Excel文檔
//---------------------------------------------------------------------------
void __fastcall DBGrid2Excel(TDBGrid *dbg, String strXlsFile)
{
 if(!dbg->DataSource->DataSet->Active) // 數(shù)據(jù)集沒有打開就返回
  return;
 Variant vExcelApp, vSheet;
 try
 {
  vExcelApp = Variant::CreateObject("Excel.Application");
 }
 catch(...)
 {
  MessageBox(0, "啟動(dòng) Excel 出錯(cuò), 可能是沒有安裝Excel.","DBGrid2Excel", MB_OK MB_ICONERROR);
  return;
 }
 // 隱藏Excel界面
 vExcelApp.OlePropertySet("Visible", false);
 // 新建一個(gè)工作表
 vExcelApp.OlePropertyGet("Workbooks").OleFunction("Add", 1); // 工作表
 // 操作這個(gè)工作表
 vSheet = vExcelApp.OlePropertyGet("ActiveWorkbook").OlePropertyGet("Sheets", 1);
 // 設(shè)置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", "常規(guī)");
 vSheet.OlePropertyGet("Cells", 1, 1).OleProcedure("Select");
 // 表格的行數(shù)
 int nRowCount(dbg->DataSource->DataSet->RecordCount + 1);
 nRowCount = nRowCount < 2? 2: nRowCount;
 // 表格的列數(shù)
 int nColCount(dbg->Columns->Count);
 nColCount = nColCount < 1? 1: nColCount;
 // 設(shè)置單元格的寬度
 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++)
 {
  // 標(biāo)題行的行高
  vExcelApp.OlePropertyGet("Rows", 1).OlePropertySet("RowHeight", 20);
  vSheet.OlePropertyGet("Cells", 1, j + 1)
   .OlePropertySet("Value", dbg->Columns->Items[j]->FieldName.c_str());
  // 設(shè)置列名單元格的背景色
  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中的數(shù)據(jù)寫入Excel表格
 dbg->DataSource->DataSet->First();
 for(int i=0; i<nRowCount; i++)
 {
  // 普通數(shù)據(jù)行的行高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;
 // 工作結(jié)束
 MessageBox(0, "DBGrid2Excel 轉(zhuǎn)換結(jié)束!","DBGrid2Excel", MB_OK MB_ICONINFORMATION);
}


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 凭祥市| 长岛县| 个旧市| 新田县| 乌拉特中旗| 山西省| 扬州市| 荆州市| 若尔盖县| 姚安县| 西乡县| 台北县| 栖霞市| 唐海县| 岳阳县| 故城县| 四川省| 城步| 依兰县| 砚山县| 黔东| 吴堡县| 洛南县| 桂平市| 蒲江县| 东方市| 平果县| 根河市| 安仁县| 枣庄市| 阳城县| 曲麻莱县| 隆德县| 彭泽县| 商洛市| 崇文区| 正蓝旗| 克拉玛依市| 黄梅县| 邹城市| 恭城|