在对话框头文件中导入头文件
#include "CApplication.h"
#include "CRange.h"
#include "CWorkbook.h"
#include "CWorkbooks.h"
#include "CWorksheet.h"
#include "CWorksheets.h"
#include "ShowData.h"
依次打开并注释这些头文件中如下代码
//#import "C:\\Program Files\\Microsoft Office\\root\\Office16\\EXCEL.EXE" no_namespace
打开CRange.h
将DialogBox改为_DialogBox
到这里可以成功运行项目了
//获取随机数
int data[100];
srand(time(0));
for(int i=0;i<100;i++)
{
data[i] = 10 + rand() % 200;
}
m_list.SetExtendedStyle(m_list.GetExtendedStyle()
| LVS_EX_GRIDLINES |LVS_EX_FULLROWSELECT |LVS_EX_DOUBLEBUFFER);
//插入标题
m_list.InsertColumn(0, TEXT("行/列"), LVCFMT_LEFT, 50);
for (int i = 1;i<11;i++)
{
CString str;
str.Format(TEXT("%d列"), i );
m_list.InsertColumn(i, str, LVCFMT_LEFT, 40);
}
int temp = 0;
//插入正文内容,先确定行,再确定列
for (int i = 0; i < 10; i++)
{
CString str;
str.Format(TEXT("第%d行"), i+1 );
//确定行
m_list.InsertItem(i, str);
}
//填入数据
CString value;
for (int i=0; i<10; i++)
{
for (int j=0;j<10; j++)
{
value.Format(TEXT("%d"), data[i*10+j]);
m_list.SetItemText(i, j+1, value);
}
}
在导出按钮的响应方法中添加将数据写入Excel的方法
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CApplication app; //创建应用实例
CWorkbooks books; //多个工作簿
CWorkbook book; //单个工作簿
CWorksheets sheets; //多个工作表
CWorksheet sheet; //单个工作表
CRange range;
//app.put_DisplayAlerts(VARIANT_FALSE); // 禁用 Excel 的警告对话框显示
if (!app.CreateDispatch(_T("Excel.Application")))
{
MessageBox(_T("创建失败!"));
return;
}
char path[MAX_PATH];
GetCurrentDirectory(MAX_PATH, (TCHAR*)path);//获取当前路径
CString strExcelFile = (TCHAR*)path;
CString strdevName = _T("\\data.xls"); //xlsx也行
strExcelFile += strdevName;
books = app.get_Workbooks();
book = books.Add(covOptional);//创建工作簿
sheets = book.get_Worksheets(); //得到此工作簿下所有的工作表对象
sheet = sheets.get_Item(COleVariant((short)1)); //获取第一个工作表对象
//得到全部Cells
range.AttachDispatch(sheet.get_Cells());
for (int setnum = 0; setnum < 10; setnum++) //GetItemCount()列表的里面的条目数量, 横着的 行
{
for (int num = 0; num < 10; num++)
{
range.put_Item(_variant_t((long)(setnum + 1)), _variant_t((long)(num + 1)),
_variant_t(m_list.GetItemText(setnum , num+1)));//GetItemText()获取第setnum行,num+1列的文本
}
}
//保存
book.SaveCopyAs(COleVariant(strExcelFile)); //SaveCopyAs在strFile路径下保存为指定格式.xlsx的文件
book.put_Saved(TRUE); //将Workbook的保存状态设置为已保存,即不让系统提示是否人工保存
app.put_Visible(TRUE); //设置表可见性
//释放对象
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
app.ReleaseDispatch();
app.Quit();
在资源视图中添加一个新的Dlg,给这个对话框添加一个类 CShowData,并添加一个list Control,用来显示从Excel中读出的数据
在原来的对话框头文件中创建ShowData对象,用来显示这个对话框。
CShowData showDataDlg;
在导出按钮的响应方法中添加读取Excel的方法,并将数据写入数组中,待会就可以将数组中的数据显示在列表里了(当然也可以读数据的同时将数据写入列表,不用数组存储
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
CApplication app;
CRange range;
COleVariant vResult;
if (!app.CreateDispatch(_T("Excel.Application")))
{
MessageBox(_T("创建失败!"));
return;
}
char path[MAX_PATH];
GetCurrentDirectory(MAX_PATH, (TCHAR*)path);//获取当前路径
CString strExcelFile = (TCHAR*)path;
CString strdevName = _T("\\data.xls"); //xls也行
strExcelFile += strdevName;
CWorkbooks books = app.get_Workbooks();
// 按文件路径打开现有的工作簿
CWorkbook book = books.Open(strExcelFile, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional);
CWorksheets sheets = book.get_Worksheets();
CWorksheet sheet = sheets.get_Item(COleVariant((short)1));
//获取有效行号
range = sheet.get_UsedRange();
range = range.get_Rows();
long rows = range.get_Count();
//行号加一可以得到这个电容对应的工作表,(m_Col-1)*3可以得到这个电容的数据
for (long i = 1; i < rows + 1; i++)
{
for (int j=1; j<11; j++)
{
range.AttachDispatch(sheet.get_Cells());
range.AttachDispatch(range.get_Item(COleVariant(i), COleVariant((long)j)).pdispVal); //第一变量是行,第二个变量是列,即第i行第j列
vResult = range.get_Value2();
showDataDlg.listData[(i-1)*10+(j-1)] = vResult.dblVal;
}
}
books.Close();
app.Quit();
//释放对象
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
app.ReleaseDispatch();
//渲染showData对话框
showDataDlg.DoModal();
在ShowData.cpp的OnInitDialog方法中将数据写入列表
m_list.SetExtendedStyle(m_list.GetExtendedStyle()
| LVS_EX_GRIDLINES |LVS_EX_FULLROWSELECT |LVS_EX_DOUBLEBUFFER);
//插入标题
m_list.InsertColumn(0, TEXT("行/列"), LVCFMT_LEFT, 50);
for (int i = 1;i<11;i++)
{
CString str;
str.Format(TEXT("%d列"), i );
m_list.InsertColumn(i, str, LVCFMT_LEFT, 40);
}
int temp = 0;
//插入正文内容,先确定行,再确定列
for (int i = 0; i < 10; i++)
{
CString str;
str.Format(TEXT("第%d行"), i+1 );
//确定行
m_list.InsertItem(i, str);
}
//填入数据
CString value;
for (int i=0; i<10; i++)
{
for (int j=0;j<10; j++)
{
value.Format(TEXT("%d"), listData[i*10+j]);
m_list.SetItemText(i, j+1, value);
}
}
OK了