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 ...
随机推荐
- TD中INPUT的宽度设置
最近在用C#做Web程序时,碰到一个问题: 将TextBox的asp控件放在Table中的TD里面时,不设置宽度,自动会将TD撑的很大. 查看运行后页面的源代码发现,其实TextBox控件最终在客户端 ...
- js 根据固定位置获取经纬度--腾讯地图
1.首先引入jq 和 腾讯地图js <script src="../js/jQuery.js"></script> <script charset=& ...
- 347. 前K个高频元素
题目描述 给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 示例 1: 输入: nums = [1,1,1,2,2,3], k = 2 输出: [1,2] 示例 2: 输入: nums = ...
- mysql 读写分离 ,mysql_proxy实现
下载安装mysql_proxy: 解压后, 该目录包含已经编译好的二进制文件 . 1: 利用mysql_proxy实现负载均衡 执行mysql_proxy ./mysql-proxy-path/bin ...
- with as递归调用
一.递归调用--在代码中偶尔看到以记之,便于下次学习 https://blog.csdn.net/johnf_nash/article/details/78681060 --查询节点及其下所有子节点 ...
- Python笔记16-------类
1.类的定义 (1)#括号中要加入父类,如果没有则默认为object,万类之源 class 类名(父类): '类的文档字符串' 类体代码 若类什么都不做,则类只作为命名空间,仅作为一个容器. (2)类 ...
- C语言提高 (1) 第一天 数据类型本质与内存四区
(物联网的分层的概念 b/s c/s 结构 习惯: 在C语言 0 函数执行成功 <0是错误 >1做一些返回值处理 3 课前准备 工作经验,记录 4 数据类型的本质 数据类型的本质是固定大小 ...
- ELO kernels 记录
these kernel for discuss how to handle outliers in target values. 一:Ashish Gupta: 在16年6月到18年8月,激活卡的人 ...
- docker-compose,docker-machine,docker swarm 的简单总结
1.docker-compose: 用来在单机上启动一组应用服务.这个服务中可能包含有很多应用,比如你的app服务,redis,mysql,等等. (1)我们需要先创建好docker-compose的 ...
- ROPI下载安装
ROPI下载安装 官方地址 参考文献 安装过程 wget http://num.math.uni-goettingen.de/~m.goerigk/ropi/0.1.0/ropi-0.1.0.tar. ...