ASP.NET关于对excel数据导入到数据库

 //*******************************C#(UI代码)********************************/
Web界面上放一个FileUpload 名称为:FileUpload1, 一个button按钮,名称为button1
程序中应用的两个外部引用为:Ionic.Zip.dll 和 NPOI.dll ; Ionic.Zip.dll可以不引用,单独的NPOI.dll也是可以的
//*********************************C#(UI代码)****************************/
namespace WebApp.BaseData
{
public partial class Test : System.Web.UI.Page
{
public string strPath; /*strPath这个变量用来存储上传上来的文件名*/
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.FileName != "") /*1判断用户是否添加了文件*/
{
strPath = FileUpload1.FileName; /*1获取文件名*/
string MapPath = Server.MapPath("../bin/"); /*1更改路径*/
FileUpload1.SaveAs(MapPath + FileUpload1.FileName);
#region MyRegion
using (FileStream file = new FileStream(MapPath + strPath, FileMode.Open, FileAccess.Read))//1以文件流的形式打开文件
{
using (Workbook wk = new HSSFWorkbook(file))/*1打开工作薄*/
{
using (Sheet sheet = wk.GetSheetAt())
{
for (int j = ; j <= sheet.LastRowNum; j++)//1循环行数量 j=1表示不要第一列,因为第一列是字段的名字
{
Row row = sheet.GetRow(j);//1获取行
int length = row.LastCellNum; ////////1这条语句,在本程序中,没有作用,可以删除,仅做调试使用
List<string> list = new List<string>();
for (int i = ; i < row.LastCellNum; i++)
{
Cell cell = row.GetCell(i);//1获取单元格里的数据
if (cell != null)//1如果单元格里有数据
{
list.Add(cell.ToString());//则将这个单元格里的数据添加到List集合里;
}
}
ArrayList array = new ArrayList(); //1创建一个Array数组
for (int k = ; k < list.Count; k++)//1循环读取List集合里的一行数据,本程序是读一行,插入一行
{
array.Add(list[k].ToString());//1将List集合里的一行数据,放到Array数组里,方便作为参数传递;
}
BLL.AL.BLL_Test bll = new BLL.AL.BLL_Test();
int num = bll.InsertDate(array);//1调用BLL的Test(参数)方法,将一行数据传过去
}
}
}
}
#endregion
}
else
{
Response.Write("<script>alert('请选择文件')</script>");
}
}
}
} //*****************************************************C#(BLL代码)***************************************************/
DAL.AL.DAL_Test dal = new DAL.AL.DAL_Test();
public int InsertDate(ArrayList array)
{
return dal.InsertDate(array);//将一行数据传给DAL
}
//*****************************************************C#(DAL代码)***************************************************/
public int InsertDate(ArrayList array)
{
string sql = "insert into Test(T_ProdDate,T_Paln,T_Prod) values(@T_ProdDate,@T_Paln,@T_Prod)";//写插入的SQL语句
SqlParameter[] parameter ={ //1参数,这里面写的有点死
new SqlParameter("@T_ProdDate",array[]), //1,我的数据库里只有三列数据
new SqlParameter("@T_Paln",array[]),
new SqlParameter("@T_Prod",array[])
};
return Utility.SqlHelper.ExecuteNonQuery(sql, parameter);
}
//*****************************************************C#(Excel里面的样式,将下面的数据复制到EXCEL表格里面)***************************************************/
时间 计划 实际
-- :
-- :
-- :
-- :
-- :
-- :
-- :
-- :
-- :
-- :
-- :
-- :
-- :
-- :
-- :
-- :
-- :
-- :
-- :
-- :
-- : //*****************************************************C#(SQL Server 数据表结构)***************************************************/
create table Test
(
T_ID INT identity(,) primary key, //1序号
T_ProdDate varchar() not null, //1生产日期
T_Paln varchar() not null, //1计划产量
T_Prod varchar() not null, //1实际产量
T_IsDel CHAR() default '' //1删除标志
)
上一篇:Windows Azure 网站 (WAWS) 中的服务器端包含 (SSI)


下一篇:关于国产手机(含山寨机)的mrp格式文件使用