.net 导入导出
/// <summary>
/// 导入(上传文件)
/// </summary>
/// <param name="FileNameExcel"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Index(HttpPostedFileBase FileNameExcel)
{
string success = "1";
try
{
string Path = Server.MapPath("/Excel/");
if (!Directory.Exists(Path))
{
Directory.CreateDirectory(Path); //目录不存在则创建
}
var NewFileName = Guid.NewGuid().ToString() + FileNameExcel.FileName;
var NewPath = Path + NewFileName;
FileNameExcel.SaveAs(NewPath);
InsertCopy(NewPath);
}
catch (Exception)
{
success = "0";
}
return Content(success);
} /// <summary>
/// 导入(添加到数据库)
/// </summary>
/// <param name="Path"></param>
void InsertCopy(string Path)
{
Workbook Book = new Workbook(Path); //声明一个操作工作簿的对象
Cells cells = Book.Worksheets[0].Cells;//获取所有的单元格信息
List<Student> List = new List<Student>();
for (int i = 1; i < cells.Rows.Count; i++)
{
Student Model = new Student();
Model.ID = int.Parse(cells[i, 0].StringValue);
Model.Name = cells[i, 1].StringValue;
Model.Photo = cells[i, 2].StringValue;
Model.Age = int.Parse(cells[i, 3].StringValue);
Model.ClassName = cells[i, 4].StringValue;
Model.Subject = cells[i, 5].StringValue;
Model.Grade = int.Parse(cells[i, 6].StringValue);
List.Add(Model);
}
new StudentBLL().Add(List);//保存到数据库
} /// <summary>
/// 导出
/// </summary>
/// <returns></returns>
public string DaoChu()
{
var List = new StudentBLL().SelAll();//获取所有数据
Workbook Book = new Workbook();//声明一个操作工作簿的对象
Cells Cells = Book.Worksheets[0].Cells;//获取工作簿中所有单元格
Cells[0, 0].PutValue("编号");//设置表头
Cells[0, 1].PutValue("学生姓名"); //设置表头
Cells[0, 2].PutValue("头像"); //设置表头
Cells[0, 3].PutValue("年龄"); //设置表头
Cells[0, 4].PutValue("班级"); //设置表头
Cells[0, 5].PutValue("学科"); //设置表头
Cells[0, 6].PutValue("分数"); //设置表头
//把数据循环添加到Excel表格中
for (int i = 1; i <= List.Count(); i++)
{
Cells[i, 0].PutValue(List[i - 1].ID);// 设置单元格内容
Cells[i, 1].PutValue(List[i - 1].Name); //设置单元格内容
Cells[i, 2].PutValue(List[i - 1].Photo); //设置单元格内容
Cells[i, 3].PutValue(List[i - 1].Age); //设置单元格内容
Cells[i, 4].PutValue(List[i - 1].ClassName); //设置单元格内容
Cells[i, 5].PutValue(List[i - 1].Subject); //设置单元格内容
Cells[i, 6].PutValue(List[i - 1].Grade); //设置单元格内容
}
var newFileName = Guid.NewGuid() + "_.xlsx";//给个Excel名称
string path = Server.MapPath("/Ext/");//声明一个路径
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path); //如果目录不存在则创建
}
var NewPath = path + newFileName;
Book.Save(NewPath); //生成Excel
return "1";
}
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄
/// 导入(上传文件)
/// </summary>
/// <param name="FileNameExcel"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Index(HttpPostedFileBase FileNameExcel)
{
string success = "1";
try
{
string Path = Server.MapPath("/Excel/");
if (!Directory.Exists(Path))
{
Directory.CreateDirectory(Path); //目录不存在则创建
}
var NewFileName = Guid.NewGuid().ToString() + FileNameExcel.FileName;
var NewPath = Path + NewFileName;
FileNameExcel.SaveAs(NewPath);
InsertCopy(NewPath);
}
catch (Exception)
{
success = "0";
}
return Content(success);
} /// <summary>
/// 导入(添加到数据库)
/// </summary>
/// <param name="Path"></param>
void InsertCopy(string Path)
{
Workbook Book = new Workbook(Path); //声明一个操作工作簿的对象
Cells cells = Book.Worksheets[0].Cells;//获取所有的单元格信息
List<Student> List = new List<Student>();
for (int i = 1; i < cells.Rows.Count; i++)
{
Student Model = new Student();
Model.ID = int.Parse(cells[i, 0].StringValue);
Model.Name = cells[i, 1].StringValue;
Model.Photo = cells[i, 2].StringValue;
Model.Age = int.Parse(cells[i, 3].StringValue);
Model.ClassName = cells[i, 4].StringValue;
Model.Subject = cells[i, 5].StringValue;
Model.Grade = int.Parse(cells[i, 6].StringValue);
List.Add(Model);
}
new StudentBLL().Add(List);//保存到数据库
} /// <summary>
/// 导出
/// </summary>
/// <returns></returns>
public string DaoChu()
{
var List = new StudentBLL().SelAll();//获取所有数据
Workbook Book = new Workbook();//声明一个操作工作簿的对象
Cells Cells = Book.Worksheets[0].Cells;//获取工作簿中所有单元格
Cells[0, 0].PutValue("编号");//设置表头
Cells[0, 1].PutValue("学生姓名"); //设置表头
Cells[0, 2].PutValue("头像"); //设置表头
Cells[0, 3].PutValue("年龄"); //设置表头
Cells[0, 4].PutValue("班级"); //设置表头
Cells[0, 5].PutValue("学科"); //设置表头
Cells[0, 6].PutValue("分数"); //设置表头
//把数据循环添加到Excel表格中
for (int i = 1; i <= List.Count(); i++)
{
Cells[i, 0].PutValue(List[i - 1].ID);// 设置单元格内容
Cells[i, 1].PutValue(List[i - 1].Name); //设置单元格内容
Cells[i, 2].PutValue(List[i - 1].Photo); //设置单元格内容
Cells[i, 3].PutValue(List[i - 1].Age); //设置单元格内容
Cells[i, 4].PutValue(List[i - 1].ClassName); //设置单元格内容
Cells[i, 5].PutValue(List[i - 1].Subject); //设置单元格内容
Cells[i, 6].PutValue(List[i - 1].Grade); //设置单元格内容
}
var newFileName = Guid.NewGuid() + "_.xlsx";//给个Excel名称
string path = Server.MapPath("/Ext/");//声明一个路径
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path); //如果目录不存在则创建
}
var NewPath = path + newFileName;
Book.Save(NewPath); //生成Excel
return "1";
}

更多精彩