我用StoredProc 返回了数据集,用DBGrid显示,数据源datasource的dataset是此存储过程,现在我想将DBGrid的数据保存到Excel中,这就需要数据集dataset了,但好像dataset不能只将此datasouce设置就行了,还需要设置commandtype、commandtext(存储过程名称等)等,那我不就是重复执行了两遍存储过程吗(显示一次、生成Excel一次)?各位大虾有和建议,告知小弟,不胜感激!

解决方案 »

  1.   

    自己写代码一条一条加入Excel
    例子如下:
     if templistview = nil then
        exit;
      if templistview.Items.Count < 1 then
        exit;
      tempGauge.Progress := 0;
      tempGauge.MinValue := 0;
      tempGauge.MaxValue := (templistview.Items.Count - 1) *
        (templistview.Columns.Count - 1) + 2;
      ExcelApp := TExcelApplication.Create(nil);
      if messagedlg('是否可视', mtconfirmation, [mbyes, mbno], 0) = mryes then
        ExcelApp.Visible[0] := true //是否可视
      else
        ExcelApp.Visible[0] := false; //是否可视
      ExcelApp.Connect;
      tempGauge.Progress := tempGauge.Progress + 1;
      LCID := GetUserDefaultLCID();
      wkBook := ExcelApp.WorkBooks.Add(EmptyParam, LCID);
      wkSheet := wkBook.Sheets[1] as _WorkSheet;
      wkSheet.Activate(LCID);
      for j := 0 to templistview.Columns.Count - 1 do
      begin
        //showmessage(templistview.Columns[j].Caption );
        wksheet.Cells.Item[1, j + 1].Value := templistview.Columns[j].Caption;
        {todo: 加上宽度的限定}
      end;   //设置标题颜色
      wksheet.Range[wksheet.Cells.Item[1, 1], wksheet.Cells.Item[1,
        wksheet.Columns.Count]].Font.Color := clBlue;  //改变标题宽度
      for j := 0 to templistview.Columns.Count - 1 do
      begin
        wksheet.Range[wksheet.Cells.Item[1,  j+1],
          wksheet.Cells.Item[wksheet.Rows.Count, j+1]].ColumnWidth :=
          templistview.Column[j].Width div 5;
      end;  tempGauge.Progress := tempGauge.Progress + 1;
      for i := 0 to templistview.Items.Count - 1 do
      begin
        wksheet.Cells.Item[i + 2, 1].Value := templistview.Items[i].Caption;
        for j := 0 to templistview.Columns.Count - 2 do
        begin
          //showmessage(templistview.Items[i].SubItems[j]);
          wksheet.Cells.Item[i + 2, j + 2].Value :=
            templistview.Items[i].SubItems[j];
          tempGauge.Progress := tempGauge.Progress + 1;
        end;
      end;
      

  2.   

    那问题是templistview的数据来源是什么?StoredProc、DataSouce?这是写excel文件的方法,我问的是数据集如何设置可以公用已产生的结果。