一.前台代码
<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);
}
}