简单的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中,用string-array是一种简单的提取XML资源文件数据的方法。例子如下:把相应的数据放到values文件夹的arr
- 最近,在与同事进行协同编程时,我们开始讨论在C#中初始化新对象的最佳方法。我一直是使用构造函数实现,尽管他倾向于静态工程方法。这引起了关于每
- 简单工厂模式(Simple Factory),说他简单是因为我们可以将此模式比作一个简单的民间作坊,他们只有固定的生产线生产固定的产品。也可
- 前言MongoDB是一款由C++编写的高性能、开源、无模式的常用非关系型数据库产品,是非关系数据库当 * 能最丰富、最像关系数据库的数据库。它
- 目录一、事出有因二、解决方案困境三、柳暗花明,终级解决方案第一种实现方案第二种实现方案第三种实现方案四、引发的思考一、事出有因最近有一个场景
- 前言最近碰到了Mybatis一对多查询的场景,在这里总结对比下常见的两种实现方式。本文以常见的订单表和订单详情表来举例说明;数据库表准备订单
- 一、项目简述功能:用户的邮箱注册、验证码验证以及用户登录。 不需要注册账号,也可以上传满足条件的临时文件,但是只4小时内有效。 文件的管理,
- 一:什么是Hystrix在分布式环境中,许多服务依赖项中的一些将不可避免地失败。Hystrix是一个库,通过添加延迟容差和容错逻辑来帮助您控
- 下面就列出配置eclipse联想功能(代码的提示功能)的步骤:1. 打开Eclipse,然后“window”→“Preferences”2.
- 1. 前言Spring最重要的一个概念当属Bean了,我们写的Controller、Service、Dao凡是加了对应注解交给Spring管
- IntelliJ IDEA2022 springboot 热部署 html#pom.xml<dependency>
- 官方文档:https://central.sonatype.org/publish/publish-maven/#a-complete-ex
- 经典的排序算法有八种,分别为:冒泡排序选择排序插入排序归并排序希尔排序快速排序堆排序基数排序其中冒泡排序、选择排序、插入排序称为三大基本排序
- 常规调用方式:(这个肯定会弹出cmd窗口)Runtime.getRuntime().exec("cmd.exe &nbs
- SpringBoot2.x过后static下的静态资源无法访问package com.example.thymeleaf.commons;i
- excel对于下拉框较多选项的,需要使用隐藏工作簿来解决,使用函数取值来做选项选项较少(一般少于5个):private static Dat
- 本文实例为大家分享了Android实现秒表功能的具体代码,供大家参考,具体内容如下今天为了给师弟们讲安卓,花了10分钟写了一个简易的秒表ap
- DES一共就有4个参数参与运作:明文、密文、密钥、向量。为了初学者容易理解,可以把4个参数的关系写成:密文=明文+密钥+向量;明文=密文-密
- 本文实例讲述了Android TextView显示Html类解析的网页和图片及自定义标签。分享给大家供大家参考,具体如下:Android系统
- 一、抽象类是什么?在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的,如果一个类中没有包含足够的