找回密码
 立即注册

QQ登录

只需一步,快速开始

xxwood

初级会员

35

主题

81

帖子

240

积分

初级会员

积分
240

活字格认证

xxwood
初级会员   /  发表于:2013-4-9 09:03  /   查看:6717  /  回复:9
如题!

9 个回复

倒序浏览
iceman
社区贡献组   /  发表于:2013-4-9 09:55:00
沙发
回复 1楼xxwood的帖子

您好,
请查看该贴 9# 中的代码:http://gcdn.grapecity.com/showtopic-2309.html
回复 使用道具 举报
iceman
社区贡献组   /  发表于:2013-4-9 15:23:00
板凳
回复 1楼xxwood的帖子

代码精简结果:

  1.     public partial class WebForm1 : System.Web.UI.Page
  2.     {
  3.         protected void Page_Load(object sender, EventArgs e)
  4.         {
  5.             if (IsPostBack)
  6.             {
  7.                 return;
  8.             }

  9.             FpSpread1.Sheets[0].GroupBarVisible = true;

  10.             //设置 group 信息

  11.             FarPoint.Web.Spread.SheetView sv = this.FpSpread1.ActiveSheetView;

  12.             sv.AllowGroup = true;

  13.             GroupDataModel gdm = new GroupDataModel(sv.DataModel);

  14.             sv.DataModel = gdm;

  15.             FarPoint.Web.Spread.SortInfo[] sort = new FarPoint.Web.Spread.SortInfo[1];

  16.             //设置分组列和排序方式
  17.             sort[0] = new FarPoint.Web.Spread.SortInfo(1, true);

  18.             gdm.Group(sort);

  19.             FarPoint.Web.Spread.Model.Group group = new Group(gdm, (FarPoint.Web.Spread.Model.Group)gdm.Groups[0], 0, false);

  20.             GroupFooter groupfooter = new GroupFooter(group);

  21.             FpSpread1.Sheets[0].GroupFooterVisible = true;


  22.             //设置 ColumnFooter,GroupFooter 第1列公式为 Sum ,用于计算第5列所有单元格加和.
  23.             this.FpSpread1.ActiveSheetView.Columns[0].AggregationType = FarPoint.Web.Spread.Model.AggregationType.Sum;
  24.         }
  25.     }
复制代码
回复 使用道具 举报
iceman
社区贡献组   /  发表于:2013-4-11 14:25:00
地板
回复 1楼xxwood的帖子

xxwood 你好,

Excel 分组如图:

2.png

Spread 分组如图:

1.png

Excel 无法支持 Spread 中的分组样式。

我们可以通过自定义实现,代码有不完善之处,还需要你继续完善,以下为 Demo :


grouping.zip (27.9 KB, 下载次数: 94)
回复 使用道具 举报
iceman
社区贡献组   /  发表于:2013-4-11 14:34:00
5#
回复 1楼xxwood的帖子

公式的应用可以参考以下代码:

  1.        protected void Page_Load(object sender, EventArgs e)
  2.         {
  3.             if (IsPostBack)
  4.             {
  5.                 return;
  6.             }
  7.             this.FpSpread1.ClientAutoCalculation = true;
  8.             this.FpSpread1.Sheets[0].Columns[3].Formula = "C1/B1";

  9.             FarPoint.Web.Spread.DoubleCellType dblc = new FarPoint.Web.Spread.DoubleCellType();
  10.             dblc.DecimalDigits = 2;
  11.             dblc.FixedPoint = true;
  12.             FpSpread1.ActiveSheetView.Columns[3].CellType = dblc;
  13.         }

  14.         protected void Button1_Click(object sender, EventArgs e)
  15.         {
  16.             this.FpSpread1.SaveExcelToResponse();
  17.         }
复制代码
回复 使用道具 举报
xxwood
初级会员   /  发表于:2013-4-15 17:29:00
6#
自动分组统计,第一次查询的时候表格正常,第二次查询后表格后面就出现一些乱七八糟的数据,什么原因?代码如下:

  1. FpSpread1.DataSource = ds;
  2.             FarPoint.Web.Spread.Model.GroupDataModel gdm = new FarPoint.Web.Spread.Model.GroupDataModel(FpSpread1.ActiveSheetView.DataModel);
  3.             FarPoint.Web.Spread.SortInfo[] st = new FarPoint.Web.Spread.SortInfo[2];
  4.             st[0] = new FarPoint.Web.Spread.SortInfo(0, true);
  5.             st[1] = new FarPoint.Web.Spread.SortInfo(1, true);
  6.             gdm.Group(st);
  7.             FpSpread1.ActiveSheetView.DataModel = gdm;
  8.             FpSpread1.ActiveSheetView.GroupFooterVisible = true;
  9.             FpSpread1.ActiveSheetView.ColumnFooter.RowCount = 1;
  10.             FpSpread1.ActiveSheetView.Columns[3].AggregationType = FarPoint.Web.Spread.Model.AggregationType.Sum;
