NX Second Development - NX on the MFC read and write interface for Excel (OLE mode (COM component))

 

NX secondary development there are no relevant API function EXCAL read and write operations, the market there are many ways to implement such transfer UFUN KF, ODBC, OLE (COM component) and the like. Here I am using the OLE (COM components) way to do it, this method created on the VC, C ++ or C #, whether or VB way ideas are the same. First introduced to do with MFC, and then write a blog describes how to do the secondary development of templates in the wizard's NX.
NX Second Development - based NX NX development wizard template for Excel reads and writes https://ufun-nxopen.blog.csdn.net/article/details/88922238 (OLE manner (COM component)) (Look at this blog before, the current blog first reading).
version: VS2013 office2016

 

Image reproduced from https://blog.csdn.net/xiaoxiangyuan123456/article/details/70941588, thanks to bloggers.

  • EXCAL operation (a) open write

Reference example: https: //blog.csdn.net/ywx123_/article/details/77074038
This is another CSDN bloggers to write, I do also refer to his example, and then thank the bloggers.

Create a new MFC project

Click Next.

Select dialog-based

Direct Done, come in below

The first point of the project, right property, change to multi-byte.

First nothing moving, compile the code. Default can not see through.
Then project, right-class wizard.

The following pop-up window

Click Add Class - MFC class type library

Pop up

We switched to a file, click Add.

To add a file found here is your computer to upload EXCAL.exe the program file.
If you do not know how to find it, there is a method, go to the Start menu to find your EXCAL shortcut, then right click, open the file location.

This found.

下面在回到MFC项目里面,我们选中这个文件。
添加进来之后,如图所示。

左侧为EXCAL给出的接口,我们来选择一些需要的添加进来。
我们就先添加这七个,后续有其他需求在添加其他的。(先在左侧接口里选中一个,点中间的>就能添加到右侧里生成的类里,不想要就在点中间的<撤退回去。)

然后我们点完成,点确定。就看到头文件自动添加进来了。

我们点击新增进来的EXCAL每个头文件,都把第一行的那句注释掉。

都注释完之后,我们在#include "stdafx.h"里添加EXCAL头文件,

然后编译代码,出现如下报错问题。这时我们双击这个错误,跳到对应的代码那里。

有问题的位置如下

我们需要在DialogBox前面加上一个 _ 下划线。再去编译就通过了。别问我为什么,我答不上来,你照着搞就是了,别人就是这么搞的。

好的,到这里我们这个项目环境就算搭建完成了。
下面可以写代码了。
点击MFC那个对话框界面,双击确定。在里面写代码。

