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. POJ_3616_Milking_Time_(动态规划)

    描述 http://poj.org/problem?id=3616 给奶牛挤奶,共m次可以挤,给出每次开始挤奶的时间st,结束挤奶的时间ed,还有挤奶的量ef,每次挤完奶要休息r时间,问最大挤奶量. ...

  2. Automator 简单使用流程

    iOS开发中常常要用到图片缩放的工作,有些需求流程很奇葩,根本找不到现成的工具去实现. 这时候,你可以去想一想Automator了. 示例:要把文件夹下所有的图片文件都缩小成原来的一半(搞iOS开发的 ...

  3. eclipse android重新安装遇到各种问题

    1.JAVA_HOME环境变量失效的解决办法 原文网址:http://www.cnblogs.com/yjmyzz/p/3521554.html 晚上把oracle自带的weblogic给卸载了,然后 ...

  4. Makefile第二讲:打印出内容和使用变量

    摘要 `@echo "开始生成最终执行文件,请稍候..."`这一句便是将一条信息输出到终端,为何前边有个`@`符号呢?有了这个符号该命令本身就不会输出到终端(不理解,自己去掉或者加 ...

  5. C# Login方法

    public static bool User_Login(string url, string uname, string password, out string[] userInfo) { st ...

  6. Tomcat启动失败闪退

    最近把电脑系统从win8升到了8.1(之前源于各种原因都没升外带升级失败),用都用了1个月了,突然发现tomcat启动不了,提示找不到什么什么- -,因为平时基本都是从开发工具里运行的服务器,都没有问 ...

  7. Bzoj 4196: [Noi2015]软件包管理器 树链剖分

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 721  Solved: 419[Submit][Statu ...

  8. Bzoj 1036: [ZJOI2008]树的统计Count 树链剖分,LCT

    1036: [ZJOI2008]树的统计Count Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 11102  Solved: 4490[Submit ...

  9. Codeforces 294E Shaass the Great

    树形DP.由于n只有5000,可以直接枚举边. 枚举边,将树分成两个子树,然后从每个子树中选出一个点分别为u,v,那么答案就是: 子树1中任意两点距离总和+子树2中任意两点距离总和+子树1中任意一点到 ...

  10. windows上zend server安装 报The server encountered an internal error or misconfiguration and was unable to complete your request -解决方法 摘自网络

    windows上zend server安装完成后报如下错误:   Internal Server Error The server encountered an internal error or m ...