|
昨天ccrun刚写了将ListView中内容导出到Word文档和Excel文档的一篇文章,今天写程序凑巧用到了将DBGrid中数据导出到Office的功能,干脆再写个兄弟版的函数出来,DBGrid2Word和DBGrid2Excel,分别实现将DBGrid中数据导出到Word和Excel文档。需要注意的是DBGrid中的数据并不代码数据库中所有的数据,因为数据集在打开的时候有可能进行了筛选,取决于使用者如何打开这个数据集,总之就是DBGrid中显示多少数据,就导出多少。看在写代码很辛苦的份上,请在转载时留下出处和原作者信息。Thank了。:D 如果您有好的想法,或者代码中存在的BUG,欢迎来信讨论: info@ccrun.com
2005.10.13 v0.1 初版发布
void __fastcall DBGrid2Word(TDBGrid *dbg, String strDocFile) { if(!dbg->DataSource->DataSet->Active) return; Variant vWordApp, vTable, vCell; try { vWordApp = Variant::CreateObject("Word.Application"); } catch(...) { MessageBox(0, "启动 Word 出错, 可能是没有安装Word.", "DBGrid2Word", MB_OK | MB_ICONERROR); vWordApp = Unassigned; return; } vWordApp.OlePropertySet("Visible", false); vWordApp.OlePropertyGet("Documents").OleFunction("Add"); Variant vSelect = vWordApp.OlePropertyGet("Selection"); vSelect.OlePropertyGet("Font").OlePropertySet("Size", dbg->Font->Size); vSelect.OlePropertyGet("Font").OlePropertySet("Name", dbg->Font->Name.c_str()); int nRowCount(dbg->DataSource->DataSet->RecordCount + 1); nRowCount = nRowCount < 2? 2: nRowCount; int nColCount(dbg->Columns->Count); nColCount = nColCount < 1? 1: nColCount; vWordApp.OlePropertyGet("ActiveDocument").OlePropertyGet("Tables") .OleProcedure("Add", vSelect.OlePropertyGet("Range"), nRowCount, nColCount, 1, 0); vTable = vWordApp.OlePropertyGet("ActiveDocument"). OleFunction("Range").OlePropertyGet("Tables").OleFunction("Item", 1); for(int i=0; i<nColCount; i++) { int nColWidth = dbg->Columns->Items[i]->Width; vTable.OlePropertyGet("Columns").OleFunction("Item", i + 1) .OlePropertySet("PreferredWidthType", 3); vTable.OlePropertyGet("Columns").OleFunction("Item", i + 1) .OlePropertySet("PreferredWidth", nColWidth); } 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()); vCell.OlePropertyGet("Shading") .OlePropertySet("BackgroundPatternColor", 14737632); } dbg->DataSource->DataSet->First(); for(int i=0; i<nRowCount; i++) { 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(); } vWordApp.OlePropertyGet("ActiveDocument") .OleProcedure("SaveAs", strDocFile.c_str()); vWordApp.OlePropertyGet("ActiveDocument").OleProcedure("Close"); Application->ProcessMessages(); vWordApp.OleProcedure("Quit"); Application->ProcessMessages(); vWordApp = Unassigned; MessageBox(0, "DBGrid2Word 转换结束!", "DBGrid2Word", MB_OK | MB_ICONINFORMATION); }
void __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; } vExcelApp.OlePropertySet("Visible", false); vExcelApp.OlePropertyGet("Workbooks").OleFunction("Add", 1); vSheet = vExcelApp.OlePropertyGet("ActiveWorkbook") .OlePropertyGet("Sheets", 1); 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); } 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); vInter.OlePropertySet("PatternColorIndex", -4105); } dbg->DataSource->DataSet->First(); for(int i=0; i<nRowCount; i++) { vExcelApp.OlePropertyGet("Rows", i + 2).OlePropertySet("RowHeight", 16); 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(); } vExcelApp.OlePropertyGet("ActiveWorkbook") .OleFunction("SaveAs", strXlsFile.c_str()); vExcelApp.OleFunction("Quit"); vSheet = Unassigned; vExcelApp = Unassigned; MessageBox(0, "DBGrid2Excel 转换结束!", "DBGrid2Excel", MB_OK | MB_ICONINFORMATION); }
DBGrid2Word(DBGrid1, "C:\\ccrun\\123.doc"); DBGrid2Excel(DBGrid1, "C:\\ccrun\\123.xls");
|