单文档打印问题!求帮助! 在VC++中建立了基于单文档的工程,有打印等功能,在单文档的视图界面上有列表控件,以显示数据库系统的数据,怎么可以让打印出内容啊,自带的打印功能打印出的是空白的, 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 数据库中的数据,可用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(); } 如何让一个线程安全退出 一个消息执行过程中 要响应另外的消息 msxml 读取xml元素名的问题,为什么会读出“#text”的字样? 网络端口资源释放问题 VC++学习 散分!顺便问下,像我现在这样的情况能否找到工作? 提供正确思路者有分,有关声卡,200分任你取! 问一个简单的问题,关于datetime的。在线等。 图形图像处理问题 断点调试和运行结果不同的问题 其他语言调用COM控件按什么原理生成包装类 mfc post 提交数据到服务器 返回的中文全是乱码。
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();
}