Function:
In MFC dialog based project, the Edit Control to display data in multiple runs saved in Excel.
Detailed steps are as follows:
1. In the dialog based MFC project built, open the Class Wizard> click on the down arrow to the right of the Add Class> select MFC class library type
2. Choose File> Select the computer installed in the path of the Microsoft office EXCEL.EXE> add several files in the figure below.
Wherein:
the Application: on behalf of the application itself. That application Excel
Workbooks: thin working set
Workbook: workbook, a sub-object Workbooks
Worksheets: is a collection of (sheet) of the Worksheet, the child object is Workbook
Worksheet: worksheet, is a child object of Worksheets
3. After the addition was complete, the statement corresponding to each class header file as shown below commented
3. Add the following to the file header files Dlg.cpp operation of Excel
4. BOOL .cpp file in the project CSaveExcelTestApp :: InitInstance () function, the INT_PTR nResponse = dlg.DoModal (); add the following statement before the statement
if (CoInitialize(NULL)!=0)
{
AfxMessageBox(_T("初始化COM支持库失败!"));
exit(1);
}
After the alarm appears as follows compiler
Solution:
In CRange class, DialogBox () was added into the foregoing underscores _DialogBox ().
5. Define a type of CString array SaveExcelTestDlg.h header file for storing data in multiple runs.
CString InsData[100];
int n ;
Dump data was as follows
UpdateData(true);
InsData[n] = m_Edit1_Data;
if(n==100)
{
n = 0;
}
else
n++;
m_Edit1_Data Edit Control is the type of value Cstring member variable.
6. Create a dialog box within the Save button, double-click into void CSaveExcelTestDlg :: OnBnClickedButton1 () function, add the following procedure:
CWorkbooks books;
CWorkbook book;
CApplication app;
CWorksheets sheets;
CWorksheet sheet;
CRange range;
CRange cols;
COleVariant vResult;
COleVariant covOptional((long) DISP_E_PARAMNOTFOUND,VT_ERROR);
CTime tm = CTime::GetCurrentTime();
CString str,str1,str2;
CString Range[]={_T("A"),_T("B"),_T("C"),_T("D"),_T("E"),_T("F")};
CString title[]={_T("NO."),_T("数据1"),_T("数据2"),_T("数据3"),_T("数据4"),_T("数据5")};
int i = 0;
if (!app.CreateDispatch(_T("Excel.Application")))
{
AfxMessageBox(_T("无法启动服务器"));
return ;
}
books.AttachDispatch(app.get_Workbooks());
//得到Workbook
book.AttachDispatch(books.Add(covOptional),true);
//得到Worksheets
sheets.AttachDispatch(book.get_Worksheets(),true);
sheet.AttachDispatch(sheets.get_Item(_variant_t("sheet1")),true);
for(int j=0;j<6;j++)
{
str.Format(_T("%s%d"),Range[j],1);
range=sheet.get_Range(COleVariant(str),COleVariant(str));
range.put_Value2(COleVariant(title[j]));
cols = range.get_EntireColumn();//选择整列,并设置宽度为自适应
cols.AutoFit();
}
for(int k=0;k<(n/5);k++)
{
for(int j=1;j<6;j++)
{
str.Format(_T("%s%d"),Range[0],k+2);
str2.Format(_T("%d"),k+1);
range=sheet.get_Range(COleVariant(str),COleVariant(str));
range.put_Value2(COleVariant(str2));
str.Format(_T("%s%d"),Range[j],k+2);
range=sheet.get_Range(COleVariant(str),COleVariant(str));
range.put_Value2(COleVariant(InsData[i]));
if(i==n)
{
i = 0;
}
i++;
}
}
//range=sheet.get_Range(COleVariant(_T("A")+ (str.Format("%d",i))),COleVariant(_T("A1")));
//range.put_Value2(COleVariant(str3[i]));
//app.put_Visible(TRUE);
str1.Format(_T("e:\\%d%d%d%-d%d%d_InsData.xlsx"),tm.GetYear(),tm.GetMonth(),tm.GetDay(),tm.GetHour(),tm.GetMinute(),tm.GetSecond());
book.SaveCopyAs(COleVariant(str1)); //按保存时间命名保存数据文件,地址在E盘
book.put_Saved(TRUE);
books.Close();
app.Quit();
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
app.ReleaseDispatch();
AfxMessageBox(_T("数据保存成功!"));
Above is complete, click the Save button after you can save the data. You can make the appropriate changes according to their needs.
After saving effect as follows: