⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exceltestdlg.cpp.bak

📁 本程序提供了一种与Excel通讯的一种方法。
💻 BAK
字号:
// exceltestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "exceltest.h"
#include "exceltestDlg.h"
#include "../abaoexcel.h"
#include "excel.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

	// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
	//}}AFX_VIRTUAL

	// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT

}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExceltestDlg dialog

CExceltestDlg::CExceltestDlg(CWnd* pParent /*=NULL*/) : CDialog(CExceltestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CExceltestDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CExceltestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CExceltestDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CExceltestDlg, CDialog)
	//{{AFX_MSG_MAP(CExceltestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CExceltestDlg message handlers

BOOL CExceltestDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CExceltestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CExceltestDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CExceltestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CExceltestDlg::OnButton1()
{
	// TODO: Add your control notification handler code here
	AbaoExcel a;
	a.Add();
	a.SetVisible(true);
	a.Copy(a.SelectSheet(1));
	a.ActiveSheet().SetName("heheok");
	a.SelectSheet("sheet1");
	a.SetCell(1, 1, 3.14159267, 6);
	long t = 1000;
	a.SetCell(1, 1, t);
	a.SetCell(1, 3, CString("CString"));
	a.SetCell(1, 4, "C Style string");
	AbaoRange b(a.GetRange("A1:B1"));
	b.Merge();
	b.Border();
	b.SetHAlign(HAlignCenter);
	b.SetVAlign(VAlignTop);
	b = a.GetRange("B2");
	b.Border();
	Range r;
	r.SetColumnWidth(_variant_t((long) 800));
	b = "123456";
	b = a.GetRange("C2");
	b = CString("CString");
	b = a.GetRange("D2");
	b = "C Style string";
	a.SelectSheet("heheok");
	a.SetCell(1, 1, "内容都在sheet1里面呢");
	a.SaveAs(CString("c:\\test.xls"));
}

void CExceltestDlg::OnButton2()
{
	// TODO: Add your control notification handler code here
	_Application ExcelApp; 
	Workbooks wbsMyBooks; 
	_Workbook wbMyBook; 
	Worksheets wssMysheets; 
	_Worksheet wsMysheet; 
	Range rgMyRge; 
	COleVariant covOptional(DISP_E_PARAMNOTFOUND, VT_ERROR);

	//创建Excel 2003服务器(启动Excel) 
	if (!ExcelApp.CreateDispatch("Excel.Application", NULL))
	{
		AfxMessageBox("创建Excel服务失败!"); 
		exit(1);
	} 

	//利用模板文件建立新文档	 
	wbsMyBooks = ExcelApp.GetWorkbooks();     
	wbMyBook = wbsMyBooks.Open("C:\\test.xls", covOptional, covOptional,
							covOptional, covOptional, covOptional,
							covOptional, covOptional, covOptional,
							covOptional, covOptional, covOptional, covOptional)   ; 	

	//得到Worksheets	 
	wssMysheets = wbMyBook.GetWorksheets(); 	

	//得到sheet1	 
	wsMysheet = wssMysheets.GetItem(_variant_t((long) 1));     
	//得到全部Cells,此时,rgMyRge是cells的集合     
	rgMyRge = wsMysheet.GetCells();

	for (long i = 1; i < 10; i++)
	{
		for (long j = 0; j < 20; j++)
		{
			rgMyRge = wsMysheet.GetRange(COleVariant("A1"), covOptional);  
			COleVariant w = rgMyRge.GetColumnWidth();
			int ww = w.lVal;
			rgMyRge = rgMyRge.GetOffset(COleVariant(i), COleVariant(j));   
			rgMyRge.SetValue(COleVariant("d1"));
		}
	}

	ExcelApp.SetVisible(true);   
	wsMysheet.SetName("tt");   
	ExcelApp.SetUserControl(false);
	//	wbMyBook.Save();   

	LPDISPATCH lpDisp = wbMyBook.GetActiveSheet();
	wsMysheet.AttachDispatch(lpDisp); 

	Range objTotal, objCols, objRows;

	objTotal.AttachDispatch(wsMysheet.GetUsedRange());
	objCols.AttachDispatch(objTotal.GetColumns());
	objRows.AttachDispatch(objTotal.GetRows());


	int NumRows = objRows.GetCount();
	int NumCols = objCols.GetCount();

	/*	//利用模板文件建立新文档 
		wbsMyBooks.AttachDispatch(ExcelApp.GetWorkbooks(),true); 
	//	wbMyBook.AttachDispatch(wbsMyBooks.Add(_variant_t("C:\\test.xls")));
		wbMyBook.AttachDispatch(wbsMyBooks.Open("C:\\百42.xls", covOptional, covOptional, covOptional, covOptional, covOptional,covOptional, covOptional, covOptional, covOptional, covOptional,covOptional, covOptional));
		//得到Worksheets 
		wssMysheets.AttachDispatch(wbMyBook.GetWorksheets(),true);
		//得到sheet1 
	//	wsMysheet.AttachDispatch(wssMysheets.GetItem(_variant_t("sheet1")),true); 
		wsMysheet.AttachDispatch(wbMyBook.GetActiveSheet(),true);
		//得到全部Cells,此时,rgMyRge是cells的集合 
		rgMyRge.AttachDispatch(wsMysheet.GetCells(),true); 
		//设置1行1列的单元的值 
		rgMyRge.SetItem(_variant_t((long)1),_variant_t((long)6),_variant_t("This Is A Excel Test Program!")); 
		//得到所有的列 
		rgMyRge.AttachDispatch(wsMysheet.GetColumns(),true); 
		//得到第一列 
		rgMyRge.AttachDispatch(rgMyRge.GetItem(_variant_t((long)1),vtMissing).pdispVal,true); 
		//设置列宽 
		rgMyRge.SetColumnWidth(_variant_t((long)20)); 
		//调用模板中预先存放的宏 
		ExcelApp.Run(_variant_t("CopyRow"),_variant_t((long)10),vtMissing,vtMissing, 
		vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing, 
		vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing, 
		vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing, 
		vtMissing,vtMissing,vtMissing,vtMissing,vtMissing,vtMissing); 
		
		//打印预览 
		wbMyBook.SetSaved(true); 
		ExcelApp.SetVisible(true); 
	//	wbMyBook.PrintPreview(_variant_t(false)); 
		//释放对象
	*/
	rgMyRge.ReleaseDispatch(); 
	wsMysheet.ReleaseDispatch(); 
	wssMysheets.ReleaseDispatch();
	wbMyBook.ReleaseDispatch();
	wbsMyBooks.ReleaseDispatch();
	ExcelApp.ReleaseDispatch();
}

