下面图老师小编要跟大家分享VC中对office的操作,简单的过程中其实暗藏玄机,还是要细心学习,喜欢还请记得收藏哦!
【 tulaoshi.com - 编程语言 】
以前导出数据的时候都是自己定义格式,自己来解析,或者直接导出到TXT里,这显然不能满足用户的继续编辑的要求,所以我用自动化技术写了几个函数可以将数据导出到word和excel中,仅仅是标格数据。。。
这个过程是这样的:http://blog.csdn.net/hoya5121
在VC6的类向导中的AUTOMATION页筐中“ADD CLASS”,然后选择“from a type”,浏览到office目录选择导入工程文件 EXCEL9.OLB 和msword9.olb,然后将需要的类导入到工程中,这里出现一个问题,就是excel和word同时导入的时候excel9.h 和msword9.h
两个文件中居然有名字相同的类导致连接出错,我不知道是我使用错误,还是ms没注意到这个问题。。。最终我这样解决了这个问题,在excel.h文件中用namespace将正个文件加入到一个名字空间中。。。
接下来的工作就比较简单了,打开office将你需要的流程录制成vba的宏,然后将宏翻译成vc的代码。。下面将我的导入标格的代码贴出来:
/**/////////////////对WORD的操作 2005-09-26///////////////////////
//.h
class CStringArrayOut : public CStringArray
...{
public:
CStringArrayOut& operator = (const CStringArrayOut& strarrarr)
...{
if(&strarrarr == this)
return *this;
this-RemoveAll();
for(int i = 0; i strarrarr.GetSize(); i++)
...{
this-Add(strarrarr[i]);
}
return *this;
}
};
typedef CArrayCStringArrayOut,CStringArrayOut& StrVecVecType;
void WordTable(CString tablename,int rowNum,int colNum,StrVecVecType& tablestr,CString strOutDocFile);
void ExcelTable(CString tablename,
int rowNum,
int colNum,
StrVecVecType& tablestr,
CString strOutExcelFile);
//.cpp
void WordTable(CString tablename,int rowNum,int colNum,StrVecVecType& tablestr,CString strOutDocFile)
...{
int i, j;
/**//////打开word/////////////////////////////////////////////////////////////////////
_Application app;
if(!app.CreateDispatch(_T("Word.Application")))
...{
AfxMessageBox(_T("您没有安装OFFICE,此功能不能执行!"));
return;
}
//app.SetVisible(TRUE); //使WORD可见
/**//////新建文档/////////////////////////////////////////////////////////////////////
Documents docs = app.GetDocuments();
CComVariant Template(_T(""));
CComVariant NewTemplate(false),DocumentType(0),Visible;
docs.Add(&Template,&NewTemplate,&DocumentType,&Visible);
//设置视图
Window win = app.GetActiveWindow();
View view = win.GetView();
Pane ap = win.GetActivePane();
if(view.GetSplitSpecial() == 0)
...{
view.SetType(1);
}
else
...{
view.SetType(1);
}
/**//////绘制标格/////////////////////////////////////////////////////////////////////
_Document doc = app.GetActiveDocument();
Selection sel = app.GetSelection();
//写表名
_Font font = sel.GetFont();
float fs = font.GetSize();
Paragraphs pg = sel.GetParagraphFormat();
long ali = pg.GetAlignment();
font.SetSize(16);
pg.SetAlignment(1);
sel.TypeText(tablename);
sel.TypeParagraph();
font.SetSize(fs);
pg.SetAlignment(ali);
Tables tables = doc.GetTables();
Range range = sel.GetRange();
//CComVariant v1((short)1);
//CComVariant v2((short)1);
CComVariant v1((short)1);
CComVariant v2((short)1);
tables.Add(range,rowNum,colNum,&v1,&v2);
/**////填写标格///////////////////////////////////////////////////////////////////////
if(tablestr.GetSize() = 1)
...{
AfxMessageBox("绘制标格时出错!");
doc.ReleaseDispatch();
docs.ReleaseDispatch();
win.ReleaseDispatch();
view.ReleaseDispatch();
ap.ReleaseDispatch();
sel.ReleaseDispatch();
font.ReleaseDispatch();
CComVariant SaveChanges(false),OriginalFormat,RouteDocument;
app.Quit(&SaveChanges,&OriginalFormat,&RouteDocument);
app.ReleaseDispatch();
return;
}
CComVariant SaveChanges(false),OriginalFormat,RouteDocument;
app.Quit(&SaveChanges,&OriginalFormat,&RouteDocument);
app.ReleaseDispatch();
return;
}
for(i = 0; i rowNum; i++)
...{
for(j = 0; j colNum; j++)
...{
sel.TypeText(tablestr[i][j]);
sel.MoveRight(COleVariant((short)1),COleVariant((short)1),COleVariant((short)0));
}
}
//设置标格居中
Table tb = tables.Item(1L);
//tb.AutoFitBehavior(1L);
Rows rs = tb.GetRows();
rs.SetAlignment(1);
COleVariant vTrue((short)TRUE),
vFalse((short)FALSE);
doc.SaveAs(//保存文档
COleVariant(strOutDocFile),
COleVariant((short)0),
vFalse,
COleVariant(""),
vTrue,
COleVariant(""),
vFalse,
vFalse,
vFalse,
vFalse,
vFalse);
tb.ReleaseDispatch();
rs.ReleaseDispatch();
doc.ReleaseDispatch();
docs.ReleaseDispatch();
win.ReleaseDispatch();
view.ReleaseDispatch();
ap.ReleaseDispatch();
sel.ReleaseDispatch();
font.ReleaseDispatch();
CComVariant SaveChanges(false),OriginalFormat,RouteDocument;
app.Quit(&SaveChanges,&OriginalFormat,&RouteDocument);
app.ReleaseDispatch();
}
void ExcelTable(CString tablename,
int rowNum,
int colNum,
StrVecVecType& tablestr,
CString strOutExcelFile)
...{
//using namespace excel9;
int i,j;
excel9::_Application app;
excel9::_Workbook wbook;
excel9::Workbooks wbooks;
excel9::Worksheets sheets;
excel9::_Worksheet sheet;
excel9::Range range;
excel9::Range iCell;
//Range unionRange;
//LPDISPATCH lpDisp;
//COleVariant vResult;
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
if(!app.CreateDispatch("Excel.Application"))
...{
AfxMessageBox ("create excel faild!");
return;
}
//app.SetVisible(TRUE);
wbooks = app.GetWorkbooks();
wbook = wbooks.Add(covOptional);
//AfxMessageBox("");
sheets = wbook.GetSheets();
sheet = sheets.GetItem(COleVariant((short)1));
range.AttachDispatch(sheet.GetCells());
long iStartCol = 1; //使用区域的起始列,从1开始
long iStartRow = 1; //使用区域的起始行,从1开始
int iMaxCol = 6;
// 单元格样式设置(居中)
COleVariant varH, varV, varInfo;
varH.vt = VT_I2;
varV.vt = VT_I2;
varH.iVal = -4108;
varV.iVal = -4108;
range.SetHorizontalAlignment (varH);
range.SetVerticalAlignment(varV);
//最大化文档
excel9::Window win;
win = app.GetActiveWindow();
win.SetWindowState(-4137);
CArrayint,int& maxcolwidarr;
for(i = 0; i colNum; i++)
...{
int maxwid = tablestr[0][i].GetLength();
maxcolwidarr.Add(maxwid);
for(j = 0; j rowNum; j++)
...{
if(tablestr[j][i].GetLength() maxcolwidarr[i])
maxcolwidarr[i] = tablestr[j][i].GetLength();
}
}
//填写标格
for(int row = 1; row rowNum+1; row++)
...{
for(int col = 1; col colNum+1; col++)
...{
iCell = sheet.GetCells();
iCell.SetItem(COleVariant((short )(row)),
COleVariant((short )(col)),
COleVariant(tablestr[row-1][col-1]));
//iCell.SetColumnWidth(COleVariant((short)(maxcolwidarr[col-1])));
}
}
//设置宽度
for (int z = 0; z maxcolwidarr.GetSize(); z++)
...{
iCell.AttachDispatch(range.GetItem
(COleVariant((long)1),COleVariant((long)(z+1))).pdispVal);
iCell.SetColumnWidth (COleVariant((long)maxcolwidarr[z]));
}
//保存文件
wbook.SaveAs(COleVariant(strOutExcelFile),
COleVariant((short )(-4143)),
COleVariant(""),
COleVariant(""),
COleVariant((short )(FALSE)),
COleVariant((short )(FALSE)),
0L,
COleVariant((short )(NULL)),
COleVariant((short )(NULL)),
COleVariant((short )(NULL)),
COleVariant((short )(NULL)));
//return
app.Quit();
app.ReleaseDispatch();
wbook.ReleaseDispatch();
wbooks.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
range.ReleaseDispatch();
iCell.ReleaseDispatch();
win.ReleaseDispatch();
}
来源:http://www.tulaoshi.com/n/20160219/1619873.html
看过《VC中对office的操作》的人还看了以下文章 更多>>