1. 程式人生 > >vc++ excel 寫入 與寫出資料

vc++ excel 寫入 與寫出資料

1、新增標頭檔案

#include "stdafx.h"

#include "testExcel.h"

#include "testExcelDlg.h"

#include "afxdialogex.h"

#include "CApplication.h"

#include "CRanges.h"

#include "CWorkbook.h"

#include "CWorkbooks.h"

#include "CWorksheet.h"

#include "CWorksheets.h"

#include "CRange.h"

 

 

2、初始化com元件

 

//初始化COM元件

if (!AfxOleInit())

{

AfxMessageBox(_T("無法初始化COM的動態連結庫"));

return FALSE;

}

 

3、關鍵部分

void CtestExcelDlg::OnBnClickedButtonWrite()

{

// TODO: Add your control notification handler code here

 

CApplication app;

//建立Excel伺服器(啟動Excel)

if (!app.CreateDispatch("Excel.Application"))

{

AfxMessageBox("無法啟動Excel伺服器");

 

return;

}

 

 

//設定Excel的狀態

//app.put_Visible(TRUE); //使Excel可見

app.put_UserControl(TRUE); //允許其他使用者控制Excel

 

 

//vc對Excel的操作

//定義變數

 

 

CWorkbooks books;

CWorkbook book;

CWorksheets sheets;

CWorksheet sheet;

CRange range;

 

LPDISPATCH lpDisp;

COleVariant vResult;

COleVariant covOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);

 

/*________________________

開啟已有的Excel檔案

 

books.AttachDispatch(app.get_Workbooks());

//books=app.get_Workbooks();

lpDisp=books.Open("D://1.xls",covOptional,covOptional,covOptional,covOptional,

covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,

covOptional,covOptional,covOptional,covOptional);

 

*/

 

 

 

//新建一個.xls檔案,並寫入資料

books.AttachDispatch(app.get_Workbooks());//得到Workbooks

book=books.Add(covOptional);//得到Workbook

 

//分別向每個單元格新增資料

//Excel2000用函式SetValue()即可,Excel2003用函式SetValue2()

sheets=book.get_Worksheets();

sheet=sheets.get_Item(COleVariant((short)1));

 

range=sheet.get_Range(COleVariant(_T("A1")),covOptional); //單個

range.put_Value2(COleVariant(_T("hanhan")));

m_excelTxt=range.get_Value2();

UpdateData(FALSE); //顯示在文字框中

range=sheet.get_Range(COleVariant(_T("B5")),COleVariant(_T("D17"))); //從B5到D17即從B第五行到D第17行矩形區域寫東西。

range.put_Value2(COleVariant(_T("Order"))); //新增資料

 

//range=sheet.get_Range(COleVariant("A1"),covOptional);;//獲取A1 range

//range.put_Value2(COleVariant(Date)); //新增資料

 

//range=sheet.get_Range(COleVariant("B1"),covOptional); //獲取B1 Range

//range.put_Value2(COleVariant("Order")); //新增資料

 

//range=sheet.get_Range(COleVariant("C1"),covOptional);

//range.put_Value2(COleVariant("Amount")); //新增資料

 

//range=sheet.get_Range(COleVariant("D1"),covOptional);//獲取D1 Range

//range.put_Value2(COleVariant("Tax")); //新增資料

 

// range.AutoFit(); //自動適合尺寸

 

 

 

CString strFile=_T("d:\\8.xlsx");

//將已建的xls檔案另存為

// COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

book.SaveAs(COleVariant(strFile),covOptional,covOptional,covOptional,covOptional,

covOptional,0,covOptional,covOptional,covOptional,covOptional,covOptional);

 

 

 

//關閉Excel服務

// COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);

book.put_Saved(TRUE); //將Workbook的儲存狀態設定為已儲存,即不讓系統提示是否人工儲存

range.ReleaseDispatch(); //釋放Range物件

sheet.ReleaseDispatch();//釋放sheet物件

sheets.ReleaseDispatch(); //釋放sheets物件

 

book.ReleaseDispatch();//釋放workbook物件

books.ReleaseDispatch(); //釋放workbooks物件

 

book.Close(covOptional,covOptional,covOptional);//關閉workbook物件

books.Close();

 

app.Quit();//退出application

app.ReleaseDispatch();//釋放application

}