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 ...
随机推荐
- hdu2112 HDU Today 基础最短路
这题的关键是把车站的名字转化为点的编号.我用的是map.声明一个map<string,int> st,然后按照字符串出现的次序给st赋值.例如:st[s1]=2;代表这字符串s1出现的次序 ...
- dw2018修改为中文
dw2018 英文版修改为中文, 把zh_CN文件夹内的内容复制到en_US文件夹内并替换, 或者重命名zh_CN文件夹为en_US
- winserver2012安装.net 3.5
运行 dism.exe /online /enable-feature /featurename:NetFX3 /Source:I:\sources\sxs
- 怎么看时序图--nand flash的读操作详解 (转)
这篇文章不是介绍 nand flash的物理结构和关于nand flash的一些基本知识的.你需要至少了解 你手上的 nand flash的物理结构和一些诸如读写命令 操作的大概印象,你至少也需要看过 ...
- Java中数组遍历
就是将数组中的每个元素分别获取出来,就是遍历.遍历也是数组操作中的基石. 数组的索引是 0 到 lenght-1 ,可以作为循环的条件出现 public class ArrayDemo4 { publ ...
- openlayers5学习笔记-添加Overlay
tmp.addPosition = function (map, item) { var ele = document.createElement("div"); var img ...
- Spring AOP 介绍与基于接口的实现
热烈推荐:超多IT资源,尽在798资源网 声明:转载文章,为防止丢失所以做此备份. 本文来自公众号:程序之心 原文地址:https://mp.weixin.qq.com/s/vo94gVyTss0LY ...
- nginx的headers_more模块的使用
nginx的headers_more模块用于 添加.修改或清除 请求/响应头,该模块不是nginx自带的,默认不包含该模块,需要另外安装.幸运的是openresty默认包含了该模块,可以直接使用. 该 ...
- with as递归调用
一.递归调用--在代码中偶尔看到以记之,便于下次学习 https://blog.csdn.net/johnf_nash/article/details/78681060 --查询节点及其下所有子节点 ...
- [luogu2081 NOI2012] 迷失游乐园 (树形期望dp 基环树)
传送门 题目描述 放假了,小Z觉得呆在家里特别无聊,于是决定一个人去游乐园玩. 进入游乐园后,小Z看了看游乐园的地图,发现可以将游乐园抽象成有n个景点.m条道路的无向连通图,且该图中至多有一个环(即m ...