dephi 翻译成bcb cxgrid主从导出

qq_18533341 2014-07-28 07:46:08
//主要想实现从cxgrid主从表导出到excel。



procedure TFormScheduleQuery.cxButtonExportClick(Sender: TObject);
const
xlNone = $FFFFEFD2;
xlAutomatic = $FFFFEFF7;
xlSolid = $00000001;

xlInsideHorizontal = $0000000C;
xlInsideVertical = $0000000B;
xlDiagonalDown = $00000005;
xlDiagonalUp = $00000006;
xlEdgeBottom = $00000009;
xlEdgeLeft = $00000007;
xlEdgeRight = $0000000A;
xlEdgeTop = $00000008;

xlContinuous = $00000001;
xlDash = $FFFFEFED;
xlDashDot = $00000004;
xlDashDotDot = $00000005;
xlDot = $FFFFEFEA;
xlDouble = $FFFFEFE9;
xlSlantDashDot = $0000000D;
xlLineStyleNone = $FFFFEFD2;

xlHairline = $00000001;
xlMedium = $FFFFEFD6;
xlThick = $00000004;
xlThin = $00000002;

xlCenter = $FFFFEFF4;
xlBottom = $FFFFEFF5;
xlLeft = $FFFFEFDD;
xlRight = $FFFFEFC8;
xlTop = $FFFFEFC0;
var
FileName, s: String;
xlApp, xlWorkbook, xlWorkSheet, xlRange: OleVariant;
i, j, RowIndex, MasterRecordIndex, TitleRows: Integer;
TableView, MainView, DetailView: TcxGridDBTableView;
DetailVisible: Boolean;
RangeBorders: array of Cardinal;
begin
//导出
if Self.tvWorkCenterSchedule.DataController.RecordCount = 0 then
begin
if Language = Chinese then
s := '没有导出数据...'
else
s := 'No data to export!';
Application.MessageBox(PChar(s), PChar(Application.Title),
MB_OK or MB_ICONERROR);
Exit;
end;

if Language = Chinese then
begin
FormMain.SaveDialogExport.Filter := 'Excel文件 (*.xls)';
FormMain.SaveDialogExport.Title := '导出为';
end
else
begin
FormMain.SaveDialogExport.Filter := 'Excel File (*.xls)';
FormMain.SaveDialogExport.Title := 'Export As';
end;

if not FormMain.SaveDialogExport.Execute then
Exit;

FileName := FormMain.SaveDialogExport.FileName;
Application.ProcessMessages;

try
xlApp := CreateOleObject('Excel.Application');
except
Application.MessageBox('无法创建Excel文件, 请确认是否安装了Excel软件',
PChar(Application.Title), MB_OK + MB_ICONWarning);
Exit;
end;
Screen.Cursor := crHourGlass;
Application.ProcessMessages;

xlApp.Visible := False;
xlApp.DisplayAlerts := False;
xlApp.ScreenUpdating := False;

xlWorkBook := xlApp.WorkBooks.Add;
xlWorkSheet := xlWorkBook.WorkSheets[1];
xlWorkSheet.Name := 'WorkCenter Plan';

TableView := TcxGridDBTableView(Self.cxGrid1.FocusedView);
if TableView.IsMaster then
begin
MainView := TableView;
MasterRecordIndex := MainView.DataController.FocusedRecordIndex;
with MainView.DataController do
DetailView := TcxGridDBTableView(TcxGridDataController(GetDetailDataController(FocusedRecordIndex, 0)).GridView);
end
else
begin
DetailView := TableView;
MainView := TcxGridDBTableView(TcxGridDataController(DetailView.DataController.GetMasterDataController).GridView);
MasterRecordIndex := DetailView.DataController.GetMasterRecordIndex;
end;

TitleRows := 3;
RowIndex := TitleRows + 1;
xlWorkSheet.Rows[RowIndex].Font.Name := '宋体';
xlWorkSheet.Rows[RowIndex].Font.Bold := True;
xlWorkSheet.Rows[RowIndex].Font.Size := 9;
xlWorkSheet.Rows[RowIndex].WrapText := True;

for i := 0 to DetailView.VisibleColumnCount - 1 do
xlWorkSheet.Cells[RowIndex, i + 1].Value := VarToStr(DetailView.VisibleColumns[i].Caption);

//设置标题行
xlWorkSheet.Cells[RowIndex, 1].AutoFilter;

xlRange := xlWorkSheet.Range[xlWorkSheet.Cells[RowIndex, 1], xlWorkSheet.Cells[RowIndex, DetailView.VisibleColumnCount]];
xlRange.Interior.ColorIndex := 37;
xlRange.Interior.Pattern := xlSolid;
xlRange.Interior.PatternColorIndex := xlAutomatic;

