home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / Chip_2003-01_cd1.bin / zkuste / delphi / kolekce / d567 / FLEXCEL.ZIP / FlexCel / UFlexCelImport.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2002-10-10  |  17.0 KB  |  531 lines

  1. unit UFlexCelImport;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, Classes, UExcelAdapter,
  7.   {$IFDEF ConditionalExpressions}{$if CompilerVersion >= 14} variants,{$IFEND}{$ENDIF} //Delphi 6 or above
  8.   UFlxMessages, UFlxFormats;
  9.  
  10. type
  11.   TFlexCelImport = class(TComponent)
  12.   private
  13.     function GetIsFormula(Row, Column: integer): boolean;
  14.     function GetAutoRowHeight(Row: integer): boolean;
  15.     procedure SetAutoRowHeight(Row: integer; const Value: boolean);
  16.     function GetCellFormat(Row, Col: integer): integer;
  17.     procedure SetCellFormat(Row, Col: integer; const Value: integer);
  18.     function GetCell(Row, Col: integer): TXlsCellValue;
  19.     procedure SetCell(Row, Col: integer; const Value: TXlsCellValue);
  20.     function GetColorPalette(index: TColorPaletteRange): LongWord;
  21.     procedure SetColorPalette(index: TColorPaletteRange; const Value: LongWord);
  22.     function GetColorPaletteCount: integer;
  23.     function GetColumnFormat(aColumn: integer): integer;
  24.     function GetRowFormat(aRow: integer): integer;
  25.     procedure SetColumnFormat(aColumn: integer; const Value: integer);
  26.     procedure SetRowFormat(aRow: integer; const Value: integer);
  27.     function GetFormatListCount: integer;
  28.     function GetFlxFormatList(index: integer): TFlxFormat;
  29.     function GetCellFormatDef(aRow, aCol: integer): TFlxFormat;
  30.     function GetColByIndex(Row, ColIndex: integer): integer;
  31.     function GetColIndexCount(Row: integer): integer;
  32.     function GetColIndex(Row, Col: integer): integer;
  33.     function GetDefaultColWidth: integer;
  34.     function GetDefaultRowHeight: integer;
  35.     function GetShowGridLines: boolean;
  36.     procedure SetShowGridLines(const Value: boolean);
  37.     function GetPictureName(aPos: integer): Widestring;
  38.     function GetPicturesCount: integer;
  39.     function GetCellMergedBounds(aRow, aCol: integer): TXlsCellRange;
  40.  
  41.   private
  42.     Workbook: TExcelFile;
  43.     FAdapter: TExcelAdapter;
  44.     procedure SetAdapter(const Value: TExcelAdapter);
  45.     function GetCellValue(Row, Col: integer): variant;
  46.     procedure SetCellValue(Row, Col: integer; const Value: variant);
  47.     function GetActiveSheet: integer;
  48.     procedure SetActiveSheet(const Value: integer);
  49.     function GetSheetCount: integer;
  50.     function GetMaxRow: integer;
  51.     function GetMaxCol: integer;
  52.     function GetComments(Row, Col: integer): variant;
  53.     procedure SetComments(Row, Col: integer; const Value: variant);
  54.     function GetCommentsCount(Row: integer): integer;
  55.     function GetActiveSheetName: widestring;
  56.     procedure SetActiveSheetName(const Value: widestring);
  57.     function GetColumnWidth(aCol: integer): integer;
  58.     function GetRowHeight(aRow: integer): integer;
  59.     procedure SetColumnWidth(aCol: integer; Value: integer);
  60.     procedure SetRowHeight(aRow: integer; Value: integer);
  61.  
  62.     { Private declarations }
  63.   protected
  64.     procedure Notification(AComponent: TComponent; Operation: TOperation); override;
  65.     { Protected declarations }
  66.   public
  67.     { Public declarations }
  68.     destructor Destroy;override;
  69.  
  70.     procedure OpenFile(const FileName: TFileName);
  71.     procedure Save(const FileName: TFileName);
  72.     procedure CloseFile;
  73.  
  74.     property SheetCount: integer read GetSheetCount;
  75.     property ActiveSheet:integer read GetActiveSheet write SetActiveSheet ;
  76.     property ActiveSheetName: widestring read GetActiveSheetName write SetActiveSheetName;
  77.  
  78.     property MaxRow: integer read GetMaxRow;
  79.     property MaxCol: integer read GetMaxCol;
  80.     function IsEmptyRow(const aRow: integer): boolean;
  81.  
  82.     property CellValue[Row, Col: integer]: variant read GetCellValue write SetCellValue;
  83.     property CellFormat[Row, Col: integer]: integer read GetCellFormat write SetCellFormat;
  84.     property IsFormula[Row, Column: integer]: boolean read GetIsFormula;
  85.     property Cell[Row, Col: integer]: TXlsCellValue read GetCell write SetCell;
  86.  
  87.     property ColByIndex[Row, ColIndex: integer]: integer read GetColByIndex;
  88.     property ColIndex[Row, Col: integer]: integer read GetColIndex;
  89.     property ColIndexCount[Row: integer]: integer read GetColIndexCount;
  90.  
  91.     property ColorPalette[index: TColorPaletteRange]: LongWord read GetColorPalette write SetColorPalette;
  92.     property ColorPaletteCount: integer read GetColorPaletteCount;
  93.  
  94.     property FormatList[index: integer]: TFlxFormat read GetFlxFormatList;
  95.     property CellFormatDef[aRow, aCol: integer]: TFlxFormat read GetCellFormatDef;
  96.     property FormatListCount: integer read GetFormatListCount;
  97.  
  98.     property ColumnWidth[aCol: integer]: integer read GetColumnWidth write SetColumnWidth;
  99.     property RowHeight[aRow: integer]: integer read GetRowHeight write SetRowHeight;
  100.     property AutoRowHeight[Row: integer]: boolean read GetAutoRowHeight write SetAutoRowHeight;
  101.  
  102.     property DefaultRowHeight: integer read GetDefaultRowHeight;
  103.     property DefaultColWidth: integer read GetDefaultColWidth;
  104.  
  105.     property ColumnFormat[aColumn: integer]: integer read GetColumnFormat write SetColumnFormat;
  106.     property RowFormat[aRow: integer]: integer read GetRowFormat write SetRowFormat;
  107.  
  108.     property CellMergedBounds[aRow, aCol: integer]: TXlsCellRange read GetCellMergedBounds;
  109.  
  110.     procedure InsertAndCopyRows(const FirstRow, LastRow, DestRow, aCount: integer);
  111.     procedure DeleteRows(const aRow, aCount: integer);
  112.  
  113.     property CommentsCount[Row: integer]: integer read GetCommentsCount;
  114.     property Comments[Row, Col: integer]: variant read GetComments write SetComments;
  115.  
  116.     property PicturesCount: integer read GetPicturesCount;
  117.     property PictureName[aPos: integer]: Widestring read GetPictureName;
  118.     procedure AssignPicture(const aPos: integer; const Pic: string; const PicType: TXlsImgTypes);
  119.     procedure GetPicture(const aPos: integer; const Pic: TStream; var PicType: TXlsImgTypes; var ClientAnchor: TClientAnchor);
  120.  
  121.     property ShowGridLines: boolean read GetShowGridLines write SetShowGridLines;
  122.  
  123.     function CanOptimizeRead:boolean;
  124.  
  125.     function IsLoaded: boolean;
  126.   published
  127.     property Adapter: TExcelAdapter read FAdapter write SetAdapter;
  128.     { Published declarations }
  129.   end;
  130.  
  131. procedure Register;
  132.  
  133. implementation
  134. {$R IFlexCelImport.res}
  135. procedure Register;
  136. begin
  137.   RegisterComponents('FlexCel', [TFlexCelImport]);
  138. end;
  139.  
  140. { TFlexCelImport }
  141.  
  142. procedure TFlexCelImport.CloseFile;
  143. begin
  144.   if Workbook=nil then exit;
  145.   try
  146.     Workbook.CloseFile;
  147.   except
  148.     //nothing;
  149.   end; //Except
  150.   try
  151.     Workbook.Disconnect;
  152.   except
  153.     //nothing;
  154.   end; //Except
  155.   FreeAndNil(Workbook);
  156. end;
  157.  
  158. procedure TFlexCelImport.DeleteRows(const aRow, aCount: integer);
  159. begin
  160.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  161.   Workbook.DeleteRows(aRow, aCount);
  162. end;
  163.  
  164. destructor TFlexCelImport.Destroy;
  165. begin
  166.   CloseFile;
  167.   inherited;
  168. end;
  169.  
  170. function TFlexCelImport.GetActiveSheet: integer;
  171. begin
  172.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  173.   Result:=Workbook.ActiveSheet;
  174. end;
  175.  
  176. function TFlexCelImport.GetCellValue(Row, Col: integer): variant;
  177. begin
  178.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  179.   Result:= Workbook.CellValue[Row, Col];
  180. end;
  181.  
  182. function TFlexCelImport.GetComments(Row, Col: integer): variant;
  183. begin
  184.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  185.   Result:= Workbook.CommentText[Row, Col];
  186. end;
  187.  
  188. function TFlexCelImport.GetCommentsCount(Row: integer): integer;
  189. begin
  190.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  191.   Result:=Workbook.CommentsCount[Row];
  192. end;
  193.  
  194. function TFlexCelImport.GetMaxRow: integer;
  195. begin
  196.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  197.   Result:=Workbook.MaxRow;
  198. end;
  199.  
  200. function TFlexCelImport.GetMaxCol: integer;
  201. begin
  202.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  203.   Result:=Workbook.MaxCol;
  204. end;
  205.  
  206.  
  207. function TFlexCelImport.GetSheetCount: integer;
  208. begin
  209.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  210.   Result:=Workbook.SheetCount;
  211. end;
  212.  
  213. function TFlexCelImport.GetActiveSheetName: widestring;
  214. begin
  215.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  216.   Result:= Workbook.ActiveSheetName;
  217. end;
  218.  
  219. procedure TFlexCelImport.InsertAndCopyRows(const FirstRow, LastRow,
  220.   DestRow, aCount: integer);
  221. begin
  222.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  223.   Workbook.InsertAndCopyRows(FirstRow, LastRow, DestRow, aCount, False);
  224. end;
  225.  
  226. function TFlexCelImport.IsLoaded: boolean;
  227. begin
  228.   Result:= Workbook<>nil;
  229. end;
  230.  
  231. procedure TFlexCelImport.Notification(AComponent: TComponent; Operation: TOperation);
  232. begin
  233.   inherited Notification(AComponent, Operation);
  234.   if Operation = opRemove then
  235.   begin
  236.     if AComponent = FAdapter then
  237.         FAdapter:= nil;
  238.   end;
  239. end;
  240.  
  241. procedure TFlexCelImport.OpenFile(const FileName: TFileName);
  242. begin
  243.   if FAdapter=nil then raise Exception.Create(ErrNoAdapter);
  244.   CloseFile;
  245.   Workbook:=FAdapter.GetWorkbook;
  246.   try
  247.     Workbook.Connect;
  248.     Workbook.OpenFile(FileName);
  249.     Workbook.SelectSheet(Workbook.ActiveSheet);
  250.   except
  251.     CloseFile;
  252.     raise;
  253.   end;
  254. end;
  255.  
  256. procedure TFlexCelImport.Save(const FileName: TFileName);
  257. begin
  258.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  259.   Workbook.Save(true, FileName, nil);
  260. end;
  261.  
  262. procedure TFlexCelImport.SetActiveSheet(const Value: integer);
  263. begin
  264.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  265.   Workbook.ActiveSheet:=Value;
  266.   Workbook.SelectSheet(Value);
  267. end;
  268.  
  269. procedure TFlexCelImport.SetAdapter(const Value: TExcelAdapter);
  270. begin
  271.   FAdapter := Value;
  272. end;
  273.  
  274. procedure TFlexCelImport.SetCellValue(Row, Col: integer;
  275.   const Value: variant);
  276. begin
  277.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  278.   Workbook.CellValue[Row, Col]:= Value;
  279. end;
  280.  
  281. procedure TFlexCelImport.SetComments(Row, Col: integer;
  282.   const Value: variant);
  283. begin
  284.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  285.   Workbook.AssignComment(Row, Col, Value);
  286. end;
  287.  
  288. procedure TFlexCelImport.SetActiveSheetName(const Value: widestring);
  289. begin
  290.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  291.   Workbook.ActiveSheetName:=Value;
  292.  
  293. end;
  294.  
  295. function TFlexCelImport.IsEmptyRow(const aRow: integer): boolean;
  296. begin
  297.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  298.   Result:=Workbook.IsEmptyRow(aRow);
  299. end;
  300.  
  301. function TFlexCelImport.GetColumnWidth(aCol: integer): integer;
  302. begin
  303.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  304.   Result:=Workbook.ColumnWidth[aCol];
  305. end;
  306.  
  307. function TFlexCelImport.GetRowHeight(aRow: integer): integer;
  308. begin
  309.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  310.   Result:=Workbook.RowHeight[aRow];
  311. end;
  312.  
  313. procedure TFlexCelImport.SetColumnWidth(aCol: integer; Value: integer);
  314. begin
  315.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  316.   Workbook.ColumnWidth[aCol]:=Value;
  317. end;
  318.  
  319. procedure TFlexCelImport.SetRowHeight(aRow: integer; Value: integer);
  320. begin
  321.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  322.   Workbook.RowHeight[aRow]:=Value;
  323. end;
  324.  
  325. function TFlexCelImport.GetIsFormula(Row, Column: integer): boolean;
  326. begin
  327.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  328.   Result:=Workbook.CellValueX[Row, Column].IsFormula;
  329. end;
  330.  
  331. function TFlexCelImport.GetAutoRowHeight(Row: integer): boolean;
  332. begin
  333.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  334.   Result:=Workbook.AutoRowHeight[Row];
  335. end;
  336.  
  337. procedure TFlexCelImport.SetAutoRowHeight(Row: integer; const Value: boolean);
  338. begin
  339.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  340.   Workbook.AutoRowHeight[Row]:=Value;
  341. end;
  342.  
  343. function TFlexCelImport.GetCellFormat(Row, Col: integer): integer;
  344. begin
  345.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  346.   Result:=Workbook.CellValueX[Row, Col].XF;
  347. end;
  348.  
  349. procedure TFlexCelImport.SetCellFormat(Row, Col: integer;
  350.   const Value: integer);
  351. var
  352.   C: TXlsCellValue;
  353. begin
  354.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  355.   C.IsFormula:=false;
  356.   C.Value:=GetCellValue(Row, Col);
  357.   C.XF:=Value;
  358.   Workbook.CellValueX[Row, Col]:=C;
  359. end;
  360.  
  361. function TFlexCelImport.GetCell(Row, Col: integer): TXlsCellValue;
  362. begin
  363.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  364.   Result:=Workbook.CellValueX[Row, Col];
  365. end;
  366.  
  367. procedure TFlexCelImport.SetCell(Row, Col: integer;
  368.   const Value: TXlsCellValue);
  369. begin
  370.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  371.   Workbook.CellValueX[Row, Col]:=Value;
  372. end;
  373.  
  374. function TFlexCelImport.GetColorPalette(index: TColorPaletteRange): LongWord;
  375. begin
  376.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  377.   Result:= Workbook.ColorPalette[Index];
  378. end;
  379.  
  380. procedure TFlexCelImport.SetColorPalette(index: TColorPaletteRange;
  381.   const Value: LongWord);
  382. begin
  383.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  384.   Workbook.ColorPalette[Index]:=Value;
  385. end;
  386.  
  387. function TFlexCelImport.GetColorPaletteCount: integer;
  388. begin
  389.   Result:=56;
  390. end;
  391.  
  392.  
  393. function TFlexCelImport.GetColumnFormat(aColumn: integer): integer;
  394. begin
  395.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  396.   Result:=Workbook.ColumnFormat[aColumn];
  397. end;
  398.  
  399. function TFlexCelImport.GetRowFormat(aRow: integer): integer;
  400. begin
  401.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  402.   Result:=Workbook.RowFormat[aRow];
  403. end;
  404.  
  405. procedure TFlexCelImport.SetColumnFormat(aColumn: integer;
  406.   const Value: integer);
  407. begin
  408.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  409.   Workbook.ColumnFormat[aColumn]:=Value;
  410. end;
  411.  
  412. procedure TFlexCelImport.SetRowFormat(aRow: integer; const Value: integer);
  413. begin
  414.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  415.   Workbook.RowFormat[aRow]:=Value;
  416. end;
  417.  
  418. function TFlexCelImport.GetFormatListCount: integer;
  419. begin
  420.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  421.   Result:=Workbook.FormatListCount;
  422. end;
  423.  
  424. function TFlexCelImport.GetFlxFormatList(index: integer): TFlxFormat;
  425. begin
  426.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  427.   if (Index<0) or (Index>=GetFormatListCount) then
  428.     raise Exception.CreateFmt(ErrIndexOutBounds,[index,'FormatList',0,GetFormatListCount]);
  429.   Result:=Workbook.FormatList[index];
  430. end;
  431.  
  432. function TFlexCelImport.GetCellFormatDef(aRow, aCol: integer): TFlxFormat;
  433. var
  434.   XF: integer;
  435. begin
  436.   XF:= CellFormat[aRow, aCol];
  437.   if XF<0 then
  438.   begin
  439.     XF:=RowFormat[aRow];
  440.     if XF<=0 then XF:=ColumnFormat[aCol];
  441.   end;
  442.   if (XF<0) then XF:=0;
  443.   Result:=FormatList[XF];
  444. end;
  445.  
  446. function TFlexCelImport.GetColByIndex(Row, ColIndex: integer): integer;
  447. begin
  448.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  449.   if (ColIndex<=0) or (ColIndex>ColIndexCount[Row]) then
  450.     raise Exception.CreateFmt(ErrIndexOutBounds,[ColIndex,'ColByIndex',1,ColIndexCount[Row]]);
  451.   Result:=Workbook.ColByIndex(Row, ColIndex);
  452. end;
  453.  
  454. function TFlexCelImport.GetColIndexCount(Row: integer): integer;
  455. begin
  456.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  457.   Result:=Workbook.ColIndexCount(Row);
  458. end;
  459.  
  460. function TFlexCelImport.GetColIndex(Row, Col: integer): integer;
  461. begin
  462.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  463.   Result:=Workbook.ColIndex(Row, Col);
  464. end;
  465.  
  466. function TFlexCelImport.GetDefaultColWidth: integer;
  467. begin
  468.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  469.   Result:=Workbook.DefaultColWidth;
  470. end;
  471.  
  472. function TFlexCelImport.GetDefaultRowHeight: integer;
  473. begin
  474.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  475.   Result:=Workbook.DefaultRowHeight;
  476. end;
  477.  
  478. function TFlexCelImport.GetShowGridLines: boolean;
  479. begin
  480.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  481.   Result:=Workbook.ShowGridLines;
  482. end;
  483.  
  484. procedure TFlexCelImport.SetShowGridLines(const Value: boolean);
  485. begin
  486.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  487.   Workbook.ShowGridLines:=Value;
  488. end;
  489.  
  490. function TFlexCelImport.GetPictureName(aPos: integer): Widestring;
  491. begin
  492.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  493.   Result:=Workbook.PictureName[-1, aPos];
  494. end;
  495.  
  496. function TFlexCelImport.GetPicturesCount: integer;
  497. begin
  498.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  499.   Result:=Workbook.PicturesCount[-1];
  500. end;
  501.  
  502. procedure TFlexCelImport.AssignPicture(const aPos: integer;
  503.   const Pic: string; const PicType: TXlsImgTypes);
  504. begin
  505.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  506.   Workbook.AssignPicture(-1, aPos, Pic, PicType);
  507. end;
  508.  
  509. procedure TFlexCelImport.GetPicture(const aPos: integer;
  510.   const Pic: TStream; var PicType: TXlsImgTypes;
  511.   var ClientAnchor: TClientAnchor);
  512. begin
  513.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  514.   Workbook.GetPicture(-1, aPos, Pic, PicType, ClientAnchor);
  515. end;
  516.  
  517. function TFlexCelImport.CanOptimizeRead: boolean;
  518. begin
  519.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  520.   Result:=Workbook.CanOptimizeRead;
  521. end;
  522.  
  523.  
  524. function TFlexCelImport.GetCellMergedBounds(aRow, aCol: integer): TXlsCellRange;
  525. begin
  526.   if Workbook=nil then raise Exception.Create(ErrNoOpenFile);
  527.   Result:=Workbook.CellMergedBounds[aRow, aCol];
  528. end;
  529.  
  530. end.
  531.