这篇文章是接上一篇 VC++操作Excel在VS2008配置过程后的编码实例,详细给出了操作EXCEL的操作过程,大家可以举一反三,实现自己的功能。
gsExcel::CApplication m_ExlApp;
gsExcel::CRange m_ExlRange;
gsExcel::CWorkbook m_ExlWorkBook;
gsExcel::CWorkbooks m_ExlWorkBooks;
gsExcel::CWorksheet m_ExlWorkSheet;
gsExcel::CWorksheets m_ExlWorkSheets;
gsExcel::CRange m_ExlColor;
gsExcel::CFont0 m_ExlFont;
CString strTheAppPath;
CString strTheAppName;
TCHAR szAppPath[MAX_PATH];
GetModuleFileName(NULL, szAppPath, MAX_PATH);
strTheAppPath = CString(szAppPath);
strTheAppName = AfxGetApp()->m_pszAppName;
strTheAppName += _T(".exe");
int iLengthOne = strTheAppPath.GetLength();
int iLengthTwo = strTheAppName.GetLength();
strTheAppPath.Delete(iLengthOne - iLengthTwo, iLengthTwo);
CString strTemplatePath;
strTemplatePath += _T("TemplateOne.xlsx");
strTemplatePath = strTheAppPath + strTemplatePath;
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
// 启动EXCEL应用程序
if(!m_ExlApp.CreateDispatch(_T("Excel.Application")))
{
MessageBox(_T("创建Excel服务失败...."),_T("信息提示"), MB_OK);
return;
}
// 设置为可见
m_ExlApp.put_Visible(FALSE);
// 获取工作簿集合
m_ExlWorkBooks = m_ExlApp.get_Workbooks();
// 在工作簿集合中添加一个工作簿
m_ExlWorkBook = m_ExlWorkBooks.Add(covOptional);
m_ExlWorkSheets = m_ExlWorkBook.get_Sheets();
m_ExlWorkSheet = m_ExlWorkSheets.get_Item(_variant_t((short)2));
m_ExlRange=m_ExlWorkSheet.get_Range(_variant_t(_T("A1")),_variant_t(_T("A1")));
m_ExlRange.put_Value2(_variant_t("HELLO EXCEL!"));
m_ExlRange=m_ExlWorkSheet.get_Range(_variant_t("A2"),_variant_t("A2"));
m_ExlRange.put_Formula(_variant_t("=RAND()*100000"));
m_ExlRange.put_NumberFormat(_variant_t("$0.00"));
m_ExlRange=m_ExlWorkSheet.get_Range(_variant_t("A3"),_variant_t("A3"));
m_ExlRange.put_Value2(_variant_t(5));
m_ExlColor=m_ExlRange.get_EntireColumn();
m_ExlColor.AutoFit();
// 获取第一个表单
m_ExlWorkSheet = m_ExlWorkSheets.get_Item(_variant_t((short)1));
m_ExlRange = m_ExlWorkSheet.get_Range(_variant_t(_T("A1")), _variant_t(_T("A1")));
m_ExlRange.put_Value2(_variant_t(_T("Date")));
m_ExlRange = m_ExlWorkSheet.get_Range(_variant_t(_T("B1")), _variant_t(_T("B1")));
m_ExlRange.put_Value2(_variant_t(_T("Order")));
m_ExlRange = m_ExlWorkSheet.get_Range(_variant_t(_T("C1")), _variant_t(_T("C1")));
m_ExlRange.put_Value2(_variant_t(_T("Amount")));
m_ExlRange = m_ExlWorkSheet.get_Range(_variant_t(_T("D1")), _variant_t(_T("D1")));
m_ExlRange.put_Value2(_variant_t(_T("Tax")));
// 向单元格中添加公式
m_ExlRange = m_ExlWorkSheet.get_Range(COleVariant(_T("D2")), covOptional); // 获得D2 Range
m_ExlRange = m_ExlRange.get_Resize(COleVariant((long)1), COleVariant((long)1)); // 重新设置D2的大小
m_ExlRange.put_Formula(COleVariant(_T("=C2*0.07"))); // 给D2:D21设置公式
// 设置单元格的格式
m_ExlRange = m_ExlWorkSheet.get_Range(COleVariant(_T("A1")), COleVariant(_T("D1"))); // 获得A1:D1的Range
m_ExlFont = m_ExlRange.get_Font(); // 获得Range的字体
m_ExlFont.put_Bold(COleVariant((short)TRUE)); // 设置是否粗体
m_ExlFont.put_Color(COleVariant((long)RGB(255, 0, 0))); // 设置字体颜色
m_ExlFont.put_Name(COleVariant(_T("黑体"))); // 设置字体类型
m_ExlColor = m_ExlRange.get_EntireColumn(); // 获得全部的单元格
m_ExlColor.AutoFit();
//3、 合并单元格
// 思路:1.先获取A1:C1的Range范围,然后重新定义此范围,最后合并
// 2.直接获得A1:C2的Range范围,直接合并。结果和第一种方法一样
gsExcel::CRange unionRange;
unionRange = m_ExlWorkSheet.get_Range(COleVariant(_T("A2")), COleVariant(_T("C2")));
VARIANT vResult = unionRange.get_MergeCells();
unionRange = unionRange.get_Resize(COleVariant((long)2), COleVariant((long)3));
unionRange.Merge(COleVariant((long)0)); //合并单元格
unionRange.put_RowHeight(COleVariant((short)30)); //设置单元格的高度
unionRange.put_HorizontalAlignment(COleVariant((long)-4108));// 水平居中对齐 // 自动适合尺寸
//4、向单元格中插入图片(支持BMP、JPG格式,其他没试)
gsExcel::CShapes shapes = m_ExlWorkSheet.get_Shapes(); // 从Sheet对象上获得一个Shapes
m_ExlRange = m_ExlWorkSheet.get_Range(COleVariant(_T("B16")),COleVariant(_T("G22"))); // 获得Range对象,用来插入图片
gsExcel::CRange rgMyRge1;
rgMyRge1 = m_ExlRange;
strTheAppPath += _T("001.jpg");
shapes.AddPicture(strTheAppPath , false , true ,
(float)m_ExlRange .get_Left().dblVal, (float)m_ExlRange .get_Top().dblVal, // 从本地添加一个图片
(float)m_ExlRange .get_Width().dblVal, (float)m_ExlRange .get_Height().dblVal);
gsExcel::CShapeRange sRange = shapes.get_Range(_variant_t(long(1)));
sRange.put_Height(float(30));
sRange.put_Width(float(30));
//m_ExlApp.put_Visible(TRUE);
//m_ExlApp.put_UserControl(TRUE);
//5、将已建的.xls文件另存为
m_ExlWorkBook.SaveAs(COleVariant(strTemplatePath),covOptional,covOptional,
covOptional,covOptional,covOptional,0,
covOptional,covOptional,covOptional,covOptional,covOptional);
//6、关闭Excel服务
m_ExlWorkBook.put_Saved(TRUE); // 将Workbook的保存状态设置为已保存,即不让系统提示是否人工保存
//m_ExlRange.ReleaseDispatch(); // 释放Range对象
//m_ExlColor.ReleaseDispatch();
//m_ExlWorkSheet.ReleaseDispatch(); // 释放Sheet对象
//m_ExlWorkSheets.ReleaseDispatch(); // 释放Sheets对象
//m_ExlWorkBook.ReleaseDispatch(); // 释放Workbook对象
//m_ExlWorkBooks.ReleaseDispatch(); // 释放Workbooks对象
m_ExlWorkBook.Close (covOptional, covOptional,covOptional);// 关闭Workbook对象
m_ExlWorkBooks.Close(); // 关闭Workbooks对象
m_ExlApp.Quit(); // 退出_Application
//m_ExlApp.ReleaseDispatch (); // 释放_Application
欢迎一起交流提高,QQ:1906733710,欢迎来踩我哦:http://www.csto.com/my/info/edit