//设置列格式
xlWorkSheet.Columns[DetailView.GetColumnByFieldName('OrderID').VisibleIndex + 1].NumberFormatLocal := '@';
xlWorkSheet.Columns[DetailView.GetColumnByFieldName('ItemID').VisibleIndex + 1].NumberFormatLocal := '@';
xlWorkSheet.Columns[DetailView.GetColumnByFieldName('StartDate').VisibleIndex + 1].NumberFormatLocal := 'yyyy-mm-dd hh:mm;@'; //yyyy-m-d h:mm;@
xlWorkSheet.Columns[DetailView.GetColumnByFieldName('StopDate').VisibleIndex + 1].NumberFormatLocal := 'yyyy-mm-dd hh:mm;@';

for i := 0 to DetailView.DataController.RecordCount - 1 do
begin
Inc(RowIndex);
xlWorkSheet.Rows[RowIndex].Font.Name := '宋体';
xlWorkSheet.Rows[RowIndex].Font.Size := 9;

for j := 0 to DetailView.VisibleColumnCount - 1 do
xlWorkSheet.Cells[RowIndex, j + 1].Value := VarToStr(DetailView.DataController.Values[i, DetailView.VisibleColumns[j].Index]);
end;

for j := 0 to DetailView.VisibleColumnCount - 1 do
xlWorkSheet.Columns[j + 1].EntireColumn.AutoFit;

RowIndex := 1;
xlWorkSheet.Rows[RowIndex].Font.Name := '宋体';
xlWorkSheet.Rows[RowIndex].Font.Bold := True;
xlWorkSheet.Rows[RowIndex].Font.Size := 18;

xlRange := xlWorkSheet.Range[xlWorkSheet.Cells[RowIndex, 1], xlWorkSheet.Cells[RowIndex, DetailView.VisibleColumnCount]];
xlRange.HorizontalAlignment := xlCenter;
xlRange.VerticalAlignment := xlCenter;
xlRange.ShrinkToFit := False;
xlRange.MergeCells := True;
xlWorkSheet.Cells[RowIndex, 1].Value := 'WorkCenter Plan';

Inc(RowIndex);
xlWorkSheet.Rows[RowIndex].Font.Name := '宋体';
xlWorkSheet.Rows[RowIndex].Font.Size := 9;

xlRange := xlWorkSheet.Range[xlWorkSheet.Cells[RowIndex, 1], xlWorkSheet.Cells[RowIndex, DetailView.VisibleColumnCount]];
xlRange.HorizontalAlignment := xlRight;
xlRange.VerticalAlignment := xlCenter;
xlRange.ShrinkToFit := False;
xlRange.MergeCells := True;
with MainView.DataController do
xlWorkSheet.Cells[RowIndex, 1].Value := '计划日期: ' + VarToStr(Values[MasterRecordIndex, GetItemByFieldName('StartDate').Index])
+ ' 到 ' + VarToStr(Values[MasterRecordIndex, GetItemByFieldName('StopDate').Index]);

xlApp.ActiveWindow.SplitRow := TitleRows + 1;
//xlApp.ActiveWindow.SplitColumn = 1
xlApp.ActiveWindow.FreezePanes := True;
xlApp.ActiveWindow.DisplayGridlines := False;

xlRange := xlWorkSheet.Range[xlWorkSheet.Cells[TitleRows + 1, 1],
xlWorkSheet.Cells[TitleRows + 1 + DetailView.DataController.RecordCount, DetailView.VisibleColumnCount]];
xlRange.Borders[xlDiagonalDown].LineStyle := xlNone;
xlRange.Borders[xlDiagonalUp].LineStyle := xlNone;
SetLength(RangeBorders, 6);
RangeBorders[0] := xlEdgeBottom;
RangeBorders[1] := xlEdgeLeft;
RangeBorders[2] := xlEdgeRight;
RangeBorders[3] := xlEdgeTop;
RangeBorders[4] := xlInsideHorizontal;
RangeBorders[5] := xlInsideVertical;
for i := low(RangeBorders) to high(RangeBorders) do
begin
xlRange.Borders[RangeBorders[i]].LineStyle := xlContinuous;
xlRange.Borders[RangeBorders[i]].Weight := xlThin;
xlRange.Borders[RangeBorders[i]].ColorIndex := xlAutomatic;
end;

xlWorkBook.SaveAs(FileName);
xlWorkBook.Close;
xlWorkBook := Unassigned;
xlWorkSheet := Unassigned;
xlRange := Unassigned;
xlApp.Quit;
xlApp := Unassigned;
Application.ProcessMessages;

