您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> Delphi >> TExcelApplication的问题

TExcelApplication的问题

来源:网络整理     时间:2016/7/31 9:43:25     关键词:

关于网友提出的“ TExcelApplication的问题”问题疑问,本网通过在网上对“ TExcelApplication的问题”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: TExcelApplication的问题
描述:

我做了一个查询,使用TExcelApplication将数据库中的内容全部写到在Excel文件中作为报表输出。请问怎样在Excel文件中画出表格的边框。
谢谢


解决方案1:

//Excel报表的打印
procedure TFmain.Print_Excel(RepID:string);
var
  eaImport : TExcelApplication;//
  ewbImport : TExcelWorkbook;  // v
  sheet : TExcelWorksheet; //sheet
  I,J,IrowCount,tempPos:Integer;//
  tempSQL,tempField,PreField:string;//临时变量
  SqlStr,FileName,FieldStr:string;
  StartCol,StartRow:integer;
begin
  //根据报表号找出SQL语句
  //设计一张表,保存报表的基本数据
  with Q1 do
  begin
    close;
    sql.Clear;
    tempSQL:='select * from syspz_Report where reportId=' + RepId;
    sql.Add(tempSQL);
    open;
    IRowCount:=RecordCount;
    if IRowCount=0 then
    begin
      ShowMess('没有该报表的记录,请确认是否添加了该报表');
      exit;
    end;
    SqlStr :=Tblobfield(fieldByName('SqLStr')).asstring;
    FieldStr:=FieldByName('FieldStr').asstring;//显示字段的字符串
    FileName:=FieldByName('Path').AsString;   //文件名
    StartCol:=FieldByName('StartCol').AsInteger;//起始列
    StartRow:=FieldByName('StartRow').AsInteger;//起始行
  end;
  //往EXCEL写入数据
  with q1 do
  begin
    close;
    sql.Clear;
    sql.Add(SqlStr);
    Open;
    IrowCount:=RecordCount;
    if IRowCount=0 then
    begin
      ShowMess('该报表现在暂时没有你要的数据');
      exit;
    end;
    //打开已存在的报表,插入数据
    eaImport := TExcelApplication.Create(Application);
    try
      eaImport.ConnectKind := ckNewInstance;
      eaImport.Connect;
    except
      showmess('请先安装 Excel !');
      eaImport.Free;
      Exit;
    end;
    try
      eaImport.Workbooks.Open(FileName,null,null,null,null,null,null,null,null,null,null,null,null,0);
      eaImport.Visible[1] := true;
    except
      eaImport.Disconnect;
      eaImport.Quit;
      eaImport.Free;
      showmess('无法打开文件!');
      Exit;
    end;
    try
      ewbImport := TExcelWorkbook.Create(Application);
      sheet := TExcelWorksheet.Create(Application);
      SBar.panels[0].text:='正在读取数据...';
      ewbImport.ConnectTo(eaImport.Workbooks[1]);
      sheet.ConnectTo(ewbImport.Worksheets[1] as _Worksheet);
      first;
      //用循环取字符串的字段,然后插入到EXCEL表里
      For i:=0 to IRowCount-1 do
      begin
        j:=0;
        tempField:=FieldStr;
        //取要显示的字符串
        while tempField<>'' do
        begin
          tempPos:=pos(',',tempField);
          PreField:=copy(tempField,1,TempPos-1);
          sheet.cells.Item[StartRow+i,StartCol+j]:=FieldByname(PreField).asstring;
          tempField:=copy(tempField,tempPos+1,length(tempField));
          inc(j);
        end;//while
        next;
      end;//for
      //*************************************************************************
      //给数据加上边框
      sheet.Range[sheet.Cells.Item[StartRow,StartCol],sheet.Cells.Item[StartRow+IRowCount,StartCol+J-1]].Borders.LineStyle :=1;
      {sheet.Range[sheet.Cells.Item[4,4],sheet.Cells.Item[6,6]].Borders.LineStyle :=2;
      sheet.Range[sheet.Cells.Item[5,2],sheet.Cells.Item[5,2]].Borders[1].LineStyle :=3;
      sheet.Range[sheet.Cells.Item[5,2],sheet.Cells.Item[5,2]].Borders[2].LineStyle :=4;
      sheet.Range[sheet.Cells.Item[5,2],sheet.Cells.Item[5,2]].Borders[3].LineStyle :=5;
      sheet.Range[sheet.Cells.Item[5,2],sheet.Cells.Item[5,2]].Borders[4].LineStyle :=6;
      sheet.Range[sheet.Cells.Item[5,2],sheet.Cells.Item[5,2]].Borders[5].LineStyle :=7;
      sheet.Range[sheet.Cells.Item[5,2],sheet.Cells.Item[5,2]].Borders[6].LineStyle :=8;}
      sheet.Disconnect;
      ewbImport.Disconnect;
      eaImport.Disconnect;
      eaImport.Quit;
      sheet.Free;
      ewbImport.Free;
      eaImport.Free;
      SBar.panels[0].text:='数据读取完毕。';
    except
      eaImport.Disconnect;
      eaImport.Quit;
      eaImport.Free;
      showmess('文件读取错误!');
    end;//try
  end;//with
end;


以上介绍了“ TExcelApplication的问题”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/3048096.html

相关图片

相关文章