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

📄 abaoexcel.cpp

📁 用vc编写的将数据导入到excel的程序
💻 CPP
字号:
#include "abaoexcel.h"

/************************************
  REVISION LOG ENTRY
  Revision By: abao++
  Revised on 2006-8-11 8:13:15
  Comments: ...
 ************************************/


extern double Round(double value,unsigned int num);
AbaoRange::AbaoRange(Range& range)
{
	rg=range;
}
void AbaoRange::Merge()
{
	rg.Merge(COleVariant((short)1));
}
AbaoRange& AbaoRange::operator=(const CString s)
{
	rg.SetValue2(COleVariant(s));
	return *this;
}
AbaoRange& AbaoRange::operator=(const char* str)
{
	rg.SetValue2(COleVariant(str));
	return *this;
}
AbaoRange& AbaoRange::operator=(Range& range)
{
	rg=range;
	return *this;
}

int AbaoRange::Border(short mode,long BoderWidth,long ColorIndex, VARIANT color)
{
	rg.BorderAround(COleVariant((short)mode),(long)BoderWidth,(long)ColorIndex,color);	
	return 1;
}
int AbaoRange::SetHAlign(RangeHAlignment mode)
{
	rg.SetHorizontalAlignment(COleVariant((short)mode));
	return 1;
}
int AbaoRange::SetVAlign(RangeVAlignment mode)
{
	rg.SetVerticalAlignment(COleVariant((short)mode));
	return 1;
}


AbaoExcel::AbaoExcel()
{
	if(!App.CreateDispatch("Excel.Application",NULL)) 
	{ 
		AfxMessageBox("创建Excel服务失败!"); 
		exit(1); 
	}
	workbooks.AttachDispatch(App.GetWorkbooks(),true);
	this->SetVisible(false);		//使excel不可见:如果只想用程序控制该excel而不想让用户操作时候,可以设置为false
	workbook.SetSaved(true);
	App.SetDisplayAlerts(FALSE);    //隐藏对话框,不显示是否覆盖原文件,而自动保存

}
AbaoExcel::~AbaoExcel()
{
	COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
	workbook.Close(_variant_t(true),covOptional,covOptional);
	workbooks.Close();
	App.Quit();
	//App.SetVisible(true);
	range.ReleaseDispatch(); 
	sheet.ReleaseDispatch(); 
	sheets.ReleaseDispatch(); 	
	workbook.ReleaseDispatch();
	workbooks.ReleaseDispatch(); 
	App.ReleaseDispatch(); 
}
int AbaoExcel::Add(CString& ExtPath)
{
	if(ExtPath.IsEmpty())    //若文件名为空,则建立一个新的文件
	{
		COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
		workbook.AttachDispatch(workbooks.Add(covOptional));	
	}
	else
	{
		workbook.AttachDispatch(workbooks.Add(_variant_t(ExtPath)));			
	}
	sheets.AttachDispatch(workbook.GetWorksheets(),true);
	return 1;
}
_Worksheet& AbaoExcel::SelectSheet(CString& SheetName)
{
	sheet.AttachDispatch(sheets.GetItem(_variant_t(SheetName.AllocSysString())),true);
	range.AttachDispatch(sheet.GetCells(),true);
	return sheet;
}
Range& AbaoExcel::ActiveSheetRange()
{
	range.AttachDispatch(sheet.GetCells(),true);
	return range;
}
_Worksheet& AbaoExcel::SelectSheet(int index)//选择一个已知表名的表
{
	sheet.AttachDispatch(sheets.GetItem(_variant_t((long)index)));
	range.AttachDispatch(sheet.GetCells(),true);
	return sheet;
}
int  AbaoExcel::SetCell(int row,int col,CString &str)
{
	range.SetItem(_variant_t((long)row),_variant_t((long)col),_variant_t(str));
	return 1;

}
int AbaoExcel::SetCell(int row,int col,char* str)
{
	SetCell(row,col,CString(str));
	return 1;

}
int AbaoExcel::SetCell(int row,int col,long lv)
{
	CString t;
	t.Format("%ld",lv);
	SetCell(row,col,t);
	return 1;

}


int AbaoExcel::SetCell(int row,int col,double dv,int n)
{
	CString t;
	CString format;
	format.Format("%%.%dlf",n);
	t.Format(format,dv);
	SetCell(row,col,t);
	return 1;
}
int AbaoExcel::SaveAs(CString &FileName)
{
	COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
	//this->workbook.SaveCopyAs(COleVariant(FileName));
	this->workbook.SaveAs(COleVariant(FileName),covOptional,covOptional,covOptional,covOptional,covOptional,1,covOptional,covOptional,covOptional,covOptional,covOptional);
	return 1;
}
int AbaoExcel::Save()
{
//	COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
	//this->workbook.SaveCopyAs(COleVariant(FileName));
	this->workbook.Save();
	return 1;
}
int AbaoExcel::Copy(_Worksheet &sht)
{
	sheet.Copy(vtMissing,_variant_t(sht));
	return 1;
}
Range& AbaoExcel::GetRange(CString RangeStart,CString RangeEnd)
{
	range=sheet.GetRange(COleVariant(RangeStart),COleVariant(RangeEnd));	
	return range;
}//获取range,
Range& AbaoExcel::GetRange(CString RangeStr)
{
	int pos=RangeStr.Find(':');
	if(pos>0)
	{
		CString a,b;
		a=RangeStr.Left(pos);
		b=RangeStr.Right(RangeStr.GetLength()-pos-1);
		return GetRange(a,b);
	}
	else
	{
		return GetRange(RangeStr,RangeStr);
	}
}//获取range A1:A2模式
int AbaoExcel::MergeRange(CString RangeStr)
{
	GetRange(RangeStr).Merge(COleVariant(long(1)));
	return 1;
}//合并Range
_Worksheet& AbaoExcel::ActiveSheet()
{
	sheet=workbook.GetActiveSheet();
	return sheet;
}

⌨️ 快捷键说明

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