复制代码
回复 使用道具 举报
iceman
社区贡献组   /  发表于:2013-4-15 18:24:00
7#
回复 6楼xxwood的帖子

请问查询指的是什么操作?能否发个demo到论坛调试?
回复 使用道具 举报
xxwood
初级会员   /  发表于:2013-4-16 07:47:00
8#
还要什么demo,反正代码页很少,我在这发吧。
页面上就3个文本框,一个按钮,一个FpSpread,没别的控件。后台代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.OleDb;
  9. using System.Configuration;

  10. namespace ysjyb
  11. {
  12.     public partial class zdgz : System.Web.UI.Page
  13.     {
  14.         String ConnStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
  15.         protected void Page_Load(object sender, EventArgs e)
  16.         {
  17.             if (!Page.IsPostBack)
  18.             {
  19.                 TextBox_ny1.Text = “201303”;
  20.                 TextBox_ny2.Text = “201203”;
  21.                 TextBox_nlc.Text = "3";
  22.                 fp_bind();
  23.                
  24.             }
  25.         }

  26.         void fp_bind()
  27.         {
  28.             OleDbConnection conn = new OleDbConnection(ConnStr);
  29.             conn.Open();
  30.             OleDbCommand cmd = conn.CreateCommand();
  31.             cmd.CommandText = "select c.km,c.dm,a.* from (select a.jh,a.rynl nl1,b.rynl nl2,a.rynl-b.rynl nlc from (select * from yd_bb_yjdjsjb where ny='"+TextBox_ny2.Text+"') a,(select * from yd_bb_yjdjsjb where ny='"+TextBox_ny1.Text+"') b where a.jh=b.jh) a,ys_daa01 b,dwdzb c where a.jh=b.jh and b.dwdm=c.dwdm  and nlc>"+TextBox_nlc.Text+" order by c.km,c.dm,nlc desc";
  32.             OleDbDataAdapter da = new OleDbDataAdapter();
  33.             Response.Write(cmd.CommandText);
  34.             da.SelectCommand = cmd;
  35.             DataSet ds = new DataSet();
  36.             da.Fill(ds);
  37.             FpSpread1.ActiveSheetView.DataSource = ds;
  38.             ds.Dispose();
  39.             da.Dispose();
  40.             cmd.Dispose();
  41.             conn.Close();
  42.             conn.Dispose();
  43.             FarPoint.Web.Spread.Model.GroupDataModel gdm = new FarPoint.Web.Spread.Model.GroupDataModel(FpSpread1.ActiveSheetView.DataModel);
  44.             FarPoint.Web.Spread.SortInfo[] st = new FarPoint.Web.Spread.SortInfo[2];
  45.             st[0] = new FarPoint.Web.Spread.SortInfo(0, true);
  46.             st[1] = new FarPoint.Web.Spread.SortInfo(1, true);
  47.             gdm.Group(st);
  48.             FpSpread1.ActiveSheetView.DataModel = gdm;
  49.             FpSpread1.ActiveSheetView.GroupFooterVisible = true;
  50.             FpSpread1.ActiveSheetView.ColumnFooter.RowCount = 1;
  51.             FpSpread1.ActiveSheetView.Columns[3].AggregationType = FarPoint.Web.Spread.Model.AggregationType.Sum;
  52.             FpSpread1.ActiveSheetView.Columns[4].AggregationType = FarPoint.Web.Spread.Model.AggregationType.Sum;
  53.             FpSpread1.ActiveSheetView.Columns[5].AggregationType = FarPoint.Web.Spread.Model.AggregationType.Sum;

  54.         }

  55.         protected void Button1_Click(object sender, EventArgs e)
  56.         {
  57.             fp_bind();
  58.         }
  59.     }
  60. }

复制代码
回复 使用道具 举报
xxwood
初级会员   /  发表于:2013-4-16 08:03:00
9#
不但出现乱数据,而且按钮似乎也失效了。
逐句调试了下,发现 FpSpread1.ActiveSheetView.DataModel = gdm; 这一句的问题。只要有这一句,按钮就失效。
是不是在分组完成后,还要做什么类似于close、dispose之类的处理啊?
回复 使用道具 举报
iceman
社区贡献组   /  发表于:2013-4-16 11:16:00
10#
回复 9楼xxwood的帖子

您的看法是正确的,似乎是分组有个累计效果导致的。请在 fp_bind() 方法的开始处添加:

  1.             this.FpSpread1.Sheets[0].Reset();
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 立即注册
返回顶部