代码如下:打开EXCAL,写入内容。

  1     //1.创建基本对象
  2     CApplication App;  //创建应用程序实例
  3     CWorkbooks Books;  //工作簿,多个Excel文件
  4     CWorkbook Book;    //单个工作簿
  5     CWorksheets sheets;//多个sheet页面
  6     CWorksheet sheet;  //单个sheet页面
  7     CRange range;      //操作单元格
  8     //2.打开指定Excel文件,如果不存在就创建
  9     char path[MAX_PATH];
 10     GetCurrentDirectory(MAX_PATH, (TCHAR*)path);//获取当前路径
 11     CString strExcelFile = (TCHAR*)path;
 12     CString strdevName = _T("\\Test.xlsx");    //xls也行
 13     strExcelFile += strdevName;
 14     COleVariant
 15         covTrue((short)TRUE),
 16         covFalse((short)FALSE),
 17         covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
 18 
 19     LPDISPATCH lpdisp = NULL;
 20     //1.创建Excel实例
 21     if (!App.CreateDispatch(_T("Excel.Application"), NULL))
 22     {
 23         AfxMessageBox(_T("创建Excel实例失败"));
 24         exit(-1);
 25     }
 26     else
 27     {
 28         AfxMessageBox(_T("创建成功"));
 29     }
 30     App.put_Visible(TRUE);  //打开Excel
 31     App.put_UserControl(FALSE);
 32     //2. 得到workbooks容器
 33     Books.AttachDispatch(App.get_Workbooks());
 34     Book.AttachDispatch(Books.Add(covOptional));
 35     sheets.AttachDispatch(Book.get_Worksheets());
 36     sheet.AttachDispatch(sheets.get_Item(COleVariant((short)1)));   //获取sheet1
 37     sheet.put_Name(_T("TestName"));     //设置sheet1名字
 38 
 39     //3. 加载要合并的单元格
 40     range.AttachDispatch(sheet.get_Range(COleVariant(_T("B2")), COleVariant(_T("E2"))), TRUE);
 41     range.Merge(COleVariant((long)0));  //合并单元格
 42 
 43     //4. 设置表格内容
 44     range.AttachDispatch(sheet.get_Cells(), TRUE);               //加载所有单元格
 45     range.put_Item(COleVariant((long)2), COleVariant((long)2), COleVariant(_T("电气工程及其自动化课程统计")));
 46     range.put_Item(COleVariant((long)3), COleVariant((long)2), COleVariant(_T("课程名称")));
 47     range.put_Item(COleVariant((long)3), COleVariant((long)3), COleVariant(_T("课时")));
 48     range.put_Item(COleVariant((long)3), COleVariant((long)4), COleVariant(_T("难度")));
 49     range.put_Item(COleVariant((long)3), COleVariant((long)5), COleVariant(_T("教学方式")));
 50 
 51     range.put_Item(COleVariant((long)4), COleVariant((long)2), COleVariant(_T("电磁场")));
 52     range.put_Item(COleVariant((long)4), COleVariant((long)3), COleVariant(_T("30")));
 53     range.put_Item(COleVariant((long)4), COleVariant((long)4), COleVariant(_T("变态难")));
 54     range.put_Item(COleVariant((long)4), COleVariant((long)5), COleVariant(_T("老师讲课")));
 55 
 56     range.put_Item(COleVariant((long)5), COleVariant((long)2), COleVariant(_T("电机学")));
 57     range.put_Item(COleVariant((long)5), COleVariant((long)3), COleVariant(_T("40")));
 58     range.put_Item(COleVariant((long)5), COleVariant((long)4), COleVariant(_T("")));
 59     range.put_Item(COleVariant((long)5), COleVariant((long)5), COleVariant(_T("老师讲课加实验")));
 60 
 61     range.put_Item(COleVariant((long)6), COleVariant((long)2), COleVariant(_T("PLC")));
 62     range.put_Item(COleVariant((long)6), COleVariant((long)3), COleVariant(_T("20")));
 63     range.put_Item(COleVariant((long)6), COleVariant((long)4), COleVariant(_T("普通")));
 64     range.put_Item(COleVariant((long)6), COleVariant((long)5), COleVariant(_T("老师讲课加实验")));
 65 
 66 
 67     range.put_Item(COleVariant((long)7), COleVariant((long)2), COleVariant(_T("电力系统")));
 68     range.put_Item(COleVariant((long)7), COleVariant((long)3), COleVariant(_T("50")));
 69     range.put_Item(COleVariant((long)7), COleVariant((long)4), COleVariant(_T("")));
 70     range.put_Item(COleVariant((long)7), COleVariant((long)5), COleVariant(_T("老师讲课加实验")));
 71 
 72     range.AttachDispatch(sheet.get_UsedRange());//加载已使用的单元格
 73     range.put_WrapText(COleVariant((long)1));   //设置文本自动换行
 74 
 75     //5.设置对齐方式
 76     //水平对齐:默认 1 居中 -4108, 左= -4131,右=-4152
 77     //垂直对齐:默认 2 居中 -4108, 左= -4160,右=-4107
 78     range.put_VerticalAlignment(COleVariant((long)-4108));
 79     range.put_HorizontalAlignment(COleVariant((long)-4108));
 80     //6.设置字体颜色
 81     CFont0 ft;
 82     ft.AttachDispatch(range.get_Font());
 83     ft.put_Name(COleVariant(_T("楷体"))); //字体
 84     ft.put_ColorIndex(COleVariant((long)1));//颜色    //黑色
 85     ft.put_Size(COleVariant((long)12));     //大小
 86 
 87     range.AttachDispatch(sheet.get_Range(COleVariant(_T("B2")), COleVariant(_T("E2"))), TRUE);
 88     ft.AttachDispatch(range.get_Font());
 89     ft.put_Name(COleVariant(_T("华文行楷")));
 90     ft.put_Bold(COleVariant((long)1));
 91     ft.put_ColorIndex(COleVariant((long)5));    //颜色    
 92     ft.put_Size(COleVariant((long)18));         //大小
 93     Book.SaveCopyAs(COleVariant(strExcelFile)); //保存
 94 
 95     Book.put_Saved(TRUE);
 96     //8.释放资源
 97     range.ReleaseDispatch();
 98     sheet.ReleaseDispatch();
 99     sheets.ReleaseDispatch();
100     Book.ReleaseDispatch();
101     Books.ReleaseDispatch();
102     App.ReleaseDispatch();

编译后,我们点调试-开始执行。

点确定,自动打开了EXCAL,并写入了内容。EXCAL自动保存在了你这个项目的当前文件夹里。

就写到这里了,好多我也没有用过,回头用到了再去研究。其他的相关内容大家参考下面这些大神们的博客吧。
参考资料:
一个封装的实用的EXCEL类 https://blog.csdn.net/gyssoft/article/details/1592104
vs对Excel读写操作 https://blog.csdn.net/qq_16334327/article/details/81353248
在 VS2008 下操作 Excel 的方法总结 https://blog.csdn.net/DavidHsing/article/details/4231592
VS2010 对Excel读写操作 https://blog.csdn.net/ywx123_/article/details/77074038
VS2010 MFC Excel(1)https://www.cnblogs.com/zztong/p/6695283.html
VS2010 MFC 读取Excel(2)https://www.cnblogs.com/zztong/p/6695284.html
VS2010对Excel2010进行操作 https://blog.csdn.net/xiaoxiangyuan123456/article/details/70941588
(转)VS2010 MFC 操作Excel(读写)https://blog.csdn.net/zhaodan19861107/article/details/80675888
VS2010 MFC读取Excel文件中数据 https://blog.csdn.net/V10_x/article/details/78464453
MFC vs2012 Office2013 读写excel文件(OLE/COM)https://blog.csdn.net/baidu_37503452/article/details/72842573
vs创建控制台程序使用C++读写excel文件(ODBC方式)https://www.cnblogs.com/renjiashuo/p/7545784.html
VC++遍历Excelhttps://blog.csdn.net/z3312656/article/details/8282194
感谢各位大神无私的奉献。

Guess you like

Origin www.cnblogs.com/nxopen2018/p/10981416.html