Because the project needs to read the data in Excel, and it can be edited, and requires an interface style adjustment, the control is studied.
Cxspreadsheetbook is an Excel control that can read data from Excel and control the cells of the control, enabling you to merge cells, set cell widths, set the cell's font size, font style, set cell text to center, and more. Here's the trick.
1: First define cell style
- Tstylevalue = (svalign, Svfontname, Svsize, Svbold, Svitalic, Svunderline, svstrikeout);
- Tstylevalueset = set of Tstylevalue;
The position of the text, the font name of the text, the size, whether it is bold, whether it is underlined, etc.
2: Merging cells
- Procedure Setmergedstatea (left, Top, right,
- Bottom:integer; Ismerge:boolean);
- Var
- Arect:trect;
- Begin
- With Cx_statresult.activesheet do
- Begin
- Arect.left:=left;
- Arect.top:=top;
- Arect.right:=right;
- Arect.bottom:=bottom;
- Setmergedstate (Arect, True);
- End
- End
Method of Use: Setmergedstatea (0,0,15,0,true);
3: Set the style of the cell text
- Procedure Tfrmstatiwenshu.setcellsstyle (Avaluesset:tstylevalueset;
- Aalign:tcxhorztextalign; Afontsize:integer; Const afontname:string;
- Astyles:tfontstyles);
- Procedure SetValue (Aflag:tstylevalue; Aneedstyle:tfontstyle;
- var asetstyles:tfontstyles);
- Begin
- If Aflag in Avaluesset then
- Begin
- If Aneedstyle in Astyles then
- Include (Asetstyles, Aneedstyle)
- Else
- Exclude (Asetstyles, Aneedstyle);
- End
- End
- Var
- I, J:integer;
- Astyle:tfontstyles;
- Begin
- With Cx_statresult do
- Try
- BeginUpdate;
- With ActiveSheet do
- Begin
- For I: = Selectionrect.left to Selectionrect.right do
- For J: = Selectionrect.top to Selectionrect.bottom do
- With Getcellobject (I, J) do
- Try
- With Style do
- Begin
- Astyle: = Font.style;
- If svfontname in Avaluesset then
- Font.Name: = Afontname;
- If svsize in Avaluesset then
- Font.Size: = afontsize;
- If svalign in Avaluesset then
- Horztextalign: = aalign;
- SetValue (Svbold, Fsbold, Astyle);
- SetValue (Svitalic, Fsitalic, Astyle);
- SetValue (Svunderline, Fsunderline, Astyle);
- SetValue (Svstrikeout, Fsstrikeout, Astyle);
- Font.style: = Astyle;
- End
- Finally
- Free;
- End
- End
- Finally
- endupdate;
- Updatecontrol;
- End
- End
Use as follows:
- Cx_statresult.pages[0]. Selectcell (0,0,false);
- Setcellsstyle ([Svbold,svalign], hacenter, 0, ', [Fsbold]);
First select the cell and then call Setcellsstyle to format the cell
3: Control does not select a table, use the following statement:
- Cx_statresult.pages[0]. Selectcell ( -1,-1,false);
4: Set the width of a column
- Cx_statresult.pages[0]. cols.size[15]:=70;
5: Set the text of a cell
- Cell: = Cx_statresult.pages[0]. Getcellobject (15, 1);
- Cell.text:= ' remarks ';
6: Import Excel
- Cx_statresult.loadfromfile ();
The Cxspreadsheetbook control uses