Spread 制作报表的用法

一.前台代码

 <tr>
            <td>   
               <br />
                <asp:Button ID="btnSave" runat="server" CssClass="standard-text" Text="保存为EXCEL" OnClick="btnSave_Click" Visible="false" />
                <asp:Button ID="btnJSGZ" runat="SERVER" CssClass="standard-text" Text="计算规则" OnClick="btnJSGZ_Click"/>
            <br />                       
                    <FarPoint:FpSpread ID="FpSpread1" runat="server" BorderColor="Black" BorderStyle="Solid"
                    BorderWidth="1px" Height="400" Style="position: absolute;" Width="97%" Visible="false">
                    <CommandBar BackColor="Control" ButtonFaceColor="Control" ButtonHighlightColor="ControlLightLight"
                        ButtonShadowColor="ControlDark" />
                    <Sheets>
                        <FarPoint:SheetView SheetName="Sheet1">
                        </FarPoint:SheetView>
                    </Sheets>
                </FarPoint:FpSpread>
            </td>           
        </tr> 

 

二.后台代码

/// <summary>
    /// 加载存在的文件
    /// </summary>
    private void LoadFile()
    {
        //this.panel1.Visible = true;
        try
        {           
            this.FpSpread1.OpenExcel(Server.MapPath("Table/"+filename));
            this.FpSpread1.Sheets[0].AllowPage = false;
            this.FpSpread1.Sheets[0].OperationMode = FarPoint.Web.Spread.OperationMode.SingleSelect;
        }
        catch(Exception ex)
        {
            LhTools.ShowMessageNew(this, ex.Message);
        }
    }

   /// <summary>
    /// 绑定服务项目列表
    /// </summary>
    /// <param name="dt"></param>
    private void ReportBindDataOffwxmLB(DataTable dt)
    {
        int intR = 2;
        int intI;
        int intH = 0;
        intH=this.FpSpread1.Sheets[0].GetRowHeight(intR + 1);//得到有数据一行的高
        try
        {
            //this.FpSpread1.Sheets[0].Cells[0, 5].Text = ((C_User)ViewState["UserInfo"]).HospitalName.ToString();     
            if (dt != null)
            {
                if (ddlfwxm.SelectedIndex == 0) { this.FpSpread1.Sheets[0].Cells[0, 5].Text = "全部服务"; }
                else { this.FpSpread1.Sheets[0].Cells[0, 5].Text = ddlfwxm.SelectedItem.Text; }
                foreach (DataRow dr in dt.Rows)
                {
                    intR++;
                    this.FpSpread1.Sheets[0].AddRows(intR, 1);
                    this.FpSpread1.Sheets[0].SetRowHeight(intR, intH);
                    this.FpSpread1.Sheets[0].Cells[intR, 0].Text = dr["aa"].ToString();
                    this.FpSpread1.Sheets[0].Cells[intR, 1].Text = dr["bb"].ToString();
                    this.FpSpread1.Sheets[0].Cells[intR, 2].Text = dr["cc"].ToString();
                    this.FpSpread1.Sheets[0].Cells[intR, 3].Text = dr["dd"].ToString();
                    this.FpSpread1.Sheets[0].Cells[intR, 4].Text = dr["ff"].ToString();
                    this.FpSpread1.Sheets[0].Cells[intR, 5].Text = dr["jj"].ToString();
                    this.FpSpread1.Sheets[0].Cells[intR, 6].Text = dr["kk"].ToString();
                    this.FpSpread1.Sheets[0].Cells[intR, 7].Text = dr["ss"].ToString();
                    this.FpSpread1.Sheets[0].Cells[intR, 8].Text = dr["yy"].ToString();
                    for (intI = 0; intI < 9; intI++)
                    {
                        this.FpSpread1.Sheets[0].Cells[intR, intI].Font.Size = 10;
                        this.FpSpread1.Sheets[0].Cells[intR, intI].Border.BorderStyle = System.Web.UI.WebControls.BorderStyle.Solid;
                        this.FpSpread1.Sheets[0].Cells[intR, intI].Border.BorderColor = System.Drawing.Color.Black;
                        this.FpSpread1.Sheets[0].Cells[intR, intI].Border.BorderSize = 2;
                        this.FpSpread1.Sheets[0].Cells[intR, intI].HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Left;
                    }
                }
            }
            else
            {
                this.FpSpread1.Sheets[0].Cells[3, 0, 3, 8].Border.BorderSize = 1;
                this.FpSpread1.Sheets[0].Cells[3, 0, 3, 3].Text = "暂时无服务记录。。。";
            }
        }
        catch(Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

 

/// <summary>
    /// 保存为excel
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string fileName = Guid.NewGuid().ToString() + ".xls";
            string path = Server.MapPath("tempExcel") + "//" + fileName;
            bool retvalue = FpSpread1.SaveExcel(path);//写临时文件
            //FpSpread1.Sheets[0].
            if (retvalue)
            {
                //success
                HttpResponse response = HttpContext.Current.Response;
                response.Clear();
                response.WriteFile(path);
                string httpHeader = "attachment;filename=book1.xls";
                response.AppendHeader("Content-Disposition", httpHeader);
                response.Flush();
                System.IO.File.Delete(path);//删除临时文件
                response.End();
            }
        }
        catch (Exception ex)
        {
            LhTools.ShowMessageNew(this, ex.Message);
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值