题目如上:
   效果为:班级 姓名 分数一年级 刘备 20
张飞 21
赵云 60
二年级 关羽 50
曹操 12

解决方案 »

  1.   

    这是一段创建excel报表并向里写数据的代码,可能对你有绑定  try
        Self.ExcelApplication1:=TExcelApplication.Create(self);
        Self.ExcelApplication1.Connect;
      except
        messagebox(application.Handle,'无法生成Excel报表,请确定安装了Excel后重试','信息',mb_ok or mb_iconinformation);
        exit;
      end;
      Self.ExcelApplication1.Visible[0]:=false;
      self.ExcelApplication1.DisplayAlerts[0]:=False;
      Self.ExcelApplication1.Caption:='统计报表';
      Self.ExcelApplication1.Workbooks.Add(EmptyParam,0);
      //Self.ExcelApplication1.Workbooks.Add(EmptyParam,1);
      Self.ExcelWorkbook1.ConnectTo(self.ExcelApplication1.Workbooks[1]);
      self.ExcelWorksheet1:=TExcelWorkSheet.Create(self);
      self.ExcelWorksheet1.ConnectTo(self.ExcelWorkbook1.Worksheets[1] as _worksheet);  self.ExcelWorksheet1.Range['A1','A1'].columnwidth:=length('总计(一共有多少条数据)');
      self.ExcelWorksheet1.Range['A1','A1'].WrapText:=true;
      self.ExcelWorksheet1.Cells.Item[2,1].Interior.colorindex:=15;
      self.ExcelWorksheet1.Cells.Item[2,1].BorderAround(xlcontinuous,xlthin,xlautomatic,clblack);
      self.ExcelWorksheet1.Cells.Item[2,1]:='总计(一共有多少条数据)';
      self.ExcelWorksheet1.Cells.Item[3,1].BorderAround(xlcontinuous,xlthin,xlautomatic,clblack);
      self.ExcelWorksheet1.Cells.Item[3,1]:=inttostr(cou1);
      self.ExcelWorksheet1.Cells.Item[2,2].Interior.colorindex:=15;
      self.ExcelWorksheet1.Cells.Item[2,2].BorderAround(xlcontinuous,xlthin,xlautomatic,clblack);
      self.ExcelWorksheet1.Range['B1','B1'].columnwidth:=length('w2选择a的个数');
      self.ExcelWorksheet1.Range['B1','B1'].WrapText:=true;
      self.ExcelWorksheet1.Cells.Item[2,2]:='w2选择a的个数';
      self.ExcelWorksheet1.Cells.Item[3,2].BorderAround(xlcontinuous,xlthin,xlautomatic,clblack);
      self.ExcelWorksheet1.Cells.Item[3,2]:=inttostr(cou2);  self.ExcelWorksheet1.Cells.Item[2,3].Interior.colorindex:=15;
      self.ExcelWorksheet1.Cells.Item[2,3].BorderAround(xlcontinuous,xlthin,xlautomatic,clblack);
      self.ExcelWorksheet1.Range['C1','C1'].columnwidth:=length('W2选择a时w4选择b的个数');
      self.ExcelWorksheet1.Range['C1','C1'].WrapText:=true;
      self.ExcelWorksheet1.Cells.Item[2,3]:='W2选择a时w4选择b的个数';
      self.ExcelWorksheet1.Cells.Item[3,3].BorderAround(xlcontinuous,xlthin,xlautomatic,clblack);
      self.ExcelWorksheet1.Cells.Item[3,3]:=inttostr(cou3);  self.ExcelWorksheet1.Cells.Item[2,4].Interior.colorindex:=15;
      self.ExcelWorksheet1.Cells.Item[2,4].BorderAround(xlcontinuous,xlthin,xlautomatic,clblack);
      self.ExcelWorksheet1.Range['D1','D1'].columnwidth:=length('w4占所有W4不等于空的个数的百分比');
      self.ExcelWorksheet1.Range['D1','D1'].WrapText:=true;
      self.ExcelWorksheet1.Cells.Item[2,4]:='w4占所有W4不等于空的个数的百分比';
      self.ExcelWorksheet1.Cells.Item[3,4].BorderAround(xlcontinuous,xlthin,xlautomatic,clblack);
      self.ExcelWorksheet1.Cells.Item[3,4]:=r;
      self.ExcelWorksheet1.SaveAs('f:\统计报表.xls');
      

  2.   

    合并单元格  self.ExcelWorksheet1.Range['E5','E6'].MergeCells:=true;//列  self.ExcelWorksheet1.Range['E5','F5'].MergeCells:=true;//行
      

  3.   


      for i:=1 to row do
      begin
        if i=row then
          Exit;
        if self.ExcelWorksheet1.Cells.Item[i,1]=self.ExcelWorksheet1.Cells.Item[i+1,1] then
          Self.ExcelWorksheet1.Range['1'+inttostr(i),'1'+inttostr(i+1)].MergeCells:=True;
      end;
      

  4.   

    var
    i,row,column:Integer;//数据行列变量
    query:TDataSet;
    begin
      //连接电子表格
     try
       ExcelApplication1.Connect;
     except
       MessageDlg('是否正确安装了EXCEL?',mterror,[mbOK],0);
       Abort;
     end; ExcelApplication1.Visible[0]:=True;
     ExcelApplication1.Caption:='数据导出';
     ExcelApplication1.Workbooks.Add(null,0);
     ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
     excelworksheet1.connectto(excelworkbook1.worksheets[1] as _worksheet);
     //设置数据第一行表头
     for i:=1 to DBGrid1.FieldCount   do
     begin
       ExcelWorksheet1.Cells.Item[1,i]:=DBGrid1.Fields[i-1].FieldName;
     end;
     //导出数据
     query:=DBGrid1.DataSource.DataSet;
     row:=2;//从第二行开始;
     with query do
     begin
       First;
       while not Eof do
       begin
         column:=1;
         for i:=1 to FieldCount do
         begin
           ExcelWorksheet1.Cells.Item[row,column]:=Fields[i-1].Value;
           column:=column+1;
         end;     Next;
         row:=row+1;
       end;
      end;  for i:=3 to row do
      begin
        if i=row then
          Exit;
        if self.ExcelWorksheet1.Cells.Item[i,2]=self.ExcelWorksheet1.Cells.Item[i+1,2] then
          Self.ExcelWorksheet1.Range['1'+inttostr(i),'1'+inttostr(i+1)].MergeCells:=True;
      end;
     ExcelApplication1.Disconnect;  //断开连接
    end;
    这样会提示出错:请帮我看看
    ---------------------------
    Debugger Exception Notification
    ---------------------------
    Project Project1.exe raised exception class EVariantInvalidOpError with message 'Invalid variant operation'. Process stopped. Use Step or Run to continue.
    ---------------------------
    OK   Help   
    ---------------------------