Type TSDocxCell = Class public Function Create(application, table, row, col); Function Init(); private Function GetCell(); Function SetCellTcPrWidth(row, col, widthValue, widthType); private application_; table_; // TOfficeObj("TTable") row_; column_; tcpr_; // TOfficeObj("TwTcPr") cell_; // TOfficeObj("TCell") min_dxa_width_; // 最小宽度 321 public Function AutoSum(); Function Delete(ShiftCells); Function Formula(Formula, NumFormat); Function Merge(MergeTo); Function Select(); Function SetHeight(RowHeight, HeightRule); Function SetWidth(ColumnWidth, RulerStyle); Function Split(NumRows, NumColumns); property Application read ReadApplication; property Borders read ReadBorders; property BottomPadding read ReadBottomPadding write WriteBottomPadding; property Column read ReadColumn; property ColumnIndex read ReadColumnIndex; property Creator read ReadCreator; property FitText read ReadFitText write WriteFitText; property Height read ReadHeight write WriteHeight; property HeightRule read ReadHeightRule write WriteHeightRule; property ID read ReadID write WriteID; property LeftPadding read ReadLeftPadding write WriteLeftPadding; property NestingLevel read ReadNestingLevel; property Next read ReadNext; property Parent read ReadParent; property PreferredWidth read ReadPreferredWidth write WritePreferredWidth; property PreferredWidthType read ReadPreferredWidthType write WritePreferredWidthType; property Previous read ReadPrevious; property Range read ReadRange; property RightPadding read ReadRightPadding write WriteRightPadding; property Row read ReadRow; property RowIndex read ReadRowIndex; property Shading read ReadShading; property Tables read ReadTables; property TopPadding read ReadTopPadding write WriteTopPadding; property VerticalAlignment read ReadVerticalAlignment write WriteVerticalAlignment; property Width read ReadWidth write WriteWidth; property WordWrap read ReadWordWrap write WriteWordWrap; Function WriteWordWrap(wrap); Function ReadWordWrap(); Function WriteWidth(width); Function ReadWidth(); Function WriteVerticalAlignment(alignment); Function ReadVerticalAlignment(); Function WriteTopPadding(top); Function ReadTopPadding(); Function ReadTables(index); Function ReadShading(); Function ReadRowIndex(); Function ReadRow(); Function WriteRightPadding(right); Function ReadRightPadding(); Function ReadRange(); Function ReadPrevious(); Function WritePreferredWidthType(value); Function ReadPreferredWidthType(); Function WritePreferredWidth(value); Function ReadPreferredWidth(); Function ReadParent(); Function ReadNext(); Function ReadNestingLevel(); Function WriteLeftPadding(left); Function ReadLeftPadding(); Function WriteID(); Function ReadID(); Function WriteHeightRule(); Function ReadHeightRule(); Function WriteHeight(); Function ReadHeight(); Function WriteFitText(); Function ReadFitText(); Function ReadCreator(); Function ReadColumnIndex(); Function ReadColumn(); Function WriteBottomPadding(bottom); Function ReadBottomPadding(); Function ReadBorders(index); Function ReadApplication(); End; // ============== 实现 ================= // Function TSDocxCell.Create(application, table, row, column); Begin application_ := application; table_ := table; row_ := row; column_ := column; min_dxa_width_ := 321; Init(); End; Function TSDocxCell.Init(); Begin cell := table_.GetCells()[row_-1, column_-1]; if not ifnil(cell) then begin tcpr_node := class(TSXml).GetNode(cell[0], "w:tcPr", "first"); tcpr_ := TOfficeObj("TwTcPr"); tcpr_.InitRootNode(tcpr_node); end End; Function TSDocxCell.GetCell(); Begin if ifObj(cell_) then return cell_; cell_ := table_.Cell(row_, column_); return cell_; End; Function TSDocxCell.SetCellTcPrWidth(row, col, widthValue, widthType); Begin cell := table_.GetCells()[row-1, col-1]; if ifnil(cell) then return; tcpr_node := class(TSXml).GetNode(cell[0], "w:tcPr", "first"); tcpr_obj := TOfficeObj("TwTcPr"); tcpr_obj.InitRootNode(tcpr_node); tcpr_obj.Width := widthValue; tcpr_obj.Type := widthType; tcpr_obj.Update(); End; // function Function TSDocxCell.Delete(ShiftCells); Begin if ifnil(ShiftCells) then ShiftCells := class(TSDocxEnumerations).wdDeleteCellsShiftLeft(); case ShiftCells of class(TSDocxEnumerations).wdDeleteCellsEntireColumn(): table_.DeleteCell(nil, column_); class(TSDocxEnumerations).wdDeleteCellsEntireRow(): table_.DeleteCell(row_, nil); class(TSDocxEnumerations).wdDeleteCellsShiftLeft(): table_.DeleteCell(row_, column_); class(TSDocxEnumerations).wdDeleteCellsShiftUp(): table_.DeleteCell(row_, column_, 0); end; End; Function TSDocxCell.Merge(MergeTo); Begin table_.Merge(self.RowIndex, self.ColumnIndex, MergeTo.RowIndex, MergeTo.ColumnIndex, false); End; // property Function TSDocxCell.ReadColumnIndex(); Begin return column_; End; Function TSDocxCell.ReadColumn(); Begin return new TSDocxColumn(application_, table_, column_); End; Function TSDocxCell.ReadRowIndex(); Begin return row_; End; // VBA width=100,xml是2000 Function TSDocxCell.WriteWidth(width); Begin tcpr_.Width := width * 20; tcpr_.Type := "dxa"; tcpr_.Update(); End; Function TSDocxCell.ReadWidth(); Begin return tcpr_.Value("width") / 20; End; Function TSDocxCell.WriteWordWrap(wrap); Begin tcpr_.noWrap := wrap ? 0 : 1; tcpr_.Update(); End; Function TSDocxCell.ReadWordWrap(); Begin return tcpr_.Value("noWrap") ? false : true; End; Function TSDocxCell.WriteVerticalAlignment(alignment); Begin case alignment of class(TSDocxEnumerations).wdCellAlignVerticalBottom(): begin tcpr_.vAlign := "bottom"; tcpr_.Update(); end class(TSDocxEnumerations).wdCellAlignVerticalCenter(): begin tcpr_.vAlign := "center"; tcpr_.Update(); end class(TSDocxEnumerations).wdCellAlignVerticalTop(): begin tcpr_.vAlign := "top"; tcpr_.Update(); end; end; End; Function TSDocxCell.ReadVerticalAlignment(); Begin alignment := tcpr_.Value("vAlign"); case alignment of "bottom": return class(TSDocxEnumerations).wdCellAlignVerticalBottom(); "center": return class(TSDocxEnumerations).wdCellAlignVerticalCenter(); else return class(TSDocxEnumerations).wdCellAlignVerticalTop(); end; End; Function TSDocxCell.WriteTopPadding(top); Begin tcpr_.CellMar.Top := 20 * top; tcpr_.CellMar.TopType := "dxa"; tcpr_.Update(); End; Function TSDocxCell.ReadTopPadding(); Begin return tcpr_.CellMar.Value("Top") / 20; End; Function TSDocxCell.WriteLeftPadding(left); Begin tcpr_.CellMar.Left := 20 * left; tcpr_.CellMar.LeftType := "dxa"; tcpr_.Update(); End; Function TSDocxCell.ReadLeftPadding(); Begin return tcpr_.CellMar.Value("Left") / 20; End; Function TSDocxCell.WriteBottomPadding(bottom); Begin tcpr_.CellMar.Bottom := 20 * bottom; tcpr_.CellMar.BottomType := "dxa"; tcpr_.Update(); End; Function TSDocxCell.ReadBottomPadding(); Begin return tcpr_.CellMar.Value("Bottom") / 20; End; Function TSDocxCell.WriteRightPadding(right); Begin tcpr_.CellMar.Right := 20 * right; tcpr_.CellMar.RightType := "dxa"; tcpr_.Update(); End; Function TSDocxCell.ReadRightPadding(); Begin return tcpr_.CellMar.Value("Right") / 20; End; Function TSDocxCell.ReadTables(index); Begin tables_obj := new TSDocxTables(application_, GetCell()); return ifnil(index) ? tables_obj : tables_obj[index]; End; Function TSDocxCell.ReadShading(); Begin return new TSDocxShading(application_, tcpr_); End; Function TSDocxCell.ReadBorders(index); Begin obj := new TSDocxBorders(application_, tcpr_.Borders); return ifnil(index) ? obj : obj[index]; End; Function TSDocxCell.WritePreferredWidthType(value); Begin width_value := nil; width_type := nil; case value of class(TSDocxEnumerations).wdPreferredWidthAuto(): begin table_.TblPr.Width := 0; table_.TblPr.WidthType := "auto"; table_.TblPr.Update(); width_value := 0; width_type := "auto"; end class(TSDocxEnumerations).wdPreferredWidthPercent(): begin table_.TblPr.Width := 5000; table_.TblPr.WidthType := "pct"; table_.TblPr.Update(); width_value := integer(5000 / table_.Cols()); width_type := "pct"; end class(TSDocxEnumerations).wdPreferredWidthPoints(): begin width_value := nil; width_type := "dxa"; end end; if ifnil(width_value) and ifnil(width_type) then return; for r:=1 to table_.Rows() do for c:=1 to table_.Cols() do SetCellTcPrWidth(r, c, width_value, width_type); End; Function TSDocxCell.ReadPreferredWidthType(); Begin width_type := tcpr_.Value("Type"); case width_type of "auto": return class(TSDocxEnumerations).wdPreferredWidthAuto(); "dxa": return class(TSDocxEnumerations).wdPreferredWidthPoints(); "pct": return class(TSDocxEnumerations).wdPreferredWidthPercent(); end; End; Function TSDocxCell.WritePreferredWidth(value); Begin width_type := self.PreferredWidthType; width_value := 0; case width_type of class(TSDocxEnumerations).wdPreferredWidthAuto(), class(TSDocxEnumerations).wdPreferredWidthPoints(): begin width_type := "dxa"; width_value := 20 * value; if width_type > min_dxa_width_ then width_type := min_dxa_width_; SetCellTcPrWidth(row_, column_, width_value, width_type); end class(TSDocxEnumerations).wdPreferredWidthPercent(): begin width_type := "pct"; if value < 0 or value > 100 then return; width_value := 50 * value; SetCellTcPrWidth(row_, column_, width_value, width_type); end end; End; Function TSDocxCell.ReadPreferredWidth(); Begin width_type := tcpr_.Value("Type"); width_value := tcpr_.Value("Width"); case width_type of "auto", "dxa": return width_value / 20; "pct": return width_value / 50; end; End;