1. 在Vs2010里创建一个MFC工程
2、打开ClassWizard窗口(查看—>建立类向导),选择Automation,单击AddClass按钮,选择Froma type library...,弹出文件选择对话框,之后定位到
C:\Program Files\MicrosoftOffice\OFFICE14\EXCEL.EXE。
添加 CApplication
Cfont0
CRange
CWorkbook
CWorkbooks
CWorksheet
CWorksheets
并在每个头文件中添加#include <afxdisp.h>
//以下是本人在程序中初始化函数中对表格的初始化 工作
CApplication app; CWorkbooks books; CWorkbook book; CWorksheets sheets; CWorksheet sheet; CRange range; CRange cols; CFont0 font; LPDISPATCH lpDisp; HRESULT hr; hr = CoInitialize(NULL); //ASSERT(!FAILED(hr)); if(FAILED(hr)) { AfxMessageBox("Failed to call Coinitialize()"); } COleVariant covOptional((long) DISP_E_PARAMNOTFOUND,VT_ERROR); if (!app.CreateDispatch(_T("Excel.Application"))) { this->MessageBox(_T("无法创建Excel应用")); return; } try { books = app.get_Workbooks(); } catch (CException* e) { TCHAR szError[1024]; e->GetErrorMessage(szError,1024); // e.GetErrorMessage(szError,1024); ::AfxMessageBox(szError); } books = app.get_Workbooks(); lpDisp = books.Open(csFilename,covOptional ,covOptional,covOptional,covOptional ,covOptional,covOptional,covOptional ,covOptional,covOptional,covOptional ,covOptional,covOptional,covOptional ,covOptional); book=books.Add(covOptional); book.AttachDispatch(lpDisp); sheets = book.get_Sheets(); sheet = sheets.get_Item(COleVariant((short)1)); range.AttachDispatch(sheet.get_Cells(),TRUE);//加载所有单元格 font.AttachDispatch(range.get_Font()); font.put_Name(_variant_t(_T("宋体"))); font.put_Size(_variant_t(12)); // font.put_Color(_variant_t(RGB(255, 0, 0))); //设置对其方式 中间对其 range.put_HorizontalAlignment(_variant_t((long)-4108)); //设置行高 range.put_RowHeight(COleVariant((long)20)); //设置列宽 range=sheet.get_Range(COleVariant("A1"),COleVariant("A1")); range.put_ColumnWidth(_variant_t(10)); range=sheet.get_Range(COleVariant("B1"),COleVariant("B1")); range.put_ColumnWidth(_variant_t(30)); range=sheet.get_Range(COleVariant("C1"),COleVariant("C1")); range.put_ColumnWidth(_variant_t(10)); range=sheet.get_Range(COleVariant("D1"),COleVariant("D1")); range.put_ColumnWidth(_variant_t(30)); range=sheet.get_Range(COleVariant("E1"),COleVariant("E1")); range.put_ColumnWidth(_variant_t(15)); range=sheet.get_Range(COleVariant("F1"),COleVariant("F1")); range.put_ColumnWidth(_variant_t(15)); range=sheet.get_Range(COleVariant("G1"),COleVariant("G1")); range.put_ColumnWidth(_variant_t(30)); range=sheet.get_Range(COleVariant("H1"),COleVariant("H1")); range.put_ColumnWidth(_variant_t(30)); range=sheet.get_Range(COleVariant("I1"),COleVariant("I1")); range.put_ColumnWidth(_variant_t(20)); //保存并释放 book.Save(); app.Quit(); app.ReleaseDispatch(); book.ReleaseDispatch(); books.ReleaseDispatch(); sheet.ReleaseDispatch(); sheets.ReleaseDispatch(); range.ReleaseDispatch();
该函数在线程中调用,插入并更新数据
void CPackingDlg::Insert_Data1(int row) { CApplication app; CWorkbooks books; CWorkbook book; CWorksheets sheets; CWorksheet sheet; CRange range; CRange cols; CFont0 font; CString csFilename=""; Path = Cs_Zhiling + GongXu; GetCurrentPath(g_szWorkDir); csFilename.Format("%s\\%s.xls", g_szWorkDir,Path); LPDISPATCH lpDisp; HRESULT hr; hr = CoInitialize(NULL); //ASSERT(!FAILED(hr)); if(FAILED(hr)) { AfxMessageBox("Failed to call Coinitialize()"); MyListShowSystem("Failed to call Coinitialize()"); } COleVariant covOptional((long) DISP_E_PARAMNOTFOUND,VT_ERROR); if (!app.CreateDispatch(_T("Excel.Application"))) { this->MessageBox(_T("无法创建Excel应用")); MyListShowSystem("无法创建Excel应用"); return; } try { books = app.get_Workbooks(); } catch (CException* e) { TCHAR szError[1024]; e->GetErrorMessage(szError,1024); // e.GetErrorMessage(szError,1024); ::AfxMessageBox(szError); } // app.put_Visible(TRUE); // app.put_UserControl(TRUE); books = app.get_Workbooks(); lpDisp = books.Open(csFilename,covOptional ,covOptional,covOptional,covOptional ,covOptional,covOptional,covOptional ,covOptional,covOptional,covOptional ,covOptional,covOptional,covOptional ,covOptional); book=books.Add(covOptional); book.AttachDispatch(lpDisp); sheets = book.get_Sheets(); sheet = sheets.get_Item(COleVariant((short)1)); CString str= "",strA= "",strB= "",strC= "",strD= "",strE= "",strF= "",strG= "",strH= "",strI= "",temp = ""; str.Format("%d",row+1); strA = "A" + str; strB = "B" + str; strC = "C" + str; strD = "D" + str; strE = "E" + str; strF = "F" + str; strG = "G" + str; strH = "H" + str; strI = "I" + str; range=sheet.get_Range(COleVariant(strA),COleVariant(strA));//序号 temp.Format("%d",Infor_detail.XuHao); range.put_Value2(COleVariant(temp)); range=sheet.get_Range(COleVariant(strB),COleVariant(strB));//制令 range.put_Value2(COleVariant(Infor_detail.ZhiLing)); range=sheet.get_Range(COleVariant(strC),COleVariant(strC));//批次 temp.Format("%d",Infor_detail.PiCi); range.put_Value2(COleVariant(temp)); range=sheet.get_Range(COleVariant(strD),COleVariant(strD));//条形码 range.put_Value2(COleVariant(Infor_detail.Code)); range=sheet.get_Range(COleVariant(strE),COleVariant(strE));//产品序号 range.put_Value2(COleVariant(Infor_detail.product_XuHao)); range=sheet.get_Range(COleVariant(strF),COleVariant(strF));//工序 range.put_Value2(COleVariant(Infor_detail.GongXu)); range=sheet.get_Range(COleVariant(strG),COleVariant(strG));//入时间 range.put_Value2(COleVariant(Infor_detail.time_in)); book.Save(); app.Quit(); app.ReleaseDispatch(); book.ReleaseDispatch(); books.ReleaseDispatch(); sheet.ReleaseDispatch(); sheets.ReleaseDispatch(); range.ReleaseDispatch(); }