熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

使用Spread制作報表的代碼實例

2013-11-15 12:52:41  來源: ASP編程 

  一.前台代碼

  <tr>
            <td>
               <br />
                <asp:Button ID=btnSave runat=server CssClass=standardtext Text=保存為EXCEL OnClick=btnSave_Click Visible=false />
                <asp:Button ID=btnJSGZ runat=SERVER CssClass=standardtext Text=計算規則 OnClick=btnJSGZ_Click/>
            <br />
                    <FarPoint:FpSpread ID=FpSpread runat=server BorderColor=Black BorderStyle=Solid
                    BorderWidth=px Height= Style=position: absolute; Width=% Visible=false>
                    <CommandBar BackColor=Control ButtonFaceColor=Control ButtonHighlightColor=ControlLightLight
                        ButtonShadowColor=ControlDark />
                    <Sheets>
                        <FarPoint:SheetView SheetName=Sheet>
                        </FarPoint:SheetView>
                    </Sheets>
                </FarPoint:FpSpread>
            </td>

  </tr>

  二.後台代碼

  /// <summary>
    /// 加載存在的文件
    /// </summary>
    private void LoadFile()
    {
        //thispanelVisible = true;
        try
        {
            thisFpSpreadOpenExcel(ServerMapPath(Table/+filename));
            thisFpSpreadSheets[]AllowPage = false;
            thisFpSpreadSheets[]OperationMode = FarPointWebSpreadOperationModeSingleSelect;
        }
        catch(Exception ex)
        {
            LhToolsShowMessageNew(this exMessage);
        }
    }

  /// <summary>
    /// 綁定服務項目列表
    /// </summary>
    /// <param name=dt></param>
    private void ReportBindDataOffwxmLB(DataTable dt)
    {
        int intR = ;
        int intI;
        int intH = ;
        intH=thisFpSpreadSheets[]GetRowHeight(intR + );//得到有數據一行的高
        try
        {
            //thisFpSpreadSheets[]Cells[ ]Text = ((C_User)ViewState[UserInfo])HospitalNameToString();
            if (dt != null)
            {
                if (ddlfwxmSelectedIndex == ) { thisFpSpreadSheets[]Cells[ ]Text = 全部服務; }
                else { thisFpSpreadSheets[]Cells[ ]Text = ddlfwxmSelectedItemText; }

  foreach (DataRow dr in dtRows)
                {
                    intR++;
                    thisFpSpreadSheets[]AddRows(intR );
                    thisFpSpreadSheets[]SetRowHeight(intR intH);
                    thisFpSpreadSheets[]Cells[intR ]Text = dr[aa]ToString();
                    thisFpSpreadSheets[]Cells[intR ]Text = dr[bb]ToString();
                    thisFpSpreadSheets[]Cells[intR ]Text = dr[cc]ToString();
                    thisFpSpreadSheets[]Cells[intR ]Text = dr[dd]ToString();
                    thisFpSpreadSheets[]Cells[intR ]Text = dr[ff]ToString();
                    thisFpSpreadSheets[]Cells[intR ]Text = dr[jj]ToString();
                    thisFpSpreadSheets[]Cells[intR ]Text = dr[kk]ToString();
                    thisFpSpreadSheets[]Cells[intR ]Text = dr[ss]ToString();
                    thisFpSpreadSheets[]Cells[intR ]Text = dr[yy]ToString();
                    for (intI = ; intI < ; intI++)
                    {thisFpSpreadSheets[]Cells[intR intI]FontSize = ;
                        thisFpSpreadSheets[]Cells[intR intI]BorderBorderStyle = SystemWebUIWebControlsBorderStyleSolid;
                        thisFpSpreadSheets[]Cells[intR intI]BorderBorderColor = SystemDrawingColorBlack;
                        thisFpSpreadSheets[]Cells[intR intI]BorderBorderSize = ;
                        thisFpSpreadSheets[]Cells[intR intI]HorizontalAlign = SystemWebUIWebControlsHorizontalAlignLeft;
                    }
                }
            }

   else
            {
                thisFpSpreadSheets[]Cells[ ]BorderBorderSize = ;
                thisFpSpreadSheets[]Cells[ ]Text = 暫時無服務記錄;
            }
        }
        catch(Exception ex)
        {
            throw new Exception(exMessage);
        }
    }

  /// <summary>
    /// 保存為excel
    /// </summary>
    /// <param name=sender></param>
    /// <param name=e></param>
    protected void btnSave_Click(object sender EventArgs e)

  {
        try
        {
            string fileName = GuidNewGuid()ToString() + xls;
            string path = ServerMapPath(tempExcel) + \ + fileName;
            bool retvalue = FpSpreadSaveExcel(path);//寫臨時文件
            //FpSpreadSheets[]
            if (retvalue)
            {
                //success
                HttpResponse response = HttpContextCurrentResponse;
                responseClear();
                responseWriteFile(path);
                string httpHeader = attachment;filename=bookxls;
                responseAppendHeader(ContentDisposition httpHeader);
                responseFlush();
                SystemIOFileDelete(path);//刪除臨時文件
                responseEnd();
            }
        }
        catch (Exception ex)
        {
            LhToolsShowMessageNew(this exMessage);
        }
    }


From:http://tw.wingwit.com/Article/program/ASP/201311/21806.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.