Excel—— [导入到数据库] or 将数据 [导入到Excel]
将Excel导入到数据库实现如下:
前台代码:
@model IEnumerable<Model.Student>
@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
<script src="~/js/jquery.min.js"></script>
<script>
function ExcInput()
{
location.href = "/Home/ExcInput";
}
</script>
</head>
<body>
<div>
<form action="/Home/Execl" method="post" enctype="multipart/form-data">
<input type="file" name="Exc" />
<input type="submit" value="导入" />
</form> <input type="submit" value="导出" onclick="ExcInput()"/>
<table id="table">
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.id</td>
<td>@item.name</td>
<td>@item.age</td>
<td>@item.sex</td>
</tr>
}
</table>
</div>
</body>
</html>
后台代码:
/// <summary>
/// 初始化页面
/// </summary>
/// <returns></returns>
public ActionResult Index()
{
DAL.StudentDal dal = new DAL.StudentDal();
List<Student> ls = dal.GetStudentList();
return View(ls);
}
/// <summary>
/// Excel上传部分
/// 导入 Import
/// </summary>
/// <param name="Exc"></param>
/// <returns></returns>
[HttpPost]
public ActionResult Execl(HttpPostedFileBase Exc)
{
#region /// 上传部分 //如果当前的网站目录为E:\wwwroot 应用程序虚拟目录为E:\wwwroot\company 浏览的页面路径为E:\wwwroot\company\news\show.asp
//在show.asp页面中使用
//Server.MapPath("./") 返回路径为:E:\wwwroot\company\news
//Server.MapPath("/") 返回路径为:E:\wwwroot
//Server.MapPath("../") 返回路径为:E:\wwwroot\company
//Server.MapPath("~/") 返回路径为:E:\wwwroot\company string strfileName = Server.MapPath("/Word/"); //存储文件的地方 if (!Directory.Exists(strfileName)) //判断文件路径是否存在
{
Directory.CreateDirectory(strfileName);
}
string fName = Path.GetFileName(Exc.FileName); //获取文件名
Exc.SaveAs(strfileName + fName); #endregion #region /// Execl导入部分 //execl文件读取
ExcelDAL exc = new ExcelDAL();
DataTable dt = exc.ExcelToDS(strfileName + fName); //把读取的数据导入到数据库
DAL.StudentDal dal = new DAL.StudentDal();
foreach (DataRow dr in dt.Rows)
{
Student student = new Student();
student.id = Convert.ToInt32(dr[]);
student.name = dr[].ToString();
student.sex = dr[].ToString();
student.age = Convert.ToInt32(dr[]);
dal.Add(student);
} #endregion List<Student> ls = dal.GetStudentList();//查询出所有数据 return View("Index", ls);
}
Excel导入导出帮助类 ExcelDAL.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks; using Model;
using System.Data.OleDb; namespace DAL
{
/// <summary>
///
/// </summary>
public class ExcelDAL
{
/// <summary>
/// 将excel中的数据取出,填充到dataset中
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public DataTable ExcelToDS(string path)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn);
OleDbDataAdapter oda = new OleDbDataAdapter("select * from [Sheet1$]", conn);
DataSet ds = new DataSet();
oda.Fill(ds);
return ds.Tables[];
} /// <summary>
/// 将单条数据插入到excel中
/// </summary>
/// <param name="path"></param>
/// <param name="e"></param>
/// <returns></returns>
public int ExcelToAdd(string path, Student student)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string SQL = "INSERT INTO [Sheet2$] ([编号],[姓名],[性别],[年龄]) VALUES(" + student.id + ",'" + student.name + "','" + student.sex + "'," + student.age + ")";
OleDbCommand cmd = new OleDbCommand(SQL, conn);
int i = cmd.ExecuteNonQuery();
conn.Close();
conn.Dispose();
return i;
} /// <summary>
/// 通过循环将list中的数据插入到excel中
/// </summary>
/// <param name="path">excel的路径</param>
/// <param name="studentList">数据集合</param>
/// <returns></returns>
public int ExcelToAdd(string path, List<Student> studentList)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=Excel 8.0;"; OleDbConnection conn = new OleDbConnection(strConn);
conn.Open(); OleDbCommand cmd = new OleDbCommand("CREATE TABLE [Sheet1] ([编号] INT,[姓名] Text,[性别] Text,[年龄] int)", conn);
cmd.ExecuteNonQuery(); foreach (Student e in studentList)
{
string SQL = "INSERT INTO [Sheet1$] ([编号],[姓名],[性别],[年龄]) VALUES(" + e.id + ",'" + e.name + "','" + e.sex + "'," + e.age + ")";
cmd = new OleDbCommand(SQL, conn);
int i = cmd.ExecuteNonQuery();
}
conn.Close();
conn.Dispose();
return ;
}
}
}
将数据库内容导出到Excel实现:
后台代码:
/// <summary>
/// 导出 export
/// </summary>
/// <returns></returns>
public ActionResult ExcInput()
{
#region /// 查询部分 DAL.StudentDal dal = new DAL.StudentDal();
List<Student> ls = dal.GetStudentList(); #endregion ExcelDAL exc = new ExcelDAL(); exc.ExcelToAdd("D:/studentDemo.xls", ls);
return View();
}
前台界面如下:

