|
经常看到有网友发帖子询问如何将ListView中的内容导出到Excel或Word文档中,其实在BCB中用OLE技术来操作,并不复杂,大概是有的人懒的写吧,于是ccrun(老妖)花了点时间写了以下两个函数,实现了将本程序中ListView中内容导出到Excel文档和Word文档。看在写代码很辛苦的份上,请在转载时留下出处和原作者信息。Thank了。:D 如果您有好的想法,欢迎来信讨论: info@ccrun.com
2005.10.13 v0.2 + 导出表格增加了标题一栏 2005.10.12 v0.1 初版发布
void __fastcall ListView2Word(TListView *lv, String strDocFile) { Variant vWordApp, vTable, vCell; try { vWordApp = Variant::CreateObject("Word.Application"); } catch(...) { MessageBox(0, "启动 Word 出错, 可能是没有安装Word.", "ListView2Doc", 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", lv->Font->Size); vSelect.OlePropertyGet("Font").OlePropertySet("Name", lv->Font->Name.c_str()); int nRowCount(lv->Items->Count + 1); nRowCount = nRowCount < 2? 2: nRowCount; int nColCount(lv->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; if(lv->Columns->Count > i) nColWidth = lv->Columns->Items[i]->Width; else nColWidth = 100; vTable.OlePropertyGet("Columns").OleFunction("Item", i + 1) .OlePropertySet("PreferredWidthType", 3); vTable.OlePropertyGet("Columns").OleFunction("Item", i + 1) .OlePropertySet("PreferredWidth", nColWidth * 2); } for(int i=0; i<lv->Columns->Count; i++) { vCell = vTable.OleFunction("Cell", 1, i + 1); vCell.OlePropertySet("Range", lv->Columns->Items[i]->Caption.c_str()); vCell.OlePropertyGet("Shading") .OlePropertySet("BackgroundPatternColor", 14737632); } for(int i=0; i<nRowCount - 1; i++) { vCell = vTable.OleFunction("Cell", i + 2, 1); vCell.OlePropertySet("Range", lv->Items->Item[i]->Caption.c_str()); for(int j=0; j<lv->Items->Item[i]->SubItems->Count; j++) { if(lv->Columns->Count > j) { vCell = vTable.OleFunction("Cell", i + 2, j + 2); vCell.OlePropertySet("Range", lv->Items->Item[i]->SubItems->Strings[j].c_str()); } } } vWordApp.OlePropertyGet("ActiveDocument") .OleProcedure("SaveAs", strDocFile.c_str()); vWordApp.OlePropertyGet("ActiveDocument").OleProcedure("Close"); Application->ProcessMessages(); vWordApp.OleProcedure("Quit"); Application->ProcessMessages(); vWordApp = Unassigned; MessageBox(0, "ListView2Doc 转换结束!", "ListView2Doc", MB_OK | MB_ICONINFORMATION); }
void __fastcall ListView2Excel(TListView *lv, String strXlsFile) { Variant vExcelApp, vSheet; try { vExcelApp = Variant::CreateObject("Excel.Application"); } catch(...) { MessageBox(0, "启动 Excel 出错, 可能是没有安装Excel.", "ListView2Excel", MB_OK | MB_ICONERROR); vExcelApp = Unassigned; return; } vExcelApp.OlePropertySet("Visible", true); 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", lv->Font->Size); vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font") .OlePropertySet("Name", lv->Font->Name.c_str()); vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font") .OlePropertySet("FontStyle", "常规"); vSheet.OlePropertyGet("Cells", 1, 1).OleProcedure("Select"); int nRowCount(lv->Items->Count + 1); nRowCount = nRowCount < 2? 2: nRowCount; int nColCount(lv->Columns->Count); nColCount = nColCount < 1? 1: nColCount; for(int i=0; i<nColCount; i++) { int nColWidth; if(lv->Columns->Count > i) nColWidth = lv->Columns->Items[i]->Width; else nColWidth = 100; vExcelApp.OlePropertyGet("Columns", i + 1) .OlePropertySet("ColumnWidth", nColWidth / 2.6); } for(int j=0; j<lv->Columns->Count; j++) { vExcelApp.OlePropertyGet("Rows", 1).OlePropertySet("RowHeight", 20); vSheet.OlePropertyGet("Cells", 1, j + 1) .OlePropertySet("Value", lv->Columns->Items[j]->Caption.c_str()); Variant vInter = vSheet.OlePropertyGet( "Cells", 1, j + 1).OlePropertyGet("Interior"); vInter.OlePropertySet("ColorIndex", 15); vInter.OlePropertySet("Pattern", 1); vInter.OlePropertySet("PatternColorIndex", -4105); } for(int i=0; i<nRowCount - 1; i++) { vExcelApp.OlePropertyGet("Rows", i + 2).OlePropertySet("RowHeight", 16); vSheet.OlePropertyGet("Cells", i + 2, 1) .OlePropertySet("Value", lv->Items->Item[i]->Caption.c_str()); for(int j=0; j<lv->Items->Item[i]->SubItems->Count; j++) { vSheet.OlePropertyGet("Cells", i + 2, j + 2) .OlePropertySet("Value", lv->Items->Item[i]->SubItems->Strings[j].c_str()); } } vExcelApp.OlePropertyGet("ActiveWorkbook") .OleFunction("SaveAs", strXlsFile.c_str()); vExcelApp.OleFunction("Quit"); vSheet = Unassigned; vExcelApp = Unassigned; MessageBox(0, "ListView2Excel 转换结束!", "ListView2Excel", MB_OK | MB_ICONINFORMATION); }
ListView2Word(ListView1, "C:\\ccrun\\234.doc"); ListView2Excel(ListView1, "C:\\ccrun\\234.xls");
|