Farpoint操作Excel的功能很强大,这里简单记录一下Farpoint的几个用法.
-
打开Excel模板
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
VIEW CODE this.FpSpread1.OpenExcel(Server.MapPath("../Report/" + strReport)); //打开Excel模板
this.FpSpread1.Sheets[0].AllowPage = false; //是否分页显示
this.FpSpread1.Sheets[0].AutoCalculation = true; //是否计算公式
this.FpSpread1.UseClipboard = false; //是否可以使用复制粘贴
this.FpSpread1.Sheets[0].OperationMode = FarPoint.Web.Spread.OperationMode.RowMode; //操作cell的方式(可读写)
this.FpSpread1.CommandBar.Visible = false; //是否显示工具栏
2. 保存Excel模板
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
VIEW CODE if (fpSpreadTemplate.SaveExcel(Server.MapPath("../" + strFileName)) == true)
{}
如果保存的时候报错了.可以考虑给操作Excel的文件夹赋everyone权限.
如果还是不行可以考虑将你安装过的farpoint的一个文件夹(fp_client) 配置到你的项目里,然后在webconfig配置节点
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
VIEW CODE<appSettings>
<add key="fp_client" value="Report/fp_client"/>
</appSettings>
fp_client 文件夹在你安装的farpoint根目录内.
3. 操作Excel
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
VIEW CODE//打开模板
fpSpreadTemplate.OpenExcel(Server.MapPath("../Template/" + TEMPLATE));
fpSpreadTemplate.Sheets[0].AllowPage = false;
fpSpreadTemplate.Sheets[0].AutoCalculation = true;
//循环行和列,遍历格子
for (int iRow = 0; iRow < fpSpreadTemplate.Sheets[0].RowCount; iRow++)
{
//根据列遍历excel
for (int iCol = 0; iCol < fpSpreadTemplate.Sheets[0].ColumnCount; iCol++)
{
//获取对应格子中的文本值
strCurr = fpSpreadTemplate.Sheets[0].Cells[iRow, iCol].Text;
//为格子内容赋值
fpSpreadTemplate.Sheets[0].Cells[iRow, iCol].Text = "ABC";
}
}
4. 前台操作Excel
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
VIEW CODE //使用Farpoint 的FpSpread1_UpdateCommand 事件
protected void FpSpread1_UpdateCommand(object sender, FarPoint.Web.Spread.SpreadCommandEventArgs e)
{
if (fpSpreadTemplate != null)
{
try
{
//这里循环的某一行的所有列
for (int i = 0; i < e.EditValues.Count; i++)
{
//如果行的第一列是auto说明这个报表需要输入时间自动赋值的
if (this.FpSpread1.Sheets[0].Cells[Convert.ToInt32(e.CommandArgument), 0].Text.ToString() == "auto")
{
#region
//如果当前没有编辑过值
if (e.EditValues[i].ToString() != "System.Object")
{
}
#endregion
}
}
}
catch (Exception ex)
{
}
}
}