导出excel的简单方法
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的简单方法的更多相关文章
- ASP.net中导出Excel的简单方法介绍
下面介绍一种ASP.net中导出Excel的简单方法 先上代码:前台代码如下(这是自己项目里面写的一点代码先贴出来吧) <div id="export" runat=&quo ...
- 从SQL Server中导入/导出Excel的基本方法(转)
从sql server中导入/导出 excel 的基本方法 /*=========== 导入/导出 excel 的基本方法 ===========*/ 从excel文档中,导入数据到sql数据库中,很 ...
- easyExcel导出excel的简单使用
easyExcel导出excel的简单使用 Java解析.生成Excel比较有名的框架有Apache poi.jxl.但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定 ...
- <转>.php导出excel(多种方法)
基本上导出的文件分为两种:1:类Excel格式,这个其实不是传统意义上的Excel文件,只是因为Excel的兼容能力强,能够正确打开而已.修改这种文件后再保存,通常会提示你是否要转换成Excel文件. ...
- spring mvc项目中导出excel表格简单实现
查阅了一些资料,才整理出spring mvc 项目导出excel表格的实现,其实很是简单,小计一下,方便以后查阅,也希望帮助有需要的朋友. 1.导入所需要依赖(Jar包).我使用的是maven,所以坐 ...
- MVC 导出Excel 的其中一方法(View导出excel)
场景:mvc下导出excel 思路:使用View导出excel 步骤: 1.导出标签添加事件 $("#export_A").click(function(){ //省略代码.... ...
- POI导出excel的简单demo
目前使用过两种导出excel的方式,一种是如题所示的使用POI的方式进行数据的导出,这种方式一般只有在处理比较多的数据或者说需要导出的excel表格中有图片之类的需要特殊处理的文件的时候使用:还有一种 ...
- asp.net中导出excel数据的方法汇总
1.由dataset生成 代码如下 复制代码 public void CreateExcel(DataSet ds,string typeid,string FileName) { Htt ...
- C# 导出 Excel 的各种方法总结
第一种:使用 Microsoft.Office.Interop.Excel.dll 首先需要安装 office 的 excel,然后再找到 Microsoft.Office.Interop.Excel ...
随机推荐
- C# 利用BarcodeLib.dll生成条形码
首先效果: 1:首先下载BarcodeLib.dll 下载地址 http://pan.baidu.com/share/link?shareid=2590968386&uk=2148890391 ...
- win8下光驱消失
导入这个注册表后重启,总算能读了..reg add "HKLM\System\CurrentControlSet\Services\atapi\Controller0" /f /v ...
- leecode Binary Tree Level Order Traversal II java
做完这道题,只能说基本功很重要,数组中套数组就不会用了,过几天吧1做了,看自己到底等没. https://oj.leetcode.com/problems/binary-tree-level-orde ...
- Vim常用的快捷键列表
insert: i:insert at now position 在光标之前插入 a:insert append 在光标之后插入 o:下面新建一行插入 s:删除后插入 <<:delete ...
- 4 weekend110的YARN的通用性意义 + yarn的job提交流程
Mr程序写完之后,提交给yarn,yarn会产生一个MRAppMaster,想说的是,yarn变得很 通用,yarn集群上,不光可以跑mr程序,还可以跑各种运算模型. 海量批处理,mapreduce ...
- 完美转换MySQL的字符集 Mysql 数据的导入导出,Mysql 4.1导入到4.0
MySQL从4.1版本开始才提出字符集的概念,所以对于MySQL4.0及其以下的版本,他们的字符集都是Latin1的,所以有时候需要对mysql的字符集进行一下转换,MySQL版本的升级.降级,特别是 ...
- Android基于XMPP Smack Openfire下学习开发IM(六)总结
不管学习什么都应该总结 这里我把关于Xmpp的一些方法整理到一个工具类中了 我就分享给大家 XmppConnection.java package com.techrare.utils; import ...
- Python抓取淘宝IP地址数据
def fetch(ip): url = 'http://ip.taobao.com/service/getIpInfo.php?ip=' + ip result = [] try: response ...
- Quartz定时任务学习(五)触发器
顾名思义,Trigger(触发器)的责任就是触发一个 Job 去执行.当用 Scheduler 注册一个 Job 的时候要创建一个 Trigger 与这个 Job 相关联.Quartz 提供了四种类型 ...
- 李炎恢bootstarp_项目实战__瓢城企业(注释+源码)
源代码下载地址:http://pan.baidu.com/s/1gfI9Pj9 /********************************* pc界面设备页面***************** ...