欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【asp.net导出excel的简单方法实例】,下面是详细的讲解!
asp.net导出excel的简单方法实例
/// <summary>
/// 导入数据
/// </summary>
/// <param name="file"></param>
/// <returns>true表示导入成功</returns>
public bool Impoart(HttpPostedFileBase file)
{
try
{
//保存excel
string path=HttpContext.Current.Server.MapPath("/");
file.SaveAs(path + file.FileName);
//读取
FileStream sw=File.Open(path + file.FileName, FileMode.Open, FileAccess.Read);
IWorkbook workbook=new XSSFWorkbook(sw);
ISheet sheet1=workbook.GetSheet("Sheet1");
//最大行数
int rowsCount=sheet1.PhysicalNumberOfRows;
//判断首行是否符合规范 也就是Excel中的列名
IRow firstRow=sheet1.GetRow(0);
if (
!(firstRow.GetCell(0).ToString()=="名称" && firstRow.GetCell(1).ToString()=="简称" &&
firstRow.GetCell(2).ToString()=="分类" && firstRow.GetCell(3).ToString()=="参考价" &&
firstRow.GetCell(4).ToString()=="商品介绍"))
{
return false;
}
//跳过类型不正确的品项
for (int i=1; i < rowsCount; i++)
{
IRow row=sheet1.GetRow(i);
Shop_Product product=new Shop_Product();
string category=row.GetCell(2) !=null ? row.GetCell(2).ToString() : null;
if (!string.IsNullOrEmpty(category))
{
var cate=
_unitOfWork.Shop_ProductCategoryRepository().GetAll().FirstOrDefault(t=> t.Name==category);
if (cate !=null)
{
product.ProductCategoryName=cate.Name;
product.Shop_ProductCategory_ID=cate.ID;
}
else
{
continue;
}
}
else
{
continue;
}
product.PName=row.GetCell(0) !=null ? row.GetCell(0).ToString() : null;
product.PCName=row.GetCell(1) !=null ? row.GetCell(1).ToString() : null;
if (row.GetCell(3) !=null)
{
product.Price=Double.Parse(row.GetCell(3).ToString());
}
product.Description=row.GetCell(4) !=null ? row.GetCell(4).ToString() : null;
_unitOfWork.Shop_ProductRepository().Insert(product);
}
_unitOfWork.Save();
}
catch
{
return false;
}
return true;
}
关于asp.net导出excel的简单方法实例的用户互动如下:
相关问题:
答: >>详细
相关问题:
答: >>详细
相关问题:
答: >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【asp】asp.net 动态添加多个用户控件-net-动态添
- 【ASP】ASP.NET中内嵌页面代码的一个问题-NET-内
- 【As】Asp.net中的页面乱码的问题-sp--pn-ne-et
- 【增加记录】asp.net中获取新增加记录的ID Access
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【Asp】Asp.net 页面调用javascript变量的值-net-
- 【ASP】ASP.NET 5升级后如何删除旧版本的DNX-NET5
- 【404页面】ASP.NET设置404页面返回302HTTP状态码
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