Screen.Cursor := crDefault;
Application.ProcessMessages;

if Language = Chinese then
s := '导出完成...'
else
s := 'Export Succeed!';
Application.MessageBox(PChar(s), PChar(Application.Title),
MB_OK or MB_IconInformation);
end;
...全文
158 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_18533341 2014-07-29
  • 打赏
  • 举报
回复
谢谢 结贴
缘中人 2014-07-29
  • 打赏
  • 举报
回复
新建个pas文件,起名ExportExcel,添加到c++builder工程文件里,包含生成的头文件,调用函数就省得转换了
unit ExportExcel;

interface


uses Vcl.Forms, Vcl.Dialogs;

procedure msg(aval: string);
procedure ExportExcel(TableView, MainView, DetailView: TcxGridDBTableView);

implementation

procedure msg(aval: string);
begin
  ShowMessage(aval);
end;

procedure ExportExcel(TableView, MainView, DetailView: TcxGridDBTableView);
const
  xlNone = $FFFFEFD2;
  xlAutomatic = $FFFFEFF7;
  xlSolid = $00000001;

  xlInsideHorizontal = $0000000C;
  xlInsideVertical = $0000000B;
  xlDiagonalDown = $00000005;
  xlDiagonalUp = $00000006;
  xlEdgeBottom = $00000009;
  xlEdgeLeft = $00000007;
  xlEdgeRight = $0000000A;
  xlEdgeTop = $00000008;

  xlContinuous = $00000001;
  xlDash = $FFFFEFED;
  xlDashDot = $00000004;
  xlDashDotDot = $00000005;
  xlDot = $FFFFEFEA;
  xlDouble = $FFFFEFE9;
  xlSlantDashDot = $0000000D;
  xlLineStyleNone = $FFFFEFD2;

  xlHairline = $00000001;
  xlMedium = $FFFFEFD6;
  xlThick = $00000004;
  xlThin = $00000002;

  xlCenter = $FFFFEFF4;
  xlBottom = $FFFFEFF5;
  xlLeft = $FFFFEFDD;
  xlRight = $FFFFEFC8;
  xlTop = $FFFFEFC0;
var
  FileName, s: String;
  xlApp, xlWorkbook, xlWorkSheet, xlRange: OleVariant;
  i, j, RowIndex, MasterRecordIndex, TitleRows: Integer;
  DetailVisible: Boolean;
  RangeBorders: array of Cardinal;