需要注意的:

Excel—— [导入到数据库] or 将数据 [导入到Excel]的更多相关文章
- phpexcel的写操作将数据库中的数据导入到excel中
这个版本据说是可以支持excel2007,但是我使用2007编辑的xlsx是无法获得该库的支持.于是乎我就将它转化为2003.感觉支持地很好. 下面介绍一下具体的使用: require_once('. ...
- python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图
python制作简单excel统计报表3之将mysql数据库中的数据导入excel模板并生成统计图 # coding=utf-8 from openpyxl import load_workbook ...
- SQL Server 之 在数据库之间进行数据导入导出
1.同一服务器上数据库之间进行数据导入导出 (1).使用 SELECT INTO 导出数据 在SQL Server中使用最广泛的就是通过SELECT INTO语句导出数据,SELECT INTO语句同 ...
- 将一个数据库中表的数据导入另一个数据库(DB2)
将一个数据库中的数据导入另一个数据库(DB2) 我这里举得例子是使用的DB2数据库,其他数据库思路也是这样啦! 1.从db2 数据库中将表中的数据导入本地的excel中 export to d:\my ...
- 如何将数据库中的数据导入到Solr中
要使用solr实现网站中商品搜索,需要将mysql数据库中数据在solr中创建索引. 1.需要在solr的schema.xml文件定义要存储的商品Field. 商品表中的字段为: 配置内容是: < ...
- 使用sqoop将MySQL数据库中的数据导入Hbase
使用sqoop将MySQL数据库中的数据导入Hbase 前提:安装好 sqoop.hbase. 下载jbdc驱动:mysql-connector-java-5.1.10.jar 将 mysql-con ...
- Sqoop(三)将关系型数据库中的数据导入到HDFS(包括hive,hbase中)
一.说明: 将关系型数据库中的数据导入到 HDFS(包括 Hive, HBase) 中,如果导入的是 Hive,那么当 Hive 中没有对应表时,则自动创建. 二.操作 1.创建一张跟mysql中的i ...
- 把数据库中的数据制作成Excel数据
把数据库中的数据制作成Excel数据 如果我们在使用Excel的时候,需要把数据库中的数据制作成Excel数据透视表,我们该怎么操作呢?如果数据在数据库中,我们不用把数据导入到工作表中,我们可以直接以 ...
- 如何将存储在MongoDB数据库中的数据导出到Excel中?
将MongoDB数据库中的数据导出到Excel中,只需以下几个步骤: (1)首先,打开MongoDB安装目录下的bin文件夹,(C:\Program Files (x86)\MongoDB\Serve ...
随机推荐
- Fragment_动态加载
1.新建Fragment的XML布局文件. 2.在activity.xml中添加需要加载Fragment.列如: <?xml version="1.0" encoding=& ...
- DataTable的Select()方法
DataRow[] partno = dtPack.Select("PK_SOHEAD = " + pk_sohead + " AND PART_NO = '" ...
- jq+mui 阻止事件冒泡
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- node linux服务器部署 centos
1下载 wget https://nodejs.org/dist/v6.9.5/node-v6.9.5-linux-x64.tar.xz 2解压 tar xvf node-v6.9.5-linu ...
- 性能问题解决案例01——sybase数据库内存问题
最近湖南现场反馈问题,所有电子签章页面打不开文书(pdf格式),后台日志没报任何错误. 1.首先想到是签章的ocx控件问题,检查ocx控件安装,发现其他电脑也打不开文书,测试页面可以直接打开pdf文档 ...
- Vtk读取并显示保存图像
(1):Vtk读取并显示图像:三种方法 转载:未知出处 转载:用VTK显示平面图片:http://blog.csdn.net/tonylk/article/details/464881 用到vtkJP ...
- (转)基于MVC4+EasyUI的Web开发框架经验总结(5)--使用HTML编辑控件CKEditor和CKFinder
http://www.cnblogs.com/wuhuacong/p/3780356.html Web开发上有很多HTML的编辑控件,如CKEditor.kindeditor等等,很多都做的很好,本文 ...
- WPF 利用RichTextBox 打印出不同颜色的文本
using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows ...
- Python中用绘图库绘制一条蟒蛇
一..构思设计蟒蛇的长度颜色等 首先,我们来构思一个简单的蟒蛇.让它的颜色为黄色,形状为一条正在爬行的蟒蛇. 二..准备绘图库 Python中有一个绘图库叫turtle我们先引入它. import t ...
- 容器化haproxy+keepalived
# 拉取haproxy镜像 docker pull haproxy:1.7.8-alpine mkdir /etc/haproxy cat >/etc/haproxy/haproxy.cfg&l ...