微弧氧化电源,好声音 那英,极品二少
excel的操作,最常用的就是导出和导入,废话不多说上代码。
本例使用npoi实现的,不喜勿喷哈。。。。
#endregion
return successmsg("adminmembermemberindex");
}
//这个是下载到桌面的方法,没实现自选路径
public static void download(string filename)
{
fileinfo fileinfo = new fileinfo(httpcontext.current.server.mappath(filename));
//以字符流的形式下载文件
filestream fs = new filestream(httpcontext.current.server.mappath(filename), filemode.open);
byte[] bytes = new byte[(int)fs.length];
fs.read(bytes, 0, bytes.length);
fs.close();
httpcontext.current.response.contenttype = "application/octet-stream";
//通知浏览器下载文件而不是打开
httpcontext.current.response.addheader("content-disposition", "attachment; filename=" + httputility.urlencode(fileinfo.name, system.text.encoding.utf8));
httpcontext.current.response.binarywrite(bytes);
httpcontext.current.response.flush();
httpcontext.current.response.end();
}
上面是导出,下面我介绍下导入。
//读取
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搭建博客,使用BlogEngine.NET+MySql搭建博客
网友评论