Excel导入sqlserver
/// <summary>
/// 上传Excel
/// </summary>
/// <returns></returns>
public ActionResult Excel()
{
ViewData["name"] = "你还没选择文件呢!";
return View();
}
[HttpPost]
public ActionResult Excel(FormCollection collection)
{
// var a = collection["val"];
var file = Request.Files[0];
var b = file.FileName;
Stream ss = file.InputStream;
try
{
if (b != "")
{
//根据路径通过已存在的excel来创建HSSFWorkbook,即整个excel文档
HSSFWorkbook workbook = new HSSFWorkbook(ss);
//获取excel的第一个sheet
HSSFSheet sheet = (HSSFSheet) workbook.GetSheetAt(1);
DataTable table = new DataTable();
//获取sheet的首行
HSSFRow headerRow = (HSSFRow) sheet.GetRow(0);
//一行最后一个方格的编号 即总的列数
int cellCount = headerRow.LastCellNum;
for (int i = headerRow.FirstCellNum; i < cellCount; i++)
{
DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);
table.Columns.Add(column);
}
//最后一列的标号 即总的行数
int rowCount = sheet.LastRowNum;
SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["connstring"].ConnectionString);//连接数据库
//判断数据库里面是否有数据
SqlCommand sqlComm1 = new SqlCommand();
HSSFRow row1 = (HSSFRow) sheet.GetRow(1);
sqlComm1.CommandText = "select * from t_leavenotes where ldate='" + row1.Cells[2] + "'";
sqlComm1.Connection = sqlConn;
sqlConn.Open();
object count = sqlComm1.ExecuteScalar();
sqlConn.Close();
if (count == null)//如果每次才执行下面的操作
{
SqlCommand sqlComm = new SqlCommand();
sqlComm.CommandText =
string.Format(
"insert into t_leavenotes(Lname,Ldate,Partdate,Workdate,Nextdate,startdate,enddate,Become,Rcome,Latedate," +
"befordate,iscome,overtime,worktime,other,logetype,logeto,Department,Usuallydate,weekenddate,holiday,starttime,usuallywork,weekendwork,holidaywork)" +
"values(@Lname,@Ldate,@Partdate,@Workdate,@Nextdate,@startdate,@enddate,@Become,@Rcome,@Latedate," +
"@befordate,@iscome,@overtime,@worktime,@other,@logetype,@logeto,@Department,@Usuallydate,@weekenddate,@holiday,@starttime,@usuallywork,@weekendwork,@holidaywork)");
//参数化SQL
sqlConn.Open();
for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
{
HSSFRow row = (HSSFRow) sheet.GetRow(i);
// DataRow dataRow = table.NewRow();
//dataRow[j] = row.GetCell(j).ToString();
sqlComm.Parameters.AddWithValue("@Lname", row.Cells[1].StringCellValue);
sqlComm.Parameters.AddWithValue("@Ldate", row.Cells[2].StringCellValue);
sqlComm.Parameters.AddWithValue("@Partdate", row.Cells[3].StringCellValue);
sqlComm.Parameters.AddWithValue("@Workdate", row.Cells[4].StringCellValue);
sqlComm.Parameters.AddWithValue("@Nextdate", row.Cells[5].StringCellValue);
sqlComm.Parameters.AddWithValue("@startdate", row.Cells[6].StringCellValue);
sqlComm.Parameters.AddWithValue("@enddate", row.Cells[7].StringCellValue);
sqlComm.Parameters.AddWithValue("@Become", row.Cells[8].StringCellValue);
sqlComm.Parameters.AddWithValue("@Rcome", row.Cells[9].StringCellValue);
sqlComm.Parameters.AddWithValue("@Latedate", row.Cells[10].StringCellValue);
sqlComm.Parameters.AddWithValue("@befordate", row.Cells[11].StringCellValue);
sqlComm.Parameters.AddWithValue("@iscome", row.Cells[12].ToString());
sqlComm.Parameters.AddWithValue("@overtime", row.Cells[13].StringCellValue);
sqlComm.Parameters.AddWithValue("@worktime", row.Cells[14].StringCellValue);
sqlComm.Parameters.AddWithValue("@other", row.Cells[15].StringCellValue);
sqlComm.Parameters.AddWithValue("@logetype", row.Cells[16].StringCellValue);
sqlComm.Parameters.AddWithValue("@logeto", row.Cells[17].StringCellValue);
sqlComm.Parameters.AddWithValue("@Department", row.Cells[18].StringCellValue);
sqlComm.Parameters.AddWithValue("@Usuallydate", row.Cells[19].StringCellValue);
sqlComm.Parameters.AddWithValue("@weekenddate", row.Cells[20].StringCellValue);
sqlComm.Parameters.AddWithValue("@holiday", row.Cells[21].StringCellValue);
sqlComm.Parameters.AddWithValue("@starttime", row.Cells[22].StringCellValue);
sqlComm.Parameters.AddWithValue("@usuallywork", row.Cells[23].StringCellValue);
sqlComm.Parameters.AddWithValue("@weekendwork", row.Cells[24].StringCellValue);
sqlComm.Parameters.AddWithValue("@holidaywork", row.Cells[25].StringCellValue);
sqlComm.CommandType = CommandType.Text;
sqlComm.Connection = sqlConn;
sqlComm.ExecuteNonQuery();
sqlComm.Parameters.Clear();
}
sqlConn.Close();
ViewData["name"] = "添加成功!";
}
}
else
{
ViewData["name"] = "请先选择文件再确定!";
}
}
catch (Exception ex)
{
ViewData["name"] = "你选择的文件有误!";
}
return View();
}
转载于:https://www.cnblogs.com/monkey-long/p/3431360.html
Excel导入sqlserver相关推荐
- powershell excel 导入 sqlserver
powershell excel 导入 sqlserver 更新 多表导入,这之前基础上稍加改动. cls$SqlConnection = New-Object System.Data.SqlClie ...
- Excel导入SqlServer数据库(一)
业务中经常会遇到批量导入数据库的操作,今天总结一下方法. 本文利用三层的架构的思想.将整个业务分成三部分,分别是界面,业务和数据处理. 先介绍第一种方法: 即先将Excel表格的内容转化到DataTa ...
- excel 导入 sqlserver 字符串被截取为255长度解决方案
原文出处: http://blog.csdn.net/xiaoma0529/article/details/21336535 excel表格导入sqlserver数据表中 内容被截取为255长度的字符 ...
- excel导入sqlserver数据库大数据量,可每秒控制数量
数据库代码 USE [Test] GO /****** Object: Table [dbo].[Table_1] Script Date: 11/07/2017 17:27:29 ***** ...
- 对未安装office,不支持excel各版本的数据库进行excel导入sqlserver
现在很多运营商越来越少自己部署服务器了,很多时候都是租用别的服务商的服务器,但这样就有很多麻烦事,比如数据库服务器没安装office,无法直接导入excel的内容 现在有一个稍微复杂点的办法来解决这个 ...
- 大数据量高效导入数据库(以excel导入sqlserver为例)
本人文章陆续转向本人微信公账号发布 公众号:搬砖码农SmallNNN,期望您要是搬砖码农,一起学习探讨,祝您阅读愉快. 最近正在做一个项目,要把excel中的数据导入到sqlserver数据库中,首先 ...
- 把excel导入sqlserver外部表不是预期格式
把导入的文件是从xlsx改成xls类型的,注意不要直接将文件重命名,而是进去表格里面然后将其另存为xls文件!!!
- c#如何实现excel导入到sqlserver,如何实现从sqlserver导出到excel中(详细)
对于从sqlserver中导入.导出excel,虽然sqlserver已经给了较为简单的方式,通过交互式的对话框形式实现,但是有时这种方式存在的很多问题,比方说导入.导出数据不全.而且,对于一个项目而 ...
- sqlserver excel导入数据时有null,为空值
sqlserver excel导入数据时有null,最完美解决办法 1.说明为什么会出现导入的数据会为null? 因为在数据库导入数据时,他会自动检测数据的类型,文字一般检测为nvarchar类型,而 ...
最新文章
- 截断句子取前k个单词
- 模块隐藏(LDR_MODULE链 与 PE特征)
- RealARM210 V3.2硬件版本 nand flash地址分配明细
- Fibonacci Knapsack
- iOS开发 适配iOS10以及Xcode8-b
- [转]如何让虚拟目录里面的webconfig不继承网站
- 用gallery展示图片,实现中间图片稍大,两边较小的效果
- C/C++ 异常( std::exception)
- Hadoop安装教程 Linux版
- Visio| Visio简易使用指南
- NOIP2013TGDay1
- python编程出现:expected an indented block错误。
- 先电OpenStack创建云主机报错500
- 2016域服务器自助修改密码,自助密码重置工具
- QT应用之透明背景_嵌入式freambuffer_QWS背景淡绿色问题
- python flask自定义404错误页面
- 生物AI插图免费领取
- c# 实现两个窗体来回跳转
- cctype头文件 cstring头文件
- 弘辽科技:如何快速引爆新品的流量。