导出excel的简单方法
excel的操作,最常用的就是导出和导入,废话不多说上代码。
本例使用NPOI实现的,不喜勿喷哈。。。。
1 /// <summary> 2 /// 导出Excel 3 /// </summary> 4 /// <param name="stime"></param> 5 /// <param name="etime"></param> 6 /// <returns></returns> 7 public ActionResult Export(FormCollection frm) 8 { 9 DataTable dts = new DataTable(); 10 dts = _shopMemeber.ExportMemberData(frm); 11 IWorkbook workbook = new XSSFWorkbook(); 12 ISheet sheet = workbook.CreateSheet(); 13 IRow headerRow = sheet.CreateRow(0); 14 foreach (DataColumn column in dts.Columns) 15 headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption); 16 int rowIndex = 1; 17 foreach (DataRow row in dts.Rows) 18 { 19 IRow dataRow = sheet.CreateRow(rowIndex); 20 foreach (DataColumn column in dts.Columns) 21 { 22 dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString()); 23 } 24 rowIndex++; 25 } 26 string filepath = Server.MapPath("/") + @"用户列表.xlsx"; 27 FileStream file = new FileStream(filepath, FileMode.Create); 28 workbook.Write(file); 29 ExcelHelper.DownLoad(@"/用户列表.xlsx"); 30 #region 不启用 31 32 #endregion 33 return SuccessMsg("AdminMemberMemberIndex"); 34 } 35 //这个是下载到桌面的方法,没实现自选路径 36 public static void DownLoad(string FileName) 37 { 38 FileInfo fileInfo = new FileInfo(HttpContext.Current.Server.MapPath(FileName)); 39 //以字符流的形式下载文件 40 FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(FileName), FileMode.Open); 41 byte[] bytes = new byte[(int)fs.Length]; 42 fs.Read(bytes, 0, bytes.Length); 43 fs.Close(); 44 HttpContext.Current.Response.ContentType = "application/octet-stream"; 45 //通知浏览器下载文件而不是打开 46 HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileInfo.Name, System.Text.Encoding.UTF8)); 47 HttpContext.Current.Response.BinaryWrite(bytes); 48 HttpContext.Current.Response.Flush(); 49 HttpContext.Current.Response.End(); 50 }
上面是导出,下面我介绍下导入。
1 /// <summary> 2 /// 导入数据 3 /// </summary> 4 /// <param name="file"></param> 5 /// <returns>true表示导入成功</returns> 6 public bool Impoart(HttpPostedFileBase file) 7 { 8 try 9 { 10 //保存excel 11 string path = HttpContext.Current.Server.MapPath("/"); 12 file.SaveAs(path + file.FileName); 13 14 //读取 15 16 FileStream sw = File.Open(path + file.FileName, FileMode.Open, FileAccess.Read); 17 IWorkbook workbook = new XSSFWorkbook(sw); 18 ISheet sheet1 = workbook.GetSheet("Sheet1"); 19 20 //最大行数 21 int rowsCount = sheet1.PhysicalNumberOfRows; 22 23 //判断首行是否符合规范 也就是Excel中的列名 24 IRow firstRow = sheet1.GetRow(0); 25 if ( 26 !(firstRow.GetCell(0).ToString() == "名称" && firstRow.GetCell(1).ToString() == "简称" && 27 firstRow.GetCell(2).ToString() == "分类" && firstRow.GetCell(3).ToString() == "参考价" && 28 firstRow.GetCell(4).ToString() == "商品介绍")) 29 { 30 return false; 31 } 32 33 34 //跳过类型不正确的品项 35 for (int i = 1; i < rowsCount; i++) 36 { 37 IRow row = sheet1.GetRow(i); 38 Shop_Product product = new Shop_Product(); 39 40 string category = row.GetCell(2) != null ? row.GetCell(2).ToString() : null; 41 if (!string.IsNullOrEmpty(category)) 42 { 43 var cate = 44 _unitOfWork.Shop_ProductCategoryRepository().GetAll().FirstOrDefault(t => t.Name == category); 45 if (cate != null) 46 { 47 product.ProductCategoryName = cate.Name; 48 product.Shop_ProductCategory_ID = cate.ID; 49 } 50 else 51 { 52 continue; 53 } 54 } 55 else 56 { 57 continue; 58 } 59 60 product.PName = row.GetCell(0) != null ? row.GetCell(0).ToString() : null; 61 product.PCName = row.GetCell(1) != null ? row.GetCell(1).ToString() : null; 62 if (row.GetCell(3) != null) 63 { 64 product.Price = Double.Parse(row.GetCell(3).ToString()); 65 } 66 product.Description = row.GetCell(4) != null ? row.GetCell(4).ToString() : null; 67 68 _unitOfWork.Shop_ProductRepository().Insert(product); 69 } 70 71 _unitOfWork.Save(); 72 } 73 catch 74 { 75 return false; 76 } 77 78 return true; 79 }