简单的excel导入导出示例分享
发布时间:2023-11-02 00:02:52
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="stime"></param>
/// <param name="etime"></param>
/// <returns></returns>
public ActionResult Export(FormCollection frm)
{
DataTable dts = new DataTable();
dts = _shopMemeber.ExportMemberData(frm);
IWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet();
IRow headerRow = sheet.CreateRow(0);
foreach (DataColumn column in dts.Columns)
headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);
int rowIndex = 1;
foreach (DataRow row in dts.Rows)
{
IRow dataRow = sheet.CreateRow(rowIndex);
foreach (DataColumn column in dts.Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
}
rowIndex++;
}
string filepath = Server.MapPath("/") + @"用户列表.xlsx";
FileStream file = new FileStream(filepath, FileMode.Create);
workbook.Write(file);
ExcelHelper.DownLoad(@"/用户列表.xlsx");
#region 不启用
#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();
}
上面是导出,下面是导入
/// <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;
}


猜你喜欢
- 本文为大家分享了android倒计时控件,供大家参考,具体代码如下/* * Copyright (C) 2012 The * Project
- 1.JavaBean转Map1.1.简介这篇博客是通过反射来进行实现转换的在学习redis中,发现了一个知识点,就是Java对象转map,视
- C#提升管理员权限修改本地Windows系统时间在桌面应用程序开发过程中,需要对C盘下进行文件操作或者系统参数进行设置,例如在没有外网的情况
- 本文实例为大家分享了利用多线程和Socket实现猜拳游戏的具体代码,供大家参考,具体内容如下实例:猜拳游戏猜拳游戏是指小时候玩的石头、剪刀、
- 一、目的本篇文章的目的是记录本人使用flutter加载与调用第三方aar包。二、背景本人go后端,业余时间喜欢玩玩flutter。一直有一个
- C# char类型有自带的大小写转换方法:ToUpper和ToLowerchar str1 = 'a';char str2
- 本文实例为大家分享了C#实现上传下载图片的具体代码,供大家参考,具体内容如下1.首先我们通过流来上传下载图片,所有操作只停留在流这一层Mem
- springboot的最强大的就是那些xxxAutoconfiguration,但是这些xxxAutoConfiguration又依赖那些s
- Java 存储模型和共享对象详解很多程序员对一个共享变量初始化要注意可见性和安全发布(安全地构建一个对象,并其他线程能正确访问)等问题不是很
- Springcloud Config什么是springcloud Config  简单来说,Spring
- SpringMVC中事务是否可以加在Controller层一般而言,事务都是加在Service层的,但是爱钻牛角尖的我时常想:事务加在Con
- 前言在之前的文章中,使用mybatis-plus生成了对应的包,在此基础上,我们针对项目的api接口,添加swagger配置和注解,生成sw
- 前言在项目中一般使用使用volley方式如下,用起来给人一种很乱的感觉,于是一种盘它的想法油然而生。public void get() {S
- HTTP 头处理HTTP 头是 HTTP 请求和响应中的重要组成部分。在创建 HTTP 请求时需要设置一些 HTTP 头。在得到 HTTP
- 最近在做毕业设计,想有一个功能和QQ一样可以裁剪头像并设置圆形头像,额,这是设计狮的一种潮流。而纵观现在主流的APP,只要有用户系统这个功能
- ArrayList中存放引用数据类型ArrayList中存放引用类型时,存放的是一个引用,因此在放入ArrayList之后再进行改动会影响到
- 目录一:spring读取配置或注解的过程二:spring的bean的生命周期2.1:实例化 Instantiation2.2:初始化3: 使
- Java实现驼峰、下划线互转1.使用 Guava 实现先引入相关依赖<dependency> <
- Socket,又称为套接字,Socket是计算机网络通信的基本的技术之一。如今大多数基于网络的软件,如浏览器,即时通讯工具甚至是P2P下载都
- 本文主要给大家介绍的是关于Android实现微信雷达扫描效果的相关内容,分享出来供大家参考学习,下面来看看详细的介绍:废话不多说 先上图(用