一 环境
操作系统:win7
office word版本:2010
程序类型:基于对话框的MFC应用程序
二 系统界面
三 导出word结果
四 源代码
API这里不做详细介绍了,网上资料一大堆,直接上代码。
void CEquipmentSystemDlg::ExportWord(CString strFileName, CString type)
{
COleVariant vTrue((short)TRUE),
vFalse((short)FALSE),
vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
_Application oWordApp; //开始一个新的Microsoft Word 2000实例
if (!oWordApp.CreateDispatch(_T("Word.Application"), NULL)){
AfxMessageBox(_T("服务创建失败,请确定已经安装了Office 2000或以上版本."), MB_OK | MB_SETFOREGROUND);
return;
}
Documents oDocs; //创建一个新的word文档
_Document oDoc;
oDocs = oWordApp.GetDocuments();
oDoc = oDocs.Add(vOpt, vOpt, vOpt, vOpt);//如果是word 98,则应该带两个参数,如oDocs.Add(vOpt, vOpt)
_Document curDoc = oWordApp.GetActiveDocument();
CPageSetup p = curDoc.GetPageSetup();
p.put_LeftMargin(50);
p.put_RightMargin(50);
p.put_TopMargin(50);
p.put_BottomMargin(40);
Selection oSel; //把文本添加到word文档
oSel = oWordApp.GetSelection();
CFont0 f=oSel.GetFont();
f.put_Size(18);
setlocale(LC_ALL, "chs");
CParagraphFormat format = oSel.GetParagraphFormat();
format.put_Alignment(1);
oSel.TypeText(_T("重庆东正实验耗材有限公司销售单"));
oSel.TypeParagraph();
//抬头信息
f.put_Size(13);
format.put_Alignment(3);
oSel.TypeText(_T(""));//空一行
oSel.TypeParagraph();
CString cstr1, cstr2, cstr3;
char str1[1024] = {0};
char str2[1024] = {0};
char str3[1024] = {0};
//打印购买客户和订单日期信息
this->GetDlgItem(IDC_EDIT_NAME)->GetWindowText(cstr1);
this->GetDlgItem(IDC_EDIT_DATE)->GetWindowText(cstr2);
sprintf(str1, "购买客户:%S", cstr1);
FillEmptyChar(str1, 45);
sprintf(str2, "订单日期:%S", cstr2);
sprintf(str3, "%s%s", str1, str2);
cstr3.Format(_T("%s"), CStringW(str3));
oSel.TypeText(cstr3);
oSel.TypeParagraph();
//打印客户电话和订单编号信息
this->GetDlgItem(IDC_EDIT_PHONE)->GetWindowTextW(cstr1);
this->GetDlgItem(IDC_EDIT_ID)->GetWindowTextW(cstr2);
sprintf(str1, "客户电话:%S", cstr1);
FillEmptyChar(str1, 45);
sprintf(str2, "订单编号:%S", cstr2);
sprintf(str3, "%s%s", str1, str2);
cstr3.Format(_T("%s"), CStringW(str3));
oSel.TypeText(cstr3);
oSel.TypeParagraph();
//打印客户地址信息
this->GetDlgItem(IDC_EDIT_ADDRESS)->GetWindowTextW(cstr1);
sprintf(str1, "客户地址:%S", cstr1);
cstr2.Format(_T("%s"), CStringW(str1));
oSel.TypeText(cstr2);
oSel.TypeParagraph();
//空一行
oSel.TypeText(_T(""));
oSel.TypeParagraph();
f.put_Size(13);
format.put_Alignment(3);
_Document saveDoc=oWordApp.GetActiveDocument();
CComVariant defaultBehavior(1),AutoFitBehavior(2);
CTables0 tables=saveDoc.GetTables();
int len = m_listProducts.GetItemCount();
tables.Add(oSel.GetRange(),len+1,8,&defaultBehavior,&AutoFitBehavior);
//指定表头
oSel.TypeText(_T("序号"));
oSel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
oSel.TypeText(_T("商品名称"));
oSel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
oSel.TypeText(_T("规格"));
oSel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
oSel.TypeText(_T("单位"));
oSel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
oSel.TypeText(_T("单价"));
oSel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
oSel.TypeText(_T("数量"));
oSel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
oSel.TypeText(_T("金额"));
oSel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
oSel.TypeText(_T("产地"));
oSel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
//填写表格内容
for (int i = 0; i < len; i++)
{
oSel.MoveDown(COleVariant((short)4),COleVariant((short)1),COleVariant((short)0));
for (int j = 0; j < 9; j++)
{
if (j != 1)
{
oSel.TypeText(m_listProducts.GetItemText(i, j));
oSel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
}
}
}
oSel.MoveDown(COleVariant((short)4),COleVariant((short)1),COleVariant((short)0));
oSel.MoveDown(COleVariant((short)5),COleVariant((short)1),COleVariant((short)0));
//打印统计信息
sprintf(str1, " 总数量:");
CString totalNum;
this->GetDlgItem(IDC_STATIC_NUM)->GetWindowText(totalNum);
sprintf(str2, "%S", totalNum);
this->FillEmptyChar(str2, 6);
CString totalMoney;
this->GetDlgItem(IDC_STATIC_MONEY)->GetWindowText(totalMoney);
sprintf(str3, "总金额:%S元", totalMoney);
this->FillEmptyChar(str3, 18);
char str4[1024];
sprintf(str4, "%s%s%s", str1, str2, str3);
cstr1.Format(_T("%s"), CStringW(str4));
oSel.TypeText(cstr1);
oSel.TypeParagraph();
//换行
oSel.TypeText(_T(""));
oSel.TypeParagraph();
//打印制单信息
cstr1 = _T("赵敏");
sprintf(str1, "制单人:%S 出库审核人: 送货人: 客户签字: ", cstr1);
cstr1.Format(_T("%s"), CStringW(str1));
oSel.TypeText(cstr1);
oSel.TypeParagraph();
//打印公司联系方式信息
this->GetDlgItem(IDC_STATIC_SADDRESS_CONTENT)->GetWindowTextW(cstr1);
this->GetDlgItem(IDC_STATIC_FAX_CONTENT)->GetWindowTextW(cstr2);
this->GetDlgItem(IDC_STATIC_SPHONE_CONTENT)->GetWindowTextW(cstr3);
sprintf(str1, "地址:%S", cstr1);
FillEmptyChar(str1, 35);
sprintf(str2, "传真:%S 电话:%S", cstr2, cstr3);
sprintf(str3, "%s%s", str1, str2);
cstr1.Format(_T("%s"), CStringW(str3));
oSel.TypeText(cstr1);
oSel.TypeParagraph();
//打印发票提示信息
oSel.TypeText(_T("注:收到本单3日内,提出收货异议,7日内提出质量异议有效,逾期概不更改"));
oSel.TypeParagraph();
//保存word文档
_Document oActiveDoc;
oActiveDoc = oWordApp.GetActiveDocument();
oActiveDoc.SaveAs(COleVariant(strFileName),COleVariant((short)0),vFalse, COleVariant(_T("")), vTrue, COleVariant(_T("")),vFalse, vFalse, vFalse, vFalse, vFalse);
oSel.ReleaseDispatch();
oDoc.ReleaseDispatch();
oDocs.ReleaseDispatch();
if (type == "print")
oWordApp.SetVisible(true);
oWordApp.ReleaseDispatch();
}
表格的列宽度不知道怎么设置,从导出结果图可以看出,导出的表格的列宽都是相等的,在官网上翻看了API,没有找到对应的API。