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;
}

最新文章

  1. Linux下安装Oracle11g服务器
  2. 测试table数据 winfrom datagridview 点击标头数字排序的时候table 列类型要为数字类型
  3. 【转】IOS设备旋转的内部处理流程以及一些【优化建议】
  4. AOP 底层技术比较
  5. AlphaGo实现原理
  6. IOS多线程之NSOperation学习总结
  7. 结构体dtuple_t
  8. Swift学习笔记八:枚举
  9. 动态规划(方案还原):SGU 104 Little shop of flowers
  10. MAX2323E - 原理图系列
  11. zoj 1718 poj 2031 Building a Space Station
  12. mfc---添加背景图
  13. tee 命令详解
  14. SpringMVC常见注解
  15. Windows ML,系统内置的机器学习平台初探
  16. synchronize 关键字原理
  17. Vuex学习笔记(-)安装vuex
  18. Python-HTML转义字符
  19. javaweb(2)之Servlet入门
  20. Saiku连接mysql数据库(二)

热门文章

  1. 《C语言程序设计现代方法》第4章 编程题
  2. 奇怪的Lisp和难懂的计算机程序的构造和解释
  3. [ZETCODE]wxWidgets教程四:菜单栏和工具栏
  4. Hadoop--map/reduce实现单词计数
  5. Sicily1099-Packing Passengers-拓展欧几里德算法
  6. windows ntp安装及调试
  7. Junit初体验
  8. 小物件之select单选下拉列表
  9. WindowsService的调试方法
  10. JSON和JAVA的POJO的相互转换【转载】