void CExceltestDlg::OnButton3()
{
	// TODO: Add your control notification handler code here
	//变量定义
	_Application app;    
	Workbooks books;
	_Workbook book;
	Worksheets sheets;
	_Worksheet sheet;
	Range range;
	Range iCell;
	LPDISPATCH lpDisp;    
	COleVariant vResult;
	COleVariant covTrue((short) TRUE), covFalse((short) FALSE), covOptional((long) DISP_E_PARAMNOTFOUND,
		VT_ERROR);    

	//*****
	//创建Excel 2003服务器(启动Excel)
	if (!app.CreateDispatch(_T("Excel.Application")))
	{
		AfxMessageBox("无法启动Excel服务器!");
		return;
	}

	app.SetVisible(TRUE);   	   //使Excel可见

	//*****   
	//打开c:\\test.xls
	books.AttachDispatch(app.GetWorkbooks());
	lpDisp = books.Open("C:\\test.xls", covOptional, covOptional, covOptional,
					covOptional, covOptional, covOptional, covOptional,
					covOptional, covOptional, covOptional, covOptional,
					covOptional);	


	//*****
	//得到Workbook
	book.AttachDispatch(lpDisp);

	//*****
	//得到Worksheets 
	sheets.AttachDispatch(book.GetWorksheets()); 


	//*****
	//得到当前活跃sheet
	//如果有单元格正处于编辑状态中,此操作不能返回,会一直等待
	lpDisp = book.GetActiveSheet();
	sheet.AttachDispatch(lpDisp); 

	//*****
	//读取已经使用区域的信息,包括已经使用的行数、列数、起始行、起始列
	Range usedRange;
	usedRange.AttachDispatch(sheet.GetUsedRange());
	range.AttachDispatch(usedRange.GetRows());
	long iRowNum = range.GetCount();				   //已经使用的行数

	range.AttachDispatch(usedRange.GetColumns());
	long iColNum = range.GetCount();				   //已经使用的列数

	long iStartRow = usedRange.GetRow();			   //已使用区域的起始行,从1开始
	long iStartCol = usedRange.GetColumn(); 		   //已使用区域的起始列,从1开始

	//*****
	//读取第一个单元格的值
	range.AttachDispatch(sheet.GetCells()); 
	range.AttachDispatch(range.GetItem(COleVariant((long) 2),
								COleVariant((long) 13)).pdispVal);
	vResult = range.GetValue();

	CString str;
	if (vResult.vt == VT_BSTR)  	 //字符串
	{
		str = vResult.bstrVal;
	}
	else if (vResult.vt == VT_R8)     //8字节的数字
	{
		str.Format("%f", vResult.dblVal);
	}
	else if (vResult.vt == VT_DATE)    //时间格式
	{
		SYSTEMTIME st;
		VariantTimeToSystemTime(vResult.date, &st);
	}
	else if (vResult.vt == VT_EMPTY)   //单元格空的
	{
		str = "";
	}  


	//*****
	//读取第一个单元格的对齐方式,数据类型:VT_I4
	//读取水平对齐方式
	range.AttachDispatch(sheet.GetCells());
	iCell.AttachDispatch((range.GetItem(COleVariant(long(1)),
									COleVariant(long(1)))).pdispVal);
	vResult.lVal = 0;
	vResult = iCell.GetHorizontalAlignment();
	if (vResult.lVal != 0)
	{
		switch (vResult.lVal)
		{
		case 1:
			//默认
			break;
		case -4108:
			//居中
			break;
		case -4131 :
			//靠左
			break;
		case -4152 :
			//靠右
			break;
		}
	}

	//垂直对齐方式
	iCell.AttachDispatch((range.GetItem(COleVariant(long(1)),
									COleVariant(long(1)))).pdispVal);
	vResult.lVal = 0;
	vResult = iCell.GetVerticalAlignment();
	if (vResult.lVal != 0)
	{
		switch (vResult.lVal)
		{
		case -4160 :
			//靠上
			break;
		case -4108 :
			//居中
			break;
		case -4107 :
			//靠下
			break;
		}
	}

	//*****
	//设置第一个单元格的值"HI,EXCEL!"
	// range.SetItem(COleVariant(1),COleVariant(1),COleVariant("HI,EXCEL!")); 


	//*****
	//设置第一个单元格字体颜色:红色
	Font font;
	range.AttachDispatch(sheet.GetCells());
	range.AttachDispatch((range.GetItem(COleVariant(long(2)),
									COleVariant(long(3)))).pdispVal);
	font.SetColor(COleVariant((long) 0xFF0000));  

	Borders borders;   
	Border border;   

	//单元格合并   
	range = sheet.GetRange(COleVariant("a1"), COleVariant("b1"));   
	range.Merge(COleVariant((short)true));   

	range.SetRowHeight(COleVariant((short) 40));   
	font = range.GetFont();   
//		range = sheet.GetRange(COleVariant("a1"), COleVariant("e6")); 
		range.AttachDispatch(sheet.GetUsedRange());
		borders = range.GetBorders();   
		borders.SetWeight(COleVariant((short) 1));  	 //xlthin   =   2   
		range.SetColumnWidth(COleVariant((short) 10));   
		//设置有边框,还是无边框   
		borders.SetLineStyle(COleVariant((short) - 4119));

	//*****
	//合并单元格的处理
	//包括判断第一个单元格是否为合并单元格,以及将第一个单元格进行合并
	Range unionRange;
	range.AttachDispatch(sheet.GetCells()); 
	unionRange.AttachDispatch(range.GetItem(COleVariant((long) 4),
										COleVariant((long) 2)).pdispVal);
	_variant_t w = unionRange.GetColumnWidth();
	float ww = w.fltVal;

	vResult = unionRange.GetMergeCells();    
	if (vResult.boolVal == -1)  		   //是合并的单元格
	{
		//合并单元格的行数 
		range.AttachDispatch(unionRange.GetRows());
		long iUnionRowNum = range.GetCount(); 

		//合并单元格的列数
		range.AttachDispatch(unionRange.GetColumns());
		long iUnionColumnNum = range.GetCount();   

		//合并区域的起始行,列
		long iUnionStartRow = unionRange.GetRow();  	 //起始行,从1开始
		long iUnionStartCol = unionRange.GetColumn();    //起始列,从1开始

		COleVariant w = unionRange.GetColumnWidth();
		long ww = w.bVal;
	}
	else if (vResult.boolVal == 0)
	{
		//不是合并的单元格}

		//将第一个单元格合并成2行,3列
		range.AttachDispatch(sheet.GetCells()); 
		unionRange.AttachDispatch(range.GetItem(COleVariant((long) 1),
											COleVariant((long) 1)).pdispVal);
		unionRange.AttachDispatch(unionRange.GetResize(COleVariant((long) 2),
												COleVariant((long) 3)));
		unionRange.Merge(COleVariant((long) 0));   //合并单元格


//		books.Close();  	
//		app.Quit();
	}
}





























































































































⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -