📄 abaoexcel.cpp
字号:
/* 这份源代码文件已被未注册的SourceFormatX格式化过 */
/* 如果您想不再添加此类信息,请您注册这个共享软件 */
/* 更多相关信息请访问网站: http://cn.textrush.com */
#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.SetValue(COleVariant(s));
return *this;
}
AbaoRange &AbaoRange::operator = (const char *str)
{
rg.SetValue(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);
}
AbaoExcel::~AbaoExcel()
{
COleVariant covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
workbook.Close(covOptional, 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);
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 + -