excel的操作,最常用的就是导出和导入,废话不多说上代码。

本例使用NPOI实现的,不喜勿喷哈。。。。

   /// <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();
foreach (DataColumn column in dts.Columns)
headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);
int rowIndex = ;
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, , 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();
if (
!(firstRow.GetCell().ToString() == "名称" && firstRow.GetCell().ToString() == "简称" &&
firstRow.GetCell().ToString() == "分类" && firstRow.GetCell().ToString() == "参考价" &&
firstRow.GetCell().ToString() == "商品介绍"))
{
return false;
} //跳过类型不正确的品项
for (int i = ; i < rowsCount; i++)
{
IRow row = sheet1.GetRow(i);
Shop_Product product = new Shop_Product(); string category = row.GetCell() != null ? row.GetCell().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() != null ? row.GetCell().ToString() : null;
product.PCName = row.GetCell() != null ? row.GetCell().ToString() : null;
if (row.GetCell() != null)
{
product.Price = Double.Parse(row.GetCell().ToString());
}
product.Description = row.GetCell() != null ? row.GetCell().ToString() : null; _unitOfWork.Shop_ProductRepository().Insert(product);
} _unitOfWork.Save();
}
catch
{
return false;
} return true;
}

导出excel的简单方法的更多相关文章

  1. ASP.net中导出Excel的简单方法介绍

    下面介绍一种ASP.net中导出Excel的简单方法 先上代码:前台代码如下(这是自己项目里面写的一点代码先贴出来吧) <div id="export" runat=&quo ...

  2. 从SQL Server中导入/导出Excel的基本方法(转)

    从sql server中导入/导出 excel 的基本方法 /*=========== 导入/导出 excel 的基本方法 ===========*/ 从excel文档中,导入数据到sql数据库中,很 ...

  3. easyExcel导出excel的简单使用

    easyExcel导出excel的简单使用 Java解析.生成Excel比较有名的框架有Apache poi.jxl.但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定 ...

  4. <转>.php导出excel(多种方法)

    基本上导出的文件分为两种:1:类Excel格式,这个其实不是传统意义上的Excel文件,只是因为Excel的兼容能力强,能够正确打开而已.修改这种文件后再保存,通常会提示你是否要转换成Excel文件. ...

  5. spring mvc项目中导出excel表格简单实现

    查阅了一些资料,才整理出spring mvc 项目导出excel表格的实现,其实很是简单,小计一下,方便以后查阅,也希望帮助有需要的朋友. 1.导入所需要依赖(Jar包).我使用的是maven,所以坐 ...

  6. MVC 导出Excel 的其中一方法(View导出excel)

    场景:mvc下导出excel 思路:使用View导出excel 步骤: 1.导出标签添加事件 $("#export_A").click(function(){ //省略代码.... ...

  7. POI导出excel的简单demo

    目前使用过两种导出excel的方式,一种是如题所示的使用POI的方式进行数据的导出,这种方式一般只有在处理比较多的数据或者说需要导出的excel表格中有图片之类的需要特殊处理的文件的时候使用:还有一种 ...

  8. asp.net中导出excel数据的方法汇总

    1.由dataset生成 代码如下 复制代码 public void CreateExcel(DataSet ds,string typeid,string FileName)    {    Htt ...

  9. C# 导出 Excel 的各种方法总结

    第一种:使用 Microsoft.Office.Interop.Excel.dll 首先需要安装 office 的 excel,然后再找到 Microsoft.Office.Interop.Excel ...

随机推荐

  1. Reward

    Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  2. [App]Xamarin学习资料收集

    在博客园其实有很多朋友都在介绍Xamarin的使用方法,下面是比较活跃的一些: http://www.cnblogs.com/yaozhenfa/

  3. MVC视图中ViewStart/RenderSection/Layout/Partial

    1.母板页_Layout.cshtml 2.部分视图 3.默认Layout引用的使用(_ViewStart.cshtml) Layout = "~/Views/Shared/_Layout. ...

  4. UVa10895 Placing Lampposts

    UVa10895 Placing Lampposts 链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34290 [思路] ...

  5. HTML5 application cache

    Application Cache API (一) 基本应用 http://www.cnblogs.com/blackbird/archive/2012/06/12/2546751.html Appl ...

  6. map/reduce实现数据去重

    import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.co ...

  7. usaco 土地并购 && hdu 玩具装箱

    土地并购: Description 约翰准备扩大他的农场,眼前他正在考虑购买N块长方形的土地.如果约翰单买一块土地,价格就是土地的面积.但他可以选择并购一组土地,并购的价格为这些土地中最大的长乘以最大 ...

  8. 关于ASSERT(断言)的作用

    程序一般分为Debug 版本和Release 版本,Debug 版本用于内部调试,Release 版本发行给用户使用.断言assert 是仅在Debug 版本起作用的宏,它用于检查“不应该”发生的情况 ...

  9. Clean Code读书笔记

    第一章 整洁代码 1.编程要做什么 代码呈现了需求的细节,在某些层面上,这些细节无法被忽略或抽象,必须明确.而将需求明确到机器可以执行的细节程度,就是编程要做的事. 2.项目过程中经常遇到这样的问题: ...

  10. linux-centos挂载新硬盘操作

    类似的文章网上已经有很多,这里是记录重要操作的命令,精简流程 精简后的命令: fdisk -ldf -hfdisk /dev/vdbfdisk -l /dev/vdbmkfs -t ext4 /dev ...