本文是在MFC中操作excel,并将excel中的数据导进到listCtrl控件中,并在控件中进行增删改查操作,同时保存到excel表中。
新建MFC项目
应用程序添加,基于对话框模式,下一步,直到
一定要勾选自动化。因为后面需要加入一些需要的库,不然会出现错误
对话框
在对话框中加入list Control控件,在属性中修改为report类型,并添加变量m_list
添加一个 edit control 控件,之后要在列表控件中双击修改,添加变量m_edit
在类视图中找到Dlg类,再在下面找到OnInitDialog() 双击就能进入到cpp文件中位置,
设置控件的风格,首先获取当前的风格,然后整行选中,高亮显示,小盒子
插入表头
edit控件,我们并不希望看到它显示。
连接Excel预备工作
添加需要的类
类视图中右击->添加->类
如图选择添加
可用的类型库中,找到
每个人不一样的版本,找到就可
然后在下面的接口中添加
_Application 应用程序
Range 操作区域
_Workbook 工作簿
Workbooks 工作簿集
_Worksheet 工作表
Worksheets 工作表集
CShapes 插入图片时能用到
CShape
CShapeRange
Font 字体
Interior 底色
Borders 边框
将这些类的头文件添加到cpp实现文件中
双击这些类,进入这些类的头文件中,将头文件上面的#import “C:\\Program Files (x86)\Microsoft Office\\Root\\Office16\\EXCEL.EXE” no_namespace 删除
然后进行编译,会报错,在CRange.h找到该位置,将DialogBox()前加_即可
连接Excel
首先先添加一些按钮,进行事件的操作
双击读取数据按钮进入事件编辑中
//读取数据
void CMFCexcelDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
HRESULT hr; //用来判断
hr = CoInitialize(NULL); //初始化COM支持库
if (FAILED(hr))
{
MessageBox(_T("初始化失败"));
}
else
{
//MessageBox(_T("初始化成功"));
}
CApplication m_app; //应用程序对象
CWorkbooks books; //所有的工作簿
CWorkbook book; //工作簿
CWorksheets sheets; //所有的工作表
CWorksheet sheet; //工作表
CRange range; //单元格
LPDISPATCH lpDisp;
COleVariant vResult; //结果集
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE); //false读取一次后按钮变灰,true不变灰
//打开Excel应用程序
if (!m_app.CreateDispatch(_T("Excel.Application"),NULL))
{
MessageBox(_T("无法启动"));
return;
}
else
{
//MessageBox(_T("启动成功"));
}
CFileDialog filedlg(TRUE, L"*.xls ", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L"xlsx文件(*.xls)|*.xlsx"); //打开所有的excel文件
filedlg.m_ofn.lpstrTitle = L" 打开文件";
CString strFilepath;
if (IDOK == filedlg.DoModal())
{
strFilepath = filedlg.GetPathName(); //保存路径
SetWindowText(strFilepath);
delete filedlg;
}
else
{
return;
}
//打开路径下的xlsx文件
books.AttachDispatch(m_app.get_Workbooks()); //打开工作簿集
//book = books.Add(covOptional); 创建一个新的表
lpDisp = books.Open(strFilepath, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional); //打开路径下的工作簿
//得到工作簿
book.AttachDispatch(lpDisp);
//得到表
sheets.AttachDispatch(book.get_Worksheets());
//得到当前活跃的表,若有单元格正处于编辑状态,此操作不能反回
//lpDisp = book.get_ActiveSheet();
//sheet.AttachDispatch(lpDisp);
sheet.AttachDispatch(sheets.get_Item(_variant_t((short)(1)))); //获取第一个表
//m_app.put_Visible(TRUE); //表格可见
//m_app.put_UserControl(TRUE); //控制权限
range.AttachDispatch(sheet.get_UsedRange());
range.AttachDispatch(range.get_Rows());
long irownum = range.get_Count(); //已使用的行数
range.AttachDispatch(range.get_Columns());
long icolnum = range.get_Count(); //使用的列数
long istartrow = range.get_Row(); //起始行
long istartcol = range.get_Column(); //起始列
m_list.DeleteAllItems(); //删除列表项
for (int mrow = 2; mrow < irownum + 1; mrow++)
{
range.AttachDispatch(sheet.get_Cells()); //获取所有的单元格
range.AttachDispatch(range.get_Item(COleVariant((long)mrow), COleVariant((long)1)).pdispVal);//获取选定的单元格信息
vResult = range.get_Value2();
CString str0;
str0 = vResult.bstrVal;
m_list.InsertItem(mrow - 2, str0);
//表格中第一行是标题,所以我从第二行开始
for (int j = 2; j < icolnum + 1; j++)
{
range.AttachDispatch(sheet.get_Cells());
range.AttachDispatch(range.get_Item(COleVariant((long)mrow), COleVariant((long)j)).pdispVal);
vResult = range.get_Value2();
CString str;
if (vResult.vt == VT_BSTR) //字符串
{
str = vResult.bstrVal;
}
else if (vResult.vt == VT_R8)
{
str.Format(L"%.0f", vResult.dblVal);
}
else if (vResult.vt == VT_I4)
{
str.Format(_T("%ld"), (int)vResult.lVal);
}
m_list.SetItemText(mrow - 2, j - 1, str);
}
}
//books.Close(); //关闭工作簿
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
m_app.Quit(); //程序退出
//m_app.ReleaseDispatch();
```cpp
在这里插入代码片
读取修改
接下来是修改和增加,我直接在MFC中的控件中双击修改,当双击空白处自动增加一行
首先,需要双击能够修改,这就用到一开始的那个edit控件
在列表控件中找到属性->控件事件->NM_DBCLICK
UpdateData(true);
CRect rc;
CString strtemp;
NM_LISTVIEW *pNMListCtrl = (NM_LISTVIEW*)pNMHDR; //结构体保存双击的行号列号
m_row = pNMListCtrl->iItem;
m_col = pNMListCtrl->iSubItem;
if (pNMListCtrl->iItem == -1) //在头文件中定义m_row,m_col = -1,若为-1则在空白区域
{
m_row = m_list.GetItemCount();//获得控件项目的个数
strtemp.Format(_T("%d"), m_row + 1);//在个数下一行进行编辑
m_list.InsertItem(m_row, strtemp);//插入正文的第一列的内容
m_list.SetItemState(m_row, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);//设置行的状态
m_list.EnsureVisible(m_row, FALSE);//使列表视图项至少部分可视。如果必要,可以滚动列表视图控件,此处是项部分可视时,将不发生滚动
}
else
{
m_list.GetSubItemRect(m_row, m_col, LVIR_LABEL, rc);//获取整个单元格的矩形区域,最后一个参数包含子项绑定矩形坐标的CRect对象的参考CRect
m_edit.SetParent(&m_list); //包含子项绑定矩形坐标的CRect对象的参考
m_edit.MoveWindow(rc); //指定这个窗口的位置和大小
m_edit.SetWindowTextW(m_list.GetItemText(m_row, m_col)); //窗口为一个控件,获取列表视图项或子项的文本,设置为控件内的文本。
m_edit.ShowWindow(SW_SHOW); //窗口可见
m_edit.SetFocus();//设置edit焦点,这个函数要求得到输入焦点。输入焦点将随后的所有键盘输入定向到这个窗口
m_edit.ShowCaret();//这个函数在屏幕上当前插字符位置显示插字符
m_edit.SetSel(0, -1);//单元格里的数据全选中
}
再点击一开始的edit控件->控件事件->EN_KILLFOCUS()
CString str;
if (m_col == 1)
{
m_edit.GetWindowTextW(str); //获取edit控件里的内容
CString str1 = _T("男");
CString str2 = _T("女");
if (str == str1 || str ==str2)
{
m_list.SetItemText(m_row, m_col, str);//往列表里设置内容
}
else
{
MessageBox(_T("请重新输入"));
return;
}
}
else if (m_col == 2)
{
m_edit.GetWindowTextW(str); //获取edit控件里的内容
int numz = _tstoi(str);
CString strv;
strv.Format(_T("%d"), numz);
if (str != strv)
{
MessageBox(_T("请输入数字"));
return;
}
m_list.SetItemText(m_row, m_col, strv);//往列表里设置内容
}
else if (m_col == 0)
{
m_edit.GetWindowTextW(str); //获取edit控件里的内容
m_list.SetItemText(m_row, m_col, str);//往列表里设置内容
}
m_edit.ShowWindow(SW_HIDE); //隐藏edit控件
HRESULT hr;
hr = CoInitialize(NULL); //初始化COM支持库
if (FAILED(hr))
{
MessageBox(_T("初始化失败"));
}
else
{
//MessageBox(_T("初始化成功"));
}
CApplication m_app; //应用程序对象
CWorkbooks books; //所有的工作簿
CWorkbook book; //工作簿
CWorksheets sheets; //所有的工作表
CWorksheet sheet; //工作表
CRange range; //单元格
LPDISPATCH lpDisp;
COleVariant vResult;
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE); //false读取一次后按钮变灰,true不变灰
//打开Excel应用程序
if (!m_app.CreateDispatch(_T("Excel.Application"), NULL))
{
MessageBox(_T("无法启动"));
return;
}
else
{
//MessageBox(_T("启动成功"));
}
books.AttachDispatch(m_app.get_Workbooks());
//book = books.Add(covOptional); 创建一个新的表
CString efile = _T("D:\\Excel\\Test.xlsx");
lpDisp = books.Open(efile, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional);
//得到工作簿
book.AttachDispatch(lpDisp);
//得到表
sheets.AttachDispatch(book.get_Worksheets());
//sheet = sheets.get_Item(COleVariant(short)1);
//得到当前活跃的表,若有单元格正处于编辑状态,此操作不能反回
lpDisp = book.get_ActiveSheet();
sheet.AttachDispatch(lpDisp);
m_app.put_Visible(FALSE); //表格可见
m_app.put_UserControl(TRUE); //控制权限
range.AttachDispatch(sheet.get_UsedRange());
range.AttachDispatch(range.get_Rows());
long irownum = range.get_Count(); //已使用的行数
range.AttachDispatch(range.get_Columns());
long icolnum = range.get_Count(); //使用的列数
long istartrow = range.get_Row(); //起始行
long istartcol = range.get_Column(); //起始列
range.AttachDispatch(sheet.get_Cells());
range.put_Item(_variant_t((long)(m_row + 2)), _variant_t((long)(m_col + 1)), _variant_t(str));
book.Save();
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
m_app.Quit();
m_app.ReleaseDispatch();
删除
int delnum; //接收要删除的行号
int getcunt = m_list.GetItemCount();
for (int i = 0; i < getcunt; i++)
{
if (m_list.GetItemState(i, LVIS_SELECTED) == LVIS_SELECTED)
{
delnum = i;
m_list.DeleteItem(i); //控件中删除数据
}
}
//excel中删除数据并保存
HRESULT hr;
hr = CoInitialize(NULL); //初始化COM支持库
if (FAILED(hr))
{
MessageBox(_T("初始化失败"));
}
else
{
//MessageBox(_T("初始化成功"));
}
CApplication m_app; //应用程序对象
CWorkbooks books; //所有的工作簿
CWorkbook book; //工作簿
CWorksheets sheets; //所有的工作表
CWorksheet sheet; //工作表
CRange range; //单元格
LPDISPATCH lpDisp;
COleVariant vResult;
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE); //false读取一次后按钮变灰,true不变灰
//打开Excel应用程序
if (!m_app.CreateDispatch(_T("Excel.Application"), NULL))
{
MessageBox(_T("无法启动"));
return;
}
else
{
//MessageBox(_T("启动成功"));
}
books.AttachDispatch(m_app.get_Workbooks());
//book = books.Add(covOptional); 创建一个新的表
CString efile = _T("D:\\Excel\\Test.xlsx");
lpDisp = books.Open(efile, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional);
//得到工作簿
book.AttachDispatch(lpDisp);
//得到表
sheets.AttachDispatch(book.get_Worksheets());
//sheet = sheets.get_Item(COleVariant(short)1);
//得到当前活跃的表,若有单元格正处于编辑状态,此操作不能反回
lpDisp = book.get_ActiveSheet();
sheet.AttachDispatch(lpDisp);
m_app.put_Visible(FALSE); //表格可见
m_app.put_UserControl(TRUE); //控制权限
range.AttachDispatch(sheet.get_UsedRange());
range.AttachDispatch(range.get_Rows());
long irownum = range.get_Count(); //已使用的行数
range.AttachDispatch(range.get_Columns());
long icolnum = range.get_Count(); //使用的列数
long istartrow = range.get_Row(); //起始行
long istartcol = range.get_Column(); //起始列
range.AttachDispatch(sheet.get_Cells());
CString startcell;
CString endcell;
startcell.Format(_T("A%d"), (delnum + 2));
endcell.Format(_T("C%d"),(delnum + 2));
range.AttachDispatch(sheet.get_Range(COleVariant(startcell), COleVariant(endcell)));
range.Delete(COleVariant((long)(-4162))); //删除后表格往上提
book.Save();
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
//m_app.Quit();
m_app.ReleaseDispatch();
模糊查找
需要一个edit控件输入需要查找的内容,本文用的是模糊查找
edit控件添加变量 m_edit1
UpdateData(TRUE); //更新到变量里
CString stredit;
m_edit1.GetWindowTextW(stredit);
HRESULT hr;
hr = CoInitialize(NULL); //初始化COM支持库
if (FAILED(hr))
{
MessageBox(_T("创建失败"));
}
else
{
//return;
//MessageBox(_T("创建成功"));
}
CFileDialog filedlg(TRUE, L"*.xls", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L"xls文件(*.xlsx)|*.xlsx");
filedlg.m_ofn.lpstrTitle = L" 打开文件";
CString strFilepath;
if (IDOK == filedlg.DoModal())
{
strFilepath = filedlg.GetPathName();
SetWindowText(strFilepath);
delete filedlg;
}
else
{
return;
}
CApplication m_app;
CWorkbooks books; //所有的工作簿
CWorkbook book; //工作簿
CWorksheets sheets; //所有的工作表
CWorksheet sheet; //工作表
CRange range; //单元格
CRange icell;
LPDISPATCH lpDisp;
COleVariant vResult;
COleVariant
covTrue((short)TRUE),
covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE); //false读取一次后按钮变灰
//打开Excel应用程序
if (!m_app.CreateDispatch(_T("Excel.Application"), NULL))
{
MessageBox(_T("无法启动"));
return;
}
else
{
//return;
//MessageBox(_T("启动成功"));
}
//打开路径下的xls文件
books.AttachDispatch(m_app.get_Workbooks());
lpDisp = books.Open(strFilepath, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional);
//得到工作簿
book.AttachDispatch(lpDisp);
//得到表
sheets.AttachDispatch(book.get_Worksheets());
//sheet = sheets.get_Item(COleVariant(short)1);
//得到当前活跃的表,若有单元格正处于编辑状态,此操作不能反回
lpDisp = book.get_ActiveSheet();
sheet.AttachDispatch(lpDisp);
//m_app.put_Visible(TRUE); //表格可见
//m_app.put_UserControl(TRUE);
range.AttachDispatch(sheet.get_UsedRange());
range.AttachDispatch(range.get_Rows());
long irownum = range.get_Count(); //已使用的行数
range.AttachDispatch(range.get_Columns());
long icolnum = range.get_Count(); //使用的列数
long istartrow = range.get_Row(); //起始行
long istartcol = range.get_Column(); //起始列
int cunt = 0;
m_list.DeleteAllItems();
for (int i = 2; i < irownum + 1; i++)
{
range.AttachDispatch(sheet.get_Cells()); //获取所有的单元格
range.AttachDispatch(range.get_Item(COleVariant((long)i), COleVariant((long)1)).pdispVal);//获取选定的单元格信息
vResult = range.get_Value2();
CString str;
if (vResult.vt == VT_BSTR) //字符串
{
str = vResult.bstrVal;
}
else if (vResult.vt == VT_R8)
{
str.Format(L"%.0f", vResult.dblVal);
}
else if (vResult.vt == VT_I4)
{
str.Format(_T("%ld"), (int)vResult.lVal);
}
if (str.Find(stredit) >= 0)
{
cunt++;
//MessageBox(_T("存在信息"));
range.AttachDispatch(sheet.get_Cells()); //获取所有的单元格
range.AttachDispatch(range.get_Item(COleVariant((long)i), COleVariant((long)2)).pdispVal);//获取选定的单元格信息
vResult = range.get_Value2();
CString str1;
if (vResult.vt == VT_BSTR) //字符串
{
str1 = vResult.bstrVal;
}
else if (vResult.vt == VT_R8)
{
str1.Format(L"%.0f", vResult.dblVal);
}
else if (vResult.vt == VT_I4)
{
str1.Format(_T("%ld"), (int)vResult.lVal);
}
range.AttachDispatch(sheet.get_Cells()); //获取所有的单元格
range.AttachDispatch(range.get_Item(COleVariant((long)i), COleVariant((long)3)).pdispVal);//获取选定的单元格信息
vResult = range.get_Value2();
CString str2;
if (vResult.vt == VT_BSTR) //字符串
{
str2 = vResult.bstrVal;
}
else if (vResult.vt == VT_R8)
{
str2.Format(L"%.0f", vResult.dblVal);
}
else if (vResult.vt == VT_I4)
{
str2.Format(_T("%ld"), (int)vResult.lVal);
}
m_list.InsertItem((cunt -1), str);
m_list.SetItemText((cunt - 1), 1, str1);
m_list.SetItemText((cunt - 1), 2, str2);
}
}
if (cunt == 0)
{
MessageBox(_T("信息不存在"));
m_edit1.SetSel(0, -1); //全选
m_edit1.Clear(); //清空
}
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
m_app.Quit();
m_app.ReleaseDispatch();
插入图片,边框,字体,背景色
{
// TODO: 在此添加控件通知处理程序代码
HRESULT hr;
hr = CoInitialize(NULL); //初始化COM支持库
if (FAILED(hr))
{
MessageBox(_T("创建失败"));
}
else
{
//MessageBox(_T("创建成功"));
}
CFileDialog filedlg(TRUE, L"*.xls ", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L"xlsx文件(*.xls)|*.xlsx");
filedlg.m_ofn.lpstrTitle = L" 打开文件";
CString strFilepath;
if (IDOK == filedlg.DoModal())
{
strFilepath = filedlg.GetPathName();
SetWindowText(strFilepath);
delete filedlg;
}
else
{
return;
}
CApplication m_app;
CWorkbooks books; //所有的工作簿
CWorkbook book; //工作簿
CWorksheets sheets; //所有的工作表
CWorksheet sheet; //工作表
CRange range; //单元格quyu
CRange icell;
CShapes shapes;
CShape shape;
LPDISPATCH lpDisp;
COleVariant vResult;
COleVariant
//covTrue((short)TRUE),
//covFalse((short)FALSE),
covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
GetDlgItem(IDC_BUTTON1)->EnableWindow(TRUE); //false读取一次后按钮变灰
//打开Excel应用程序
if (!m_app.CreateDispatch(_T("Excel.Application"), NULL))
{
MessageBox(_T("无法启动"));
return;
}
else
{
//MessageBox(_T("启动成功"));
}
//打开路径下的xls文件
books.AttachDispatch(m_app.get_Workbooks());
lpDisp = books.Open(strFilepath, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional,
covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional, covOptional);
//得到工作簿
book.AttachDispatch(lpDisp);
//得到表
sheets.AttachDispatch(book.get_Worksheets());
//sheet = sheets.get_Item(COleVariant(short)1);
//得到当前活跃的表,若有单元格正处于编辑状态,此操作不能反回
lpDisp = book.get_ActiveSheet();
sheet.AttachDispatch(lpDisp);
m_app.put_Visible(TRUE); //表格可见
m_app.put_UserControl(TRUE); //控制权限
/*
CRange usedRange;
usedRange.AttachDispatch(sheet.get_UsedRange());
range.AttachDispatch(usedRange.get_Rows());
long irowNUm = range.get_Count();
//已使用的行数
range.AttachDispatch(usedRange.get_Columns());
long iColNum = range.get_Count();
//已使用的列数
long iStartRow = usedRange.get_Row();
*/
range.AttachDispatch(sheet.get_UsedRange());
range.AttachDispatch(range.get_Rows());
long irownum = range.get_Count(); //已使用的行数
range.AttachDispatch(range.get_Columns());
long icolnum = range.get_Count(); //使用的列数
long istartrow = range.get_Row(); //起始行
long istartcol = range.get_Column(); //起始列
CFileDialog pfiledlg(TRUE, L"*.jpg", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, L"jpg文件(*.jpg)");
pfiledlg.m_ofn.lpstrTitle = L" 打开文件";
CString pFilepath;
if (IDOK == pfiledlg.DoModal())
{
pFilepath = pfiledlg.GetPathName(); //保存路径
SetWindowText(pFilepath);
delete pfiledlg;
}
else
{
return;
}
CShapes pshapes;
pshapes.AttachDispatch(sheet.get_Shapes());
range = sheet.get_Range(_variant_t("F5"), _variant_t(L"H8"));
//range = sheet.get_Range(COleVariant(L"F5"), COleVariant(L"H8"));
VARIANT rleft = range.get_Left();
VARIANT rtop = range.get_Top();
VARIANT rwidth = range.get_Width();
VARIANT rheight = range.get_Height();
CShape pshape;
//CString pfile = _T("F:\\p.jpg");
pshape = pshapes.AddPicture(pFilepath, false, true, (float)range.get_Left().dblVal, (float)range.get_Top().dblVal,
(float)range.get_Width().dblVal, (float)range.get_Height().dblVal);
CShapeRange pshaperange;
pshaperange = pshapes.get_Range(_variant_t((long)(1)));
pshaperange.put_Height((float)80);
pshaperange.put_Width((float)120);
CFont0 ft;
//CRange range;
range.AttachDispatch(sheet.get_Range(_variant_t("A2"), _variant_t("C2")), true);
ft.AttachDispatch(range.get_Font());
ft.put_Name(_variant_t("黑体")); //字体
ft.put_Size(_variant_t((long)14)); //字体大小
ft.put_Color(_variant_t((long)RGB(255, 6, 128))); //字体颜色
//range.put_Item(_variant_t((long)10), _variant_t((long)1), _variant_t("嘻嘻嘻"));
Cnterior0 it; //底色设置
range.AttachDispatch(sheet.get_Range(_variant_t("A2"), _variant_t("C2")), true);
it.AttachDispatch(range.get_Interior());
it.put_ColorIndex(_variant_t((long)20));
CBorders borders;
//range.AttachDispatch(sheet.get_Cells());
range.AttachDispatch(sheet.get_Range(_variant_t("A2"), _variant_t("C5")), true);
borders = range.get_Borders();
borders.put_ColorIndex(_variant_t((long)44)); //线的颜色,-4105位自动,1位黑色
borders.put_LineStyle(_variant_t((long)3)); // 线型
borders.put_Weight(_variant_t((long)3)); //线宽
range.AttachDispatch(sheet.get_Cells());
range.put_HorizontalAlignment(_variant_t((long)-4108)); //-4108居中,-4131居左,-4152居右
range.put_VerticalAlignment(_variant_t((long)-4108)); //-4160靠上,-4107靠下
//MessageBox(_T("成功"));
range.ReleaseDispatch();
sheet.ReleaseDispatch();
sheets.ReleaseDispatch();
book.ReleaseDispatch();
books.ReleaseDispatch();
m_app.Quit();
m_app.ReleaseDispatch();
}