educat Цитата: SetCellText
Поюзал я этот cx-ких Spread Sheet в одном проекте. Тормозной он жуть. В итоге все их высокоуровненвые методы доступак ячейкам я выбросил (не использую).
Вместо этого обращають напрямую минуя обёртки. Скорость возросла несравненно. В их обёртках при любом обращении создаютяс доп обьекты + какието проверки ... в итоге на больших обьёмах полный пипец.
В общем код, авось на что натолкнёт ... [more]
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
cxSSTypes, cxSSHeaders, cxSSData,
StdCtrls, dxStatusBar, cxMemo, cxButtonEdit, cxTextEdit, cxMaskEdit,
cxSSheet, cxButtons, ExtCtrls, cxDropDownEdit, cxContainer, cxEdit,
cxLabel, cxPC, cxControls;
type
TFormMain = class(TForm)
...
cxSpreadSheet: TcxSpreadSheet;
cxSpreadBook: TcxSpreadSheetBook;
...
public
procedure SetCellText(ACol, ARow: Integer; AText: string);
procedure SetCellDate(ACol, ARow: Integer; ADate: TDateTime);
procedure SetCellFont(ALeftCol, ATopRow, ARightCol, ABottomRow: Integer;
AStyle: TFontStyles; ASize: Integer); overload;
procedure SetCellFont(ALeftCol, ATopRow, ARightCol, ABottomRow: Integer;
AStyle: TFontStyles; ASize: Integer; AFontColor: TColor); overload;
procedure SetCellPattern(ALeftCol, ATopRow, ARightCol, ABottomRow,
ABackgroundColor, AForeGroundColor: Integer; AFillStyle: TcxSSFillStyle);
procedure SetCellFormat(ALeftCol, ATopRow, ARightCol, ABottomRow, AFormat: Integer);
procedure SetCellAlignment(ALeftCol, ATopRow, ARightCol, ABottomRow: Integer;
AHorzAlign: TcxHorzTextAlign; AVertAlign: TcxVertTextAlign);
procedure SetCellBorders(ALeftCol, ATopRow, ARightCol, ABottomRow: Integer;
AEdge: Integer; AStyle: TcxSSEdgeLineStyle); overload;
procedure SetCellBorders(ALeftCol, ATopRow, ARightCol, ABottomRow: Integer;
AEdge: TAlign; AStyle: TcxSSEdgeLineStyle); overload;
end;
...
procedure TFormMain.SetCellText(ACol, ARow: Integer; AText: string);
begin
with cxSpreadBook.ActiveSheet.GetCellObject(ACol, ARow) do
begin
try
Text := AText;
finally
Free; // and free it (important!)
end;
end;
end;
procedure TFormMain.SetCellDate(ACol, ARow: Integer; ADate: TDateTime);
begin
// get a cell object for the request col and row
with cxSpreadBook.ActiveSheet.GetCellObject(ACol, ARow) do
begin
try
DateTime := ADate;
finally
Free; // free it
end;
end;
end;
procedure TFormMain.SetCellFont(ALeftCol, ATopRow, ARightCol, ABottomRow: Integer;
AStyle: TFontStyles; ASize: Integer);
var
i, j: Integer;
begin
with cxSpreadBook.ActiveSheet do // using our active page
for i := ALeftCol to ARightCol do // for each column specified
for j := ATopRow to ABottomRow do // for each row specified
with GetCellObject(i, j) do // get the cell
try
Style.Font.Style := AStyle;
Style.Font.Size := ASize;
finally
Free; // free it
end;
end;
procedure TFormMain.SetCellFont(ALeftCol, ATopRow, ARightCol, ABottomRow: Integer;
AStyle: TFontStyles; ASize: Integer; AFontColor: TColor);
var
i, j: Integer;
begin
with cxSpreadBook.ActiveSheet do // using our active page
for i := ALeftCol to ARightCol do // for each column specified
for j := ATopRow to ABottomRow do // for each row specified
with GetCellObject(i, j) do // get the cell
try
Style.Font.Style := AStyle;
Style.Font.Size := ASize;
Style.Font.FontColor := AFontColor; // ??? - not worked
finally
Free; // free it
end;
end;
procedure TFormMain.SetCellPattern(ALeftCol, ATopRow, ARightCol, ABottomRow,
ABackgroundColor, AForeGroundColor: Integer; AFillStyle: TcxSSFillStyle);
var
i, j: Integer;
begin
with cxSpreadBook.ActiveSheet do // using our active page
for i := ALeftCol to ARightCol do // for each column specified
for j := ATopRow to ABottomRow do // for each row specified
with GetCellObject(i, j) do // get the cell
try
Style.Brush.BackgroundColor := ABackgroundColor;
Style.Brush.ForegroundColor := AForeGroundColor;
Style.Brush.Style := AFillStyle;
finally
Free;
end;
end;
procedure TFormMain.SetCellFormat(ALeftCol, ATopRow, ARightCol, ABottomRow, AFormat: Integer);
var
i, j: Integer;
begin
with cxSpreadBook.ActiveSheet do // using our active page
for i := ALeftCol to ARightCol do // for each column specified
for j := ATopRow to ABottomRow do // for each row specified
with GetCellObject(i, j) do // get the cell
try
Style.Format := AFormat;
finally
Free; // free it
end;
end;
procedure TFormMain.SetCellAlignment(ALeftCol, ATopRow, ARightCol, ABottomRow: Integer;
AHorzAlign: TcxHorzTextAlign; AVertAlign: TcxVertTextAlign);
var
i, j: Integer;
begin
with cxSpreadBook.ActiveSheet do // using our active page
for i := ALeftCol to ARightCol do // for each column specified
for j := ATopRow to ABottomRow do // for each row specified
with GetCellObject(i, j) do // get the cell
try
Style.HorzTextAlign := AHorzAlign;
Style.VertTextAlign := AVertAlign;
finally
Free; // free it
end;
end;
procedure TFormMain.SetCellBorders(ALeftCol, ATopRow, ARightCol, ABottomRow: Integer;
AEdge: Integer; AStyle: TcxSSEdgeLineStyle);
var
i, j: Integer;
begin
with cxSpreadBook.ActiveSheet do // using our active page
for i := ALeftCol to ARightCol do // for each column specified
for j := ATopRow to ABottomRow do // for each row specified
with GetCellObject(i, j) do // get the cell
try
case AEdge of // depending on which edge has been requested
0: Style.Borders.Left.Style := AStyle; // set to the specified style
1: Style.Borders.Top.Style := AStyle;
2: Style.Borders.Right.Style := AStyle;
3: Style.Borders.Bottom.Style := AStyle;
end;
finally
Free; // free it
end;
end;
procedure TFormMain.SetCellBorders(ALeftCol, ATopRow, ARightCol, ABottomRow: Integer;
AEdge: TAlign; AStyle: TcxSSEdgeLineStyle);
var
i, j: Integer;
begin
if AEdge <> alNone then
with cxSpreadBook.ActiveSheet do // using our active page
for i := ALeftCol to ARightCol do // for each column specified
for j := ATopRow to ABottomRow do // for each row specified
with GetCellObject(i, j) do // get the cell
try
case AEdge of // depending on which edge has been requested
alTop:
Style.Borders.Top.Style := AStyle;
alBottom:
Style.Borders.Bottom.Style := AStyle;
alLeft:
Style.Borders.Left.Style := AStyle;
alRight:
Style.Borders.Right.Style := AStyle;
alClient:
begin
Style.Borders.Left.Style := AStyle;
Style.Borders.Top.Style := AStyle;
Style.Borders.Right.Style := AStyle;
Style.Borders.Bottom.Style := AStyle;
end;
end;
finally
Free; // free it
end;
end;
type
TcxSSBookSheetPro = class(TcxSSBookSheet);
TcxSSCellObjectPro = class(TcxSSCellObject);
TcxSSDataStoragePro = class(TcxSSDataStorage);
//
// Загрузка данных из XLS файла
//
procedure TFormMain.BFileGetClick(Sender: TObject);
var
i, CurRow: Integer;
//CellObject: TcxSSCellObject;
SID, sName, sCount, sPalm, sDelim{, sArt}: string;
CHeader, RHeader: TcxSSHeader;
vCursor: TCursor;
vDataStorage, vDataStorageBook: TcxSSDataStoragePro;
//vCellObject: TcxSSCellObject;
//vCellRect: TcxSSCellRec;
begin
if OD.Execute then
begin
fIsFileLoaded := False;
LFileName.Caption := '';
vCursor := Screen.Cursor;
Screen.Cursor := crHourGlass;
cxSpreadBook.BeginUpdate;
try
cxSpreadSheet.Sheet.ClearAll();
cxSpreadBook.ActivePage := 0;
cxSpreadBook.ActiveSheet.DeleteCells(Rect(0, 0, cxSpreadBook.ActiveSheet.ColumnCount,
cxSpreadBook.ActiveSheet.RowCount), msAllRow);
CurRow := 0;
SetCellText(0, CurRow, 'Код товара');
SetCellText(1, CurRow, 'Наименование товара');
SetCellText(2, CurRow, 'Количество');
SetCellText(3, CurRow, 'Строка для Palm-а');
SetCellFont(0, 0, 3, 0, [fsBold], 9);
SetCellFont(3, 0, 3, 0, [fsBold], 9, clGray);
SetCellPattern(0, 0, 3, 0, {BackgroundColor:}18 , {ForeGroundColor:}1, fsSolid);
SetCellPattern(3, 0, 3, 0, {BackgroundColor:}23 , {ForeGroundColor:}1, fsSolid);
//TcxSSEdgeLineStyle = (lsDefault, lsThin, lsMedium, lsDashed, lsDotted,
// lsThick, lsDouble, lsHair, lsMediumDashed, lsDashDot, lsMediumDashDot,
// lsDashDotDot, lsMediumDashDotDot, lsSlantedDashDot, lsNone);
SetCellBorders(0, 0, 3, 0, {Edge:}alTop, {Style:}lsDouble);
SetCellBorders(0, 0, 3, 0, {Edge:}alBottom, {Style:}lsThin);
SetCellBorders(0, 0, 0, 0, {Edge:}alLeft, {Style:}lsDouble);
SetCellBorders(3, 0, 3, 0, {Edge:}alRight, {Style:}lsThin);
SetCellBorders(0, 0, 2, 0, {Edge:}alRight, {Style:}lsHair); //lsHair
CHeader := cxSpreadBook.ActiveSheet.Cols;
CHeader.Size[0] := 100;
CHeader.Size[1] := 280;
CHeader.Size[2] := 90;
CHeader.Size[3] := 420;
RHeader := cxSpreadBook.ActiveSheet.Rows;
RHeader.Size[0] := 30;
cxSpreadSheet.LoadFromFile(OD.FileName);
try
if cxSpreadSheet.Sheet.RowCount = 0 then
begin
MessageDlg('Нет строк для загрузки', mtError, [mbOk], 0);
Exit;
end;
if cxSpreadSheet.Sheet.ColumnCount < 3 then
begin
MessageDlg('Недостаточно колонок для загрузки. Должно быть не менее 3-х. Учитываютя первые 3.', mtError,
[mbOk],
0);
Exit;
end;
sDelim := (CDelimiter.Text + ',')[1];
vDataStorage := TcxSSDataStoragePro(TcxSSBookSheetPro(cxSpreadSheet.Sheet).DataStorage);
vDataStorageBook := TcxSSDataStoragePro(TcxSSBookSheetPro(cxSpreadBook.ActiveSheet).DataStorage);
//vCellObject := cxSpreadSheet.Sheet.GetCellObject(0, 0);
//vCell := vDataStorage[0, 0];
for i := 0 to cxSpreadSheet.Sheet.RowCount - 1 do
begin
//
// 1) Читаем входной фал
//
//CellObject := cxSpreadSheet.Sheet.GetCellObject(0, i);
//SID := Trim(CellObject.Text);
SID := Trim(vDataStorage[0, i].Text);
//CellObject.Free;
if StrToIntDef(SID, -1) < 0 then
Continue;
//CellObject := cxSpreadSheet.Sheet.GetCellObject(1, i);
//sName := Trim(CellObject.Text);
sName := Trim(vDataStorage[1, i].Text);
if sName = '' then
Continue;
//CellObject.Free;
//CellObject := cxSpreadSheet.Sheet.GetCellObject(2, i);
//sCount := Trim(CellObject.Text);
sCount := Trim(vDataStorage[2, i].Text);
//CellObject.Free;
if StrToIntDef(sCount, -1) < 0 then
Continue;
// ---
//
// 2) Проверенные строки входного файла запоминаем + формируем строку для экспорта в Палм
//
Inc(CurRow);
//SetCellText(0, CurRow, SID);
vDataStorageBook.SetCellTextEx(0, CurRow, sID, {IsFormula:}False, {Analyse:} False);
//SetCellText(1, CurRow, sName);
vDataStorageBook.SetCellTextEx(1, CurRow, sName, {IsFormula:}False, {Analyse:} False);
//SetCellText(2, CurRow, sCount);
vDataStorageBook.SetCellTextEx(2, CurRow, sCount, {IsFormula:}False, {Analyse:} False);
// Строка для палм-а
sPalm := SID + sDelim
//+ sCount + ' * '
+ sCount + ' **' // skip articul
//+ sArt + ' * '
+ FxtCyrToLat(StringReplace(sName, sDelim, '', [rfReplaceAll, rfIgnoreCase]));
if Length(sPalm) > 60 then
SetLength(sPalm, 60);
//SetCellText(3, CurRow, sPalm);
vDataStorageBook.SetCellTextEx(3, CurRow, sPalm, {IsFormula:}False, {Analyse:} False);
SetCellPattern(3, CurRow, 3, CurRow, {BackgroundColor:}34 , {ForeGroundColor:}1, fsSolid);
// ---
end; // of for;
finally
cxSpreadSheet.Sheet.ClearAll();
end;
fIsFileLoaded := CurRow > 0;
if fIsFileLoaded then
begin
fDelimiterCharLoaded := (CDelimiter.Text + ',')[1];
LFileName.Caption := OD.FileName;
SetCellBorders(0, 1, 0, CurRow, {Edge:}alLeft, {Style:}lsDouble);
SetCellBorders(3, 1, 3, CurRow, {Edge:}alRight, {Style:}lsThin);
SetCellBorders(0, CurRow, 3, CurRow, {Edge:}alBottom, {Style:}lsThin);
SetCellBorders(0, 1, 2, CurRow, {Edge:}alRight, {Style:}lsHair);
end;
finally
cxSpreadBook.EndUpdate;
Screen.Cursor := vCursor;
end;
end;
end;
//
// Передача данных в палм
//
...
procedure TFormMain.BPalmSetClick(Sender: TObject);
var
vCursor: TCursor;
iRow, iRowCount: Integer;
sPalm: string;
vDataStorageBook: TcxSSDataStoragePro;
vDataLines: TStringList;
begin
if fIsFileLoaded then
begin
vCursor := Screen.Cursor;
Screen.Cursor := crHourGlass;
vDataLines := nil;
cxSpreadBook.BeginUpdate;
try
cxSpreadBook.ActivePage := 0;
iRowCount := cxSpreadBook.ActiveSheet.RowCount;
vDataStorageBook := TcxSSDataStoragePro(TcxSSBookSheetPro(cxSpreadBook.ActiveSheet).DataStorage);
vDataLines := TStringList.Create;
for iRow := 1 to iRowCount - 1 do
begin
sPalm := vDataStorageBook[3, iRow].Text;
if sPalm <> '' then
begin
vDataLines.Add(sPalm);
end;
end;
if not PalmSymbol_SetScanData(
{DataLines:}vDataLines,
{ComPortNum:} CComPort.ItemIndex + 1,
{DelimiterChar:} fDelimiterCharLoaded,
{DataTranPath:} EDataTran.Text,
) then
EPalmSymbol.Create('Ошибка при передаче данных < Palm "symbol">');
finally
cxSpreadBook.EndUpdate;
Screen.Cursor := vCursor;
vDataLines.Free;
end;
//cxSpreadSheet.Sheet.Cols
//cxSpreadSheet.Sheet.ColumnCount
//cxSpreadSheet.Sheet.ContentColCount
//cxSpreadSheet.Sheet.RowCount
//cxSpreadSheet.Sheet.ContentRowCount
end;
end;
//
// Приём данных с палм
//
...
procedure TFormMain.BPalmGetClick(Sender: TObject);
var
vCursor: TCursor;
//--
S: string;
i, iCount: Integer;
vCallBackData: TPalmSymbolCallBackData;
IsDataLines: Boolean;
vDataLines: TStringList; // Полные строки из палм-а
vSerial: TStringList; // Список серийников
vG_ID: TStringList; // Список кодов товаров
vG_COUNT: TStringList; // Количество в товарной позиции
iG_ID: Currency;
//--
vDataStorageBook: TcxSSDataStoragePro;
CHeader, RHeader: TcxSSHeader;
CurRow: Integer;
begin
vG_ID := nil;
vG_COUNT := nil;
vDataLines := nil;
vSerial := nil;
vCursor := Screen.Cursor;
Screen.Cursor := crHourGlass;
try
vDataLines := TStringList.Create;
//
// debug only:
// debug:
//
//
//vDataLines.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'palm_symbol_scan_codes_tmp.txt');
//
//
// debug.
//
vSerial := TStringList.Create;
vG_ID := TStringList.Create;
vG_COUNT := TStringList.Create;
vCallBackData.vG_ID := vG_ID;
vCallBackData.vG_COUNT := vG_COUNT;
vCallBackData.ValidCount := 0;
iCount := 0;
IsDataLines := vDataLines.Count > 0;
//
// Запуск процесса приёма из palm или из файла
//
fIsFileSaved := PalmSymbol_GetScanData(False, vSerial, vDataLines,
CComPort.ItemIndex + 1, CDelimiter.Text[1], EDataTran.Text,
@vCallBackData, PalmSymbolParse_DS_ID, IsDataLines
);
if fIsFileSaved and (vCallBackData.ValidCount > 0) then
begin
//
// Обработка полученных серийных номеров: удаляем невалидные записи
//
for i := vG_ID.Count - 1 downto 0 do
begin
// товар
S := vG_ID[i];
iG_ID := StrToCurrDef(S, -1);
if iG_ID <= 0 then
begin
vG_ID.Delete(i);
vSerial.Delete(i);
vG_COUNT.Delete(i);
vDataLines.Delete(i);
end
else
Inc(iCount);
end;
end;
if iCount > 0 then
begin
// Обработка серийников товарных позиций: Напоняем XLS закладку
//
//
cxSpreadBook.BeginUpdate;
try
//
// удаляем старое
//
cxSpreadBook.ActivePage := 1;
cxSpreadBook.ActiveSheet.DeleteCells(Rect(0, 0, cxSpreadBook.ActiveSheet.ColumnCount,
cxSpreadBook.ActiveSheet.RowCount), msAllRow);
//
// заголовок
//
CurRow := 0;
SetCellText(0, CurRow, 'Код товара');
SetCellText(1, CurRow, 'Скан код');
SetCellText(2, CurRow, 'Количество');
SetCellText(3, CurRow, 'Строка из Palm-а');
SetCellFont(0, 0, 3, 0, [fsBold], 9);
SetCellFont(3, 0, 3, 0, [fsBold], 9, clGray);
SetCellPattern(0, 0, 3, 0, {BackgroundColor:}18 , {ForeGroundColor:}1, fsSolid);
SetCellPattern(1, 0, 1, 0, {BackgroundColor:}21 , {ForeGroundColor:}1, fsSolid);
SetCellPattern(3, 0, 3, 0, {BackgroundColor:}44 , {ForeGroundColor:}1, fsSolid);
//TcxSSEdgeLineStyle = (lsDefault, lsThin, lsMedium, lsDashed, lsDotted,
// lsThick, lsDouble, lsHair, lsMediumDashed, lsDashDot, lsMediumDashDot,
// lsDashDotDot, lsMediumDashDotDot, lsSlantedDashDot, lsNone);
SetCellBorders(0, 0, 3, 0, {Edge:}alTop, {Style:}lsDouble);
SetCellBorders(0, 0, 3, 0, {Edge:}alBottom, {Style:}lsThin);
SetCellBorders(0, 0, 0, 0, {Edge:}alLeft, {Style:}lsDouble);
SetCellBorders(3, 0, 3, 0, {Edge:}alRight, {Style:}lsThin);
SetCellBorders(0, 0, 1, 0, {Edge:}alRight, {Style:}lsHair); //lsHair
CHeader := cxSpreadBook.ActiveSheet.Cols;
CHeader.Size[0] := 100;
CHeader.Size[1] := 250;
CHeader.Size[2] := 90;
CHeader.Size[3] := 520;
RHeader := cxSpreadBook.ActiveSheet.Rows;
RHeader.Size[0] := 30;
//
// строки
//
vDataStorageBook := TcxSSDataStoragePro(TcxSSBookSheetPro(cxSpreadBook.ActiveSheet).DataStorage);
for i := 0 to vG_ID.Count - 1 do
begin
Inc(CurRow);
// товар
S := vG_ID[i];
vDataStorageBook.SetCellTextEx(0, CurRow, S, {IsFormula:}False, {Analyse:} False);
// штрих код
S := vSerial[i];
vDataStorageBook.SetCellTextEx(1, CurRow, S, {IsFormula:}False, {Analyse:} False);
// количество
S := vG_COUNT[i];
vDataStorageBook.SetCellTextEx(2, CurRow, S, {IsFormula:}False, {Analyse:} False);
// вся строка
S := vDataLines[i];
vDataStorageBook.SetCellTextEx(3, CurRow, S, {IsFormula:}False, {Analyse:} False);
SetCellPattern(3, CurRow, 3, CurRow, {BackgroundColor:}39 , {ForeGroundColor:}1, fsSolid);
end;
SetCellBorders(0, 1, 0, CurRow, {Edge:}alLeft, {Style:}lsDouble);
SetCellBorders(3, 1, 3, CurRow, {Edge:}alRight, {Style:}lsThin);
SetCellBorders(0, CurRow, 3, CurRow, {Edge:}alBottom, {Style:}lsThin);
SetCellBorders(0, 1, 2, CurRow, {Edge:}alRight, {Style:}lsHair);
//TcxHorzTextAlign = (haGENERAL, haLEFT, haCENTER, haRIGHT, haFILL, haJUSTIFY);
//TcxVertTextAlign = (vaTOP, vaCENTER, vaBOTTOM, vaJUSTIFY);
SetCellAlignment(1, 1, 1, CurRow,
{HorzAlign:} haLEFT, {VertAlign:} vaCENTER);
finally
cxSpreadBook.EndUpdate;
end;
end;
//
// Итого загружено
//
FxtMessageBox(Handle, Format('Загружено "%s" товарных позиций.', [IntToStr(iCount)]),
'Обмен данными с < palm "symbol" >', MB_ICONINFORMATION or MB_OK);
finally
Screen.Cursor := vCursor;
vDataLines.Free;
vSerial.Free;
vG_ID.Free;
vG_COUNT.Free;
end;
end;
[/more]