【后台代码】
在页面可以看得到从Exelc中导入的数据,并且使用也获得了有数据的行数的例数。
int RowCount = this.FpSpread1.Sheets[0].NonEmptyRowCount;
int ColumnCount = this.FpSpread1.Sheets[0].NonEmptyColumnCount;
下面是页面的后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using FarPoint.Excel;
using SXT.ZHLC.ProjectMnt.BLL;
using FarPoint.Web.Spread;
namespace SXT.ZHLC.ProjectMnt.Module.PeasCategory
{
public partial class Spread : ListPageBase
{
#region Page_Load
protected void Page_Load(object sender, EventArgs e)
{
FpSpread1.HorizontalScrollBarPolicy = FarPoint.Web.Spread.ScrollBarPolicy.Always;
//垂直
FpSpread1.VerticalScrollBarPolicy = FarPoint.Web.Spread.ScrollBarPolicy.Always;
this.FpSpread1.Sheets[0].RowCount = 100;
this.FpSpread1.Sheets[0].ColumnCount = 100;
}
#endregion
/// <summary>
/// 导入Execl
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnUpload_Click(object sender, EventArgs e)
{
string returnMsg = string.Empty;
string savePath = Server.MapPath("~/UploadFiles");
if (!System.IO.Directory.Exists(savePath))
{
System.IO.Directory.CreateDirectory(savePath);
}
if (this.FileUpload1.HasFile)
{
string TimeName = DateTime.Now.ToString("yyyyMMddhhmmssff");
string filename = this.FileUpload1.FileName;
string filetype = filename.Substring(filename.LastIndexOf("."));
string ServerFilePath = savePath + "\\" + filetype.Replace(filetype, "") + TimeName + filetype;
FileUpload1.SaveAs(ServerFilePath);
this.FpSpread1.OpenExcel(ServerFilePath, FarPoint.Excel.ExcelOpenFlags.TruncateEmptyRowsAndColumns);
}
foreach (SheetView sheet in this.FpSpread1.Sheets)
{
sheet.AllowPage = false;
}
this.FpSpread1.Sheets[0].RowCount = 100;
this.FpSpread1.Sheets[0].ColumnCount = 100;
}
/// <summary>
/// 检查用户上传的用户预算编制模版Execl文件模板格式是否正确
/// </summary>
/// <returns></returns>
private bool CheckYearDataField()
{
bool flag = true;
FarPoint.Web.Spread.SheetView reportinfo = this.FpSpread1.Sheets[0];
try
{
string project = reportinfo.Cells[1, 0].Text;
if (reportinfo.Cells[1, 0].Text != " ")
{
return false;
}
if (reportinfo.Cells[1, 1].Text != "项目")
{
return false;
}
if (reportinfo.Cells[1, 2].Text != "所属部门")
{
return false;
}
if (reportinfo.Cells[1, 3].Text != "预算事项编码")
{
return false;
}
if (reportinfo.Cells[1, 4].Text != "预算事项名称")
{
return false;
}
if (reportinfo.Cells[1, 5].Text != "预算科目代码")
{
return false;
}
if (reportinfo.Cells[1, 6].Text != "预算科目名称")
{
return false;
}
if (reportinfo.Cells[1, 7].Text != "年度")
{
return false;
}
if (reportinfo.Cells[1, 8].Text != "1月")
{
return false;
}
if (reportinfo.Cells[1, 9].Text != "2月")
{
return false;
}
if (reportinfo.Cells[1, 10].Text != "3月")
{
return false;
}
if (reportinfo.Cells[1, 11].Text != "4月")
{
return false;
}
if (reportinfo.Cells[1, 12].Text != "5月")
{
return false;
}
if (reportinfo.Cells[1, 13].Text != "6月")
{
return false;
}
if (reportinfo.Cells[1, 14].Text != "7月")
{
return false;
}
if (reportinfo.Cells[1, 15].Text != "8月")
{
return false;
}
if (reportinfo.Cells[1, 16].Text != "9月")
{
return false;
}
if (reportinfo.Cells[1, 17].Text != "10月")
{
return false;
}
if (reportinfo.Cells[1, 18].Text != "11月")
{
return false;
}
if (reportinfo.Cells[1, 19].Text != "12月")
{
return false;
}
}
catch (Exception)
{
return false;
}
return flag;
}
/// <summary>
/// 保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSave_Click(object sender, EventArgs e)
{
// 检测数据字段
if (!CheckYearDataField())
{
string msg = "<script>alert('模板字段与数据库字段不一致!');document.location.href='Spread.aspx'</script>";
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "tip", msg, false);
}
int RowCount = this.FpSpread1.Sheets[0].NonEmptyRowCount;
int ColumnCount = this.FpSpread1.Sheets[0].NonEmptyColumnCount;
FarPoint.Web.Spread.SheetView reportinfo = this.FpSpread1.Sheets[0];
// 循环行
for (int i = 1; i <= RowCount; i++)
{
// 循环列
for (int j = 0; j < ColumnCount; j++)
{
string projecttitle = reportinfo.Cells[1, j+1].Text;
string projectvaule = reportinfo.Cells[i, j+1].Text;
}
}
}
/// <summary>
/// 获取数据源
/// </summary>
/// <returns></returns>
private static DataTable GetDataTable()
{
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("部门");
dt.Columns.Add("姓名");
dt.Columns.Add("参加工作时间");
dt.Columns.Add("学历");
dt.Columns.Add("工资", typeof(int));
dt.Columns.Add("话费补贴", typeof(int));
dt.Columns.Add("伙食补贴", typeof(int));
dt.Columns.Add("其它", typeof(int));
dt.Rows.Add("业务部", "王晨光", "1979-12", "大学专科毕业生", null, 2583, 3894, null);
dt.Rows.Add("业务部", "吴成龙", "1979-12", "大学专科毕业生", null, null, null, null);
dt.Rows.Add("研发部", "李晶莹", "2000-03", "大学本科毕业生", 760, 2583, 3453, null);
dt.Rows.Add("研发部", "胡明辉", "2000-03", "大学本科毕业生", null, null, null, null);
dt.Rows.Add("研发部", "张冬玲", "2005-08", "硕士研究生", 4650, null, 3453, null);
dt.Rows.Add("研发部", "赵启成", "2005-08", "硕士研究生", null, null, null, null);
dt.Rows.Add("管理部", "钱阳春", "1988-01", "博士研究生", 1760, 5800, 3692, 425);
dt.Rows.Add("管理部", "冯景阳", "1988-01", "博士研究生", 1560, null, null, null);
dt.Rows.Add("管理部", "田耕", "2001-07", "硕士研究生", 1563, 2583, 1025, null);
dt.Rows.Add("财务部", "郝时光", "2001-07", "硕士研究生", 2788, null, 1255, null);
dt.Rows.Add("财务部", "王德国", "2008-12", "大学本科毕业生", 2500, 2456, 3562, null);
dt.Rows.Add("财务部", "周丹", "2008-12", "大学本科毕业生", null, 6500, null, 2580);
return dt;
}
/// <summary>
/// 查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnQuery_Click(object sender, EventArgs e)
{
var dt = GetDataTable();
//关闭自动生成列属性
this.FpSpread1.Sheets[0].AutoGenerateColumns = false;
//绑定数据源
this.FpSpread1.DataSource = dt;
//指定特定列字段
this.FpSpread1.Sheets[0].Columns[0].DataField = "姓名";
this.FpSpread1.Sheets[0].Columns[1].DataField = "部门";
this.FpSpread1.Sheets[0].Columns[2].DataField = "参加工作时间";
this.FpSpread1.Sheets[0].Columns[3].DataField = "工资";
this.FpSpread1.Sheets[0].Columns[4].DataField = "学历";
this.FpSpread1.Sheets[0].Columns[5].DataField = "话费补贴";
this.FpSpread1.Sheets[0].Columns[6].DataField = "伙食补贴";
this.FpSpread1.Sheets[0].Columns[7].DataField = "其它";
}
}
} |
|