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 ...
随机推荐
- 算法 之 3n+1问题
卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反复砍下去,最后一定在某一步得到n=1.卡拉兹在1950年的世界数 ...
- hibernate用注解配置实体类的映射
一.注解类 1. @Table 声明了该实体bean映射指定的表(table),目录(catalog)和schema名字 2. @Id 声明了该实体bean的标识属性(对应表中的主键). 3. @Co ...
- (转)shiro权限框架详解06-shiro与web项目整合(下)
http://blog.csdn.net/facekbook/article/details/54962975 shiro和web项目整合,实现类似真实项目的应用 web项目中认证 web项目中授权 ...
- js商城倒计时
js将秒转换为几天几小时几分钟 1 var oldsecond=second = 60,minute=0,hour=0,day=0; minute = parseInt(second/60); //算 ...
- Python笔记11------一个K-means聚类的小例子
#导入scipy库,库中已经有实现的kmeans模块,直接使用, #根据六个人的分数分为学霸或者学渣两类 import numpy as np from scipy.cluster.vq import ...
- Linux操作随笔
1.查看php加载的模块 /usr/local/php/bin/php -m |less 2.查询连接数 netstat -ntu | awk '{print $5}' | cut -d: -f1 | ...
- 最快理解 - IO多路复用:select / poll / epoll 的区别.
目录 第一个解决方案(多线程) 第二个解决方案(select) 第三个解决方案(poll) 最终解决方案(epoll) 客栈遇到的问题 从开始学习编程后,我就想开一个 Hello World 餐厅,由 ...
- redis 对 key 的操作
keys * :查询当前库中所有的 key keys k? :问号是占位符 del key :删除指定的 key exists k1 :判断 k1 是否存在 move k1 2 :(剪切) 将 k1 ...
- nyoj 120 建边构强连通
#include<stdio.h> #include<string.h> #include<queue> using namespace std; #define ...
- 基本SQL查询
当在数据库的表中存入数据后,就可以查询这些已经存入的数据.下面学习基本SQL查询 本节要点: l 如何使用select语句 Select语句的语法 SELECT语句中的运算 使用DISTINCT和U ...