Type TSDocxCell = Class(TSVbaBase) Uses TSDocxEnumerations; public Function Init(table, row, column); private Function GetCell(); Function SetCellTcPrWidth(row, col, widthValue, widthType); private 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 Borders read ReadBorders; property BottomPadding read ReadBottomPadding write WriteBottomPadding; property Column read ReadColumn; property ColumnIndex read ReadColumnIndex; 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 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 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 ReadColumnIndex(); Function ReadColumn(); Function WriteBottomPadding(bottom); Function ReadBottomPadding(); Function ReadBorders(index); End; // ============== 实现 ================= // Function TSDocxCell.Init(table, row, column); Begin table_ := table; row_ := row; column_ := column; min_dxa_width_ := 321; 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 := TSDocxEnumerations.wdDeleteCellsShiftLeft(); case ShiftCells of TSDocxEnumerations.wdDeleteCellsEntireColumn(): table_.DeleteCell(nil, column_); TSDocxEnumerations.wdDeleteCellsEntireRow(): table_.DeleteCell(row_, nil); TSDocxEnumerations.wdDeleteCellsShiftLeft(): table_.DeleteCell(row_, column_); 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 column_obj := new TSDocxColumn(self.Application, self.Creator, self); column_obj.Init(table_, column_); return column_obj; 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 TSDocxEnumerations.wdCellAlignVerticalBottom(): begin tcpr_.vAlign := "bottom"; tcpr_.Update(); end TSDocxEnumerations.wdCellAlignVerticalCenter(): begin tcpr_.vAlign := "center"; tcpr_.Update(); end TSDocxEnumerations.wdCellAlignVerticalTop(): begin tcpr_.vAlign := "top"; tcpr_.Update(); end; end; End; Function TSDocxCell.ReadVerticalAlignment(); Begin alignment := tcpr_.Value("vAlign"); case alignment of "bottom": return TSDocxEnumerations.wdCellAlignVerticalBottom(); "center": return TSDocxEnumerations.wdCellAlignVerticalCenter(); else return 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(self.Application, self.Creator, self); tables_obj.Init(GetCell()); return ifnil(index) ? tables_obj : tables_obj[index]; End; Function TSDocxCell.ReadShading(); Begin shading_obj := new TSDocxShading(self.Application, self.Creator, self); shading_obj.Init(tcpr_); return shading_obj; End; Function TSDocxCell.ReadBorders(index); Begin borders_obj := new TSDocxBorders(self.Application, self.Creator, self); borders_obj.Init(tcpr_.Borders); return ifnil(index) ? obj : obj[index]; End; Function TSDocxCell.WritePreferredWidthType(value); Begin width_value := nil; width_type := nil; case value of TSDocxEnumerations.wdPreferredWidthAuto(): begin table_.TblPr.Width := 0; table_.TblPr.WidthType := "auto"; table_.TblPr.Update(); width_value := 0; width_type := "auto"; end TSDocxEnumerations.wdPreferredWidthPercent(): begin table_.TblPr.Width := 5000; table_.TblPr.WidthType := "pct"; table_.TblPr.Update(); width_value := integer(5000 / table_.Cols()); width_type := "pct"; end 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 TSDocxEnumerations.wdPreferredWidthAuto(); "dxa": return TSDocxEnumerations.wdPreferredWidthPoints(); "pct": return TSDocxEnumerations.wdPreferredWidthPercent(); end; End; Function TSDocxCell.WritePreferredWidth(value); Begin width_type := self.PreferredWidthType; width_value := 0; case width_type of TSDocxEnumerations.wdPreferredWidthAuto(), 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 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;