Chinaunix首页 | 论坛 | 博客
  • 博客访问: 141912
  • 博文数量: 27
  • 博客积分: 2613
  • 博客等级: 少校
  • 技术积分: 270
  • 用 户 组: 普通用户
  • 注册时间: 2004-12-17 13:12
个人简介

No pain, no gain.

文章分类

全部博文(27)

文章存档

2011年(4)

2010年(2)

2009年(2)

2008年(3)

2007年(2)

2006年(4)

2005年(4)

2004年(6)

我的朋友

分类: C/C++

2007-05-18 11:00:41

class ConExcel
{
 //#include "vcl\\utilcls.h" in head file//
 /*LiYongQuan*/
 public:
  AnsiString *rowVal,cellVal;
  /*row当前行;col当前列;colCount总列数;rowCount总行数*/
  unsigned int row,col,colCount,rowCount;
 private:
  Variant MSSheet,MSExcel;
  AnsiString FileName;
  char RW;
 public:
 ConExcel(AnsiString FileName,char LYQ='R')
 {
  if(Trim(FileName)=="")
     {ShowMessage("未指定EXCEL文件");
      return;
     }
  else
     {
      try
      {
       MSExcel=CreateOleObject("Excel.Application");
       MSExcel.OlePropertySet("Visible",false);
      }
      catch(...)
      {
       Application->MessageBox("无法启动Excel","错误",MB_ICONSTOP|MB_OK);
       return;
      }
      if (FileExists(FileName))
        {MSExcel.OlePropertyGet("WorkBooks").OleProcedure("Open",FileName.c_str());
        }
      else
        {
         MSExcel.OlePropertyGet("WorkBooks").OleFunction("Add");
         // MSExcel.OlePropertyGet("Sheets").OleFunction("Add");
        }
         MSSheet=MSExcel.OlePropertyGet("ActiveWorkBook").OlePropertyGet("ActiveSheet");

      row=1;
      col=1;
      this->RW=LYQ;
      this->FileName=FileName;
      colCount=MSSheet.OlePropertyGet("UsedRange").OlePropertyGet("Columns").OlePropertyGet("Count"); //列数
      rowCount=MSSheet.OlePropertyGet("UsedRange").OlePropertyGet("Rows").OlePropertyGet("Count"); //行数
      rowVal=NULL;
     }
 }
 AnsiString GetCellVal(unsigned int row=0,unsigned int col=0)
 {
  if(row==0)
    row=this->row;
  if(col==0)
    col=this->col;
  AnsiString temp=Trim((AnsiString)MSSheet.OlePropertyGet("Cells",row,col).OlePropertyGet("Value"));
  this->cellVal=temp;
  return temp;
 }
 AnsiString * GetRowVal(unsigned int row=0,unsigned int tcol=0)
 {
  if(row==0)
    row=this->row;
  if(tcol==0)
    tcol=this->colCount;
  if(this->rowVal!=NULL)
    delete [] this->rowVal;
  rowVal= new AnsiString[tcol];
  for(unsigned int c=1;c<=tcol;c++)
    rowVal[c-1]=Trim((AnsiString)MSSheet.OlePropertyGet("Cells",row,c).OlePropertyGet("Value"));
  return rowVal;
 }
 void setCellVal(unsigned int row,unsigned int col,AnsiString CellVal)
  {
   if(row==0)
     row=this->row;
   if(col==0)
     col=this->col;
   MSSheet.OlePropertyGet("Cells",row,col).OlePropertySet("Value",CellVal.c_str());
  }
 void setRowVal(unsigned int row,unsigned int tcol,AnsiString * RowVal)
  {
  if(row==0)
    row=this->row;
  if(tcol==0)
    tcol=this->colCount;
  for(unsigned int c=1;c<=tcol;c++)
    MSSheet.OlePropertyGet("Cells",row,c).OlePropertySet("Value",(*(RowVal+c-1)).c_str());
  }
  void SaveFile(AnsiString FName="")
    {
     if(UpperCase(this->RW)=='W')
       {
        if(FName=="")
           FName=this->FileName;
        MSSheet.OleProcedure("SaveAs",FName.c_str());
       }
    }
 ~ConExcel()
 {
   MSExcel.OlePropertyGet("ActiveWorkBook").OleProcedure("Close");
   MSExcel.OleFunction("Quit");
   MSExcel=Unassigned;
   if(this->rowVal!=NULL)
     delete [] this->rowVal;
   Application->MessageBox("Excel读取完成!","Invoice Process System",MB_OK+MB_ICONINFORMATION);
 }
};
阅读(1619) | 评论(0) | 转发(0) |
0

上一篇:create a "select element"

下一篇:本文查找工具

给主人留下些什么吧!~~