begin
  // 导出
  if Self.tvWorkCenterSchedule.DataController.RecordCount = 0 then
  begin

    s := '没有导出数据...';

    Application.MessageBox(PChar(s), PChar(Application.Title), MB_OK or MB_ICONERROR);
    Exit;
  end;

  dlgSaveExport.Filter := 'Excel文件 (*.xls)';
  dlgSaveExport.Title := '导出为';

  if not dlgSaveExport.Execute then
    Exit;

  FileName := dlgSaveExport.FileName;
  Application.ProcessMessages;

  try
    xlApp := CreateOleObject('Excel.Application');
  except
    Application.MessageBox('无法创建Excel文件, 请确认是否安装了Excel软件', PChar(Application.Title), MB_OK + MB_ICONWarning);
    Exit;
  end;
  Screen.Cursor := crHourGlass;
  Application.ProcessMessages;

  xlApp.Visible := False;
  xlApp.DisplayAlerts := False;
  xlApp.ScreenUpdating := False;

  xlWorkbook := xlApp.WorkBooks.Add;
  xlWorkSheet := xlWorkbook.WorkSheets[1];
  xlWorkSheet.Name := 'WorkCenter Plan';

  TableView := TcxGridDBTableView(Self.cxGrid1.FocusedView);
  if TableView.IsMaster then
  begin
    MainView := TableView;
    MasterRecordIndex := MainView.DataController.FocusedRecordIndex;
    with MainView.DataController do
      DetailView := TcxGridDBTableView(TcxGridDataController(GetDetailDataController(FocusedRecordIndex, 0)).GridView);
  end
  else
  begin
    DetailView := TableView;
    MainView := TcxGridDBTableView(TcxGridDataController(DetailView.DataController.GetMasterDataController).GridView);
    MasterRecordIndex := DetailView.DataController.GetMasterRecordIndex;
  end;

  TitleRows := 3;
  RowIndex := TitleRows + 1;
  xlWorkSheet.Rows[RowIndex].Font.Name := '宋体';
  xlWorkSheet.Rows[RowIndex].Font.Bold := True;
  xlWorkSheet.Rows[RowIndex].Font.Size := 9;
  xlWorkSheet.Rows[RowIndex].WrapText := True;

  for i := 0 to DetailView.VisibleColumnCount - 1 do
    xlWorkSheet.Cells[RowIndex, i + 1].Value := VarToStr(DetailView.VisibleColumns[i].Caption);

  // 设置标题行
  xlWorkSheet.Cells[RowIndex, 1].AutoFilter;

  xlRange := xlWorkSheet.Range[xlWorkSheet.Cells[RowIndex, 1], xlWorkSheet.Cells[RowIndex, DetailView.VisibleColumnCount]];
  xlRange.Interior.ColorIndex := 37;
  xlRange.Interior.Pattern := xlSolid;
  xlRange.Interior.PatternColorIndex := xlAutomatic;

  // 设置列格式
  xlWorkSheet.Columns[DetailView.GetColumnByFieldName('OrderID').VisibleIndex + 1].NumberFormatLocal := '@';
  xlWorkSheet.Columns[DetailView.GetColumnByFieldName('ItemID').VisibleIndex + 1].NumberFormatLocal := '@';
  xlWorkSheet.Columns[DetailView.GetColumnByFieldName('StartDate').VisibleIndex + 1].NumberFormatLocal := 'yyyy-mm-dd hh:mm;@'; // yyyy-m-d h:mm;@
  xlWorkSheet.Columns[DetailView.GetColumnByFieldName('StopDate').VisibleIndex + 1].NumberFormatLocal := 'yyyy-mm-dd hh:mm;@';

  for i := 0 to DetailView.DataController.RecordCount - 1 do
  begin
    Inc(RowIndex);
    xlWorkSheet.Rows[RowIndex].Font.Name := '宋体';
    xlWorkSheet.Rows[RowIndex].Font.Size := 9;

    for j := 0 to DetailView.VisibleColumnCount - 1 do
      xlWorkSheet.Cells[RowIndex, j + 1].Value := VarToStr(DetailView.DataController.Values[i, DetailView.VisibleColumns[j].Index]);
  end;

  for j := 0 to DetailView.VisibleColumnCount - 1 do
    xlWorkSheet.Columns[j + 1].EntireColumn.AutoFit;

  RowIndex := 1;
  xlWorkSheet.Rows[RowIndex].Font.Name := '宋体';
  xlWorkSheet.Rows[RowIndex].Font.Bold := True;
  xlWorkSheet.Rows[RowIndex].Font.Size := 18;

  xlRange := xlWorkSheet.Range[xlWorkSheet.Cells[RowIndex, 1], xlWorkSheet.Cells[RowIndex, DetailView.VisibleColumnCount]];
  xlRange.HorizontalAlignment := xlCenter;
  xlRange.VerticalAlignment := xlCenter;
  xlRange.ShrinkToFit := False;
  xlRange.MergeCells := True;
  xlWorkSheet.Cells[RowIndex, 1].Value := 'WorkCenter Plan';

  Inc(RowIndex);
  xlWorkSheet.Rows[RowIndex].Font.Name := '宋体';
  xlWorkSheet.Rows[RowIndex].Font.Size := 9;

  xlRange := xlWorkSheet.Range[xlWorkSheet.Cells[RowIndex, 1], xlWorkSheet.Cells[RowIndex, DetailView.VisibleColumnCount]];
  xlRange.HorizontalAlignment := xlRight;
  xlRange.VerticalAlignment := xlCenter;
  xlRange.ShrinkToFit := False;
  xlRange.MergeCells := True;
  with MainView.DataController do
    xlWorkSheet.Cells[RowIndex, 1].Value := '计划日期: ' + VarToStr(Values[MasterRecordIndex, GetItemByFieldName('StartDate').Index]) + ' 到 ' +
      VarToStr(Values[MasterRecordIndex, GetItemByFieldName('StopDate').Index]);

  xlApp.ActiveWindow.SplitRow := TitleRows + 1;
  // xlApp.ActiveWindow.SplitColumn = 1
  xlApp.ActiveWindow.FreezePanes := True;
  xlApp.ActiveWindow.DisplayGridlines := False;

  xlRange := xlWorkSheet.Range[xlWorkSheet.Cells[TitleRows + 1, 1], xlWorkSheet.Cells[TitleRows + 1 + DetailView.DataController.RecordCount,
    DetailView.VisibleColumnCount]];
  xlRange.Borders[xlDiagonalDown].LineStyle := xlNone;
  xlRange.Borders[xlDiagonalUp].LineStyle := xlNone;
  SetLength(RangeBorders, 6);
  RangeBorders[0] := xlEdgeBottom;
  RangeBorders[1] := xlEdgeLeft;
  RangeBorders[2] := xlEdgeRight;
  RangeBorders[3] := xlEdgeTop;
  RangeBorders[4] := xlInsideHorizontal;
  RangeBorders[5] := xlInsideVertical;
  for i := low(RangeBorders) to high(RangeBorders) do
  begin
    xlRange.Borders[RangeBorders[i]].LineStyle := xlContinuous;
    xlRange.Borders[RangeBorders[i]].Weight := xlThin;
    xlRange.Borders[RangeBorders[i]].ColorIndex := xlAutomatic;
  end;

  xlWorkbook.SaveAs(FileName);
  xlWorkbook.Close;
  xlWorkbook := Unassigned;
  xlWorkSheet := Unassigned;
  xlRange := Unassigned;
  xlApp.Quit;
  xlApp := Unassigned;
  Application.ProcessMessages;

  Screen.Cursor := crDefault;
  Application.ProcessMessages;

  s := '导出完成...';

  Application.MessageBox(PChar(s), PChar(Application.Title), MB_OK or MB_IconInformation);
end;

end.
ccrun.com 2014-07-29
  • 打赏
  • 举报
回复
我现在基本不用三方组件。单纯就翻译Delphi代码为C++代码,这个没有太多的难度,只是语法上的差异而已。
qq_18533341 2014-07-29
  • 打赏
  • 举报
回复
引用 3 楼 ccrun 的回复:
楼上代码中语法错误较多,可作为学生改错的试题。
谢谢妖哥回复。 代码是csdn上 我 主要是想实现 cxgrid 主从表 导出到excel 。 用cxgrid自带的导出函数只能导出主表或者从表,因为看过妖哥的dbgrid2excel。 所以邀请妖哥回答了。
ccrun.com 2014-07-28
  • 打赏
  • 举报
回复
楼上代码中语法错误较多,可作为学生改错的试题。
xjq2003 2014-07-28
  • 打赏
  • 举报
回复
var
  FileName, s: String;
  xlApp, xlWorkbook, xlWorkSheet, xlRange: OleVariant;
  i, j, RowIndex, MasterRecordIndex, TitleRows: Integer;
  TableView, MainView, DetailView: TcxGridDBTableView;
  DetailVisible: Boolean;
  RangeBorders: array of Cardinal;
begin
  //导出
  if Self.tvWorkCenterSchedule.DataController.RecordCount = 0 then
  begin
    if Language = Chinese then
      s := '没有导出数据...'
    else
      s := 'No data to export!';
    Application.MessageBox(PChar(s), PChar(Application.Title),
      MB_OK or MB_ICONERROR);
    Exit;
  end;

  if Language = Chinese then
  begin
    FormMain.SaveDialogExport.Filter := 'Excel文件 (*.xls)';
    FormMain.SaveDialogExport.Title := '导出为';
  end
  else
  begin
    FormMain.SaveDialogExport.Filter := 'Excel File (*.xls)';
    FormMain.SaveDialogExport.Title := 'Export As';
  end;
抛砖


  String FileName, s;
  OleVariant xlApp, xlWorkbook, xlWorkSheet, xlRange;
  int i, j, RowIndex, MasterRecordIndex, TitleRows;
  TcxGridDBTableView TableView, MainView, DetailView;
  Bool DetailVisible;
  Cardinal RangeBorders[];
begin
  //导出
  if (this->tvWorkCenterSchedule->DataController>RecordCount == 0)
 {
    if (Language == Chinese)
      s = "没有导出数据...";
    else
    {  s = "No data to export!";
    Application->MessageBox(s, Application->Title,
      MB_OK | MB_ICONERROR);
    return;
    };
};

  if( Language == Chinese )
{ // begin
    FormMain->SaveDialogExport.Filter = "Excel文件 (*.xls)";
    FormMain->SaveDialogExport.Title = "导出为";
}//  end
  else
{  //begin
    FormMain->SaveDialogExport.Filter = "Excel File (*.xls)";
    FormMain->SaveDialogExport.Title = "Export As";
}//  end;


缘中人 2014-07-28
  • 打赏
  • 举报
回复
这代码也太长了吧,加分
勉励前行 2014-07-28
  • 打赏
  • 举报
回复
http://delphi-to-c-builder.updatestar.com/
勉励前行 2014-07-28
  • 打赏
  • 举报
回复
Delphi to C++Builder 网上一找,真有这么个软件。没用过不知道能行否,但应该能省一些工作量吧。

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