在VC++中建立了基于单文档的工程,有打印等功能,在单文档的视图界面上有列表控件,以显示数据库系统的数据,怎么可以让打印出内容啊,自带的打印功能打印出的是空白的,

解决方案 »

  1.   

    数据库中的数据,可用Execl进行打印,参看下面的代码:
    void CExcelPrintDlg::OnButprint() 
    {
    // TODO: Add your control notification handler code here
    _Application app;    
    Workbooks books;
    _Workbook book;
    Worksheets sheets;
    _Worksheet sheet;
    Range range; //创建Excel 2000服务器(启动Excel) 
    if (!app.CreateDispatch("Excel.Application",NULL)) 

    AfxMessageBox("创建Excel服务失败!"); 
    return; 

    app.SetVisible(false); 
    //利用模板文件建立新文档 
    char path[MAX_PATH];
    GetCurrentDirectory(MAX_PATH,path);
    CString strPath = path;
    strPath += "\\ExcelPrint";
    books.AttachDispatch(app.GetWorkbooks(),true);
    book.AttachDispatch(books.Add(_variant_t(strPath)));
    //得到Worksheets 
    sheets.AttachDispatch(book.GetWorksheets(),true);
    //得到sheet1 
    sheet.AttachDispatch(sheets.GetItem(_variant_t("sheet1")),true);
    CString str1;
    str1 = "第1页";
    sheet.SetName(str1);
    for( int i=0;i<sheets.GetCount()-1;i++)
    {
    sheet = sheet.GetNext();
    str1.Format("第%d页",i+2);
    sheet.SetName(str1);
    }
    sheet.AttachDispatch(sheets.GetItem(_variant_t("第1页")),true);
    //得到全部Cells,rgMyRge是cells的集合 
    range.AttachDispatch(sheet.GetCells(),true); 
    CString sText;
    OnInitADOConn();
    _bstr_t bstrSQL;
    bstrSQL = "select * from employees order by 学生编号 desc";
    m_pRecordset.CreateInstance(__uuidof(Recordset));
    m_pRecordset->Open(bstrSQL,m_pConnection.GetInterfacePtr(),
    adOpenDynamic,adLockOptimistic,adCmdText);
    Fields* fields = NULL;
    long countl;
    BSTR bstr;
    m_pRecordset->get_Fields(&fields);
    countl = fields->Count;
    _variant_t sField[10];
    for(long num=0;num<countl;num++)
    {
    sText.Format("%d",num);
    fields->Item[(long)num]->get_Name(&bstr);
    sField[num] = (_variant_t)bstr;
    range.SetItem(_variant_t((long)(2)),_variant_t((long)(num+1)),
    _variant_t(sField[num]));
    }
    int setnum=2;
    while(!m_pRecordset->adoEOF)
    {
    for(long num=0;num<m_pRecordset->GetFields()->GetCount();num++)
    {
    sText.Format("%d",num);
    range.SetItem(_variant_t((long)(setnum+1)),_variant_t((long)(num+1)),
    _variant_t(m_pRecordset->GetCollect(sField[num])));
    }
    m_pRecordset->MoveNext();
    setnum++;
    }
    ExitConnect();
    app.SetVisible(true); 
    book.PrintPreview(_variant_t(false));
    //释放对象 
    range.ReleaseDispatch(); 
    sheet.ReleaseDispatch(); 
    sheets.ReleaseDispatch(); 
    book.ReleaseDispatch(); 
    books.ReleaseDispatch();
    app.ReleaseDispatch(); 
    }