asp.net 中excel 导入数据库
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["LYConnectionString"].ConnectionString); ;//链接数据库
conn.Open();
try
{
string fileurl = typename(FileUpload1);//调用typename方法取得excel文件路径
DataSet ds = new DataSet();//取得数据集
ds = xsldata(fileurl);
int errorcount = ;//记录错误信息条数
int insertcount = ;//记录插入成功条数
int updatecount = ;//记录更新信息条数
for (int i = ; i < ds.Tables[].Rows.Count; i++)
{
string stuid = ds.Tables[].Rows[i][].ToString();
string stuname = ds.Tables[].Rows[i][].ToString();
string stusex = ds.Tables[].Rows[i][].ToString();
string zhuanye = ds.Tables[].Rows[i][].ToString();
string classname = ds.Tables[].Rows[i][].ToString();
Response.Write(stuid);
Response.Write(stuname);
Response.Write(stusex);
Response.Write(zhuanye);
Response.Write(classname); if (stuid != "" && stuname != "" && stusex != "" && zhuanye != "" && classname != "")
{
SqlCommand selectcmd = new SqlCommand("select count(*) from stud ", conn);
int count = Convert.ToInt32(selectcmd.ExecuteScalar());
if (count > )
{
SqlCommand selectcmd2 = new SqlCommand("select count(*) from stud where stuid='" + stuid + "'", conn);
int count2 = Convert.ToInt32(selectcmd2.ExecuteScalar());
if (count2 > )
{
SqlCommand updatecmd = new SqlCommand("update stud set stuname='" + stuname + "',stusex='" + stusex + "',zhuanye='" + zhuanye + "',classname='" + classname + "' where stuid='" + stuid + "'", conn);
updatecmd.ExecuteNonQuery();
updatecount++;
}
else
{
SqlCommand insertcmd = new SqlCommand("insert into stud values('" + stuid + "','" + stuname + "','" + stusex + "','" + zhuanye + "','" + classname + "')", conn);
insertcmd.ExecuteNonQuery();
insertcount++;
}
}
else
{
SqlCommand insertcmd = new SqlCommand("insert into stud values('" + stuid + "','" + stuname + "','" + stusex + "','" + zhuanye + "','" + classname + "')", conn);
insertcmd.ExecuteNonQuery(); //break; }
}
else
{
errorcount++;
}
}
Response.Write("<script language='javascript'>alert('" + insertcount + "条数据导入成功!" + updatecount + "条数据更新成功!" + errorcount + "条数据部分信息为空没有导入!');</script>");
}
//catch (Exception exp)
//{
// Response.Write("<script language='javascript'>alert('导入失败!');</script>");
//}
finally
{
conn.Close();
}
}
//判断上传文件,并保存文件
private String typename(FileUpload fileloads)
{
string fullfilename = fileloads.PostedFile.FileName;
string filename = fullfilename.Substring(fullfilename.LastIndexOf("\\\\") + );
string type = fullfilename.Substring(fullfilename.LastIndexOf(".") + );
string murl = "";
if (type == "xls")
{
fileloads.PostedFile.SaveAs(Server.MapPath("excel") + "\\\\" + filename);
murl = (Server.MapPath("excel") + "\\\\" + filename).ToString();
}
else
{
Response.Write("<script language='javascript'>alert('导入文件格式不对!');</script>"); }
return murl;
}
// 数据库导入数据集dataset
private DataSet xsldata(string filepath)
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;IMEX=1'";
//如果是导入excel2013版本,连接字符串则应该变成
string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties='Excel 12.0;IMEX=1'";
System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strCon);
string strCom = "SELECT * FROM [Sheet1$]";
Conn.Open();
System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, Conn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "[Sheet1$]");
Conn.Close();
return ds;
}
}
asp.net 中excel 导入数据库的更多相关文章
- ASP.NET 将Excel导入数据库
将Excel导入数据库大致流程: Excel数据->DataSet->数据库 需要做的准备:1.FileUpload控件一个,按钮一个,如果需要即时显示那么GridView或DataGr ...
- excel 导入数据库 / SSIS 中 excel data source --64位excel 版本不支持-- solution
当本地安装的excel(2013版) 是64-bit时:出现的以下两种错误 解决: 1. excel 导入数据库 , 如果文件是2007则会出现:“The 'Microsoft.ACE.OLEDB.1 ...
- 将Excel中数据导入数据库(三)
上篇文章将Excel中数据导入数据库时,将从Excel读入的数据均转换成了数据库相应字段的类型,其实这是没有必要的,因为对于数据库各种类型的插入,均可以字符串格式插入.比如表WQ_SWMSAR_A字段 ...
- 将Excel中数据导入数据库(二)
在上篇文章中介绍到将Excel中数据导入到数据库中,但上篇文章例子只出现了nvachar类型,且数据量很小.今天碰到将Excel中数据导入数据库中的Excel有6419行,其中每行均有48个字段,有i ...
- 将Excel中数据导入数据库(一)
在工作中经常要将Excel中数据导入数据库,这里介绍一种方法. 假如Excel中的数据如下: 数据库建表如下: 其中Id为自增字段: Excel中数据导入数据库帮助类如下: using System; ...
- Java实现将Excel导入数据库和从数据库中导出为Excel
实现的功能: 用Java实现从Excel导入数据库,如果存在就更新 将数据库中的数据导出为Excel 1.添加jxl.jar mysql-connector-java.1.7-bin.jar包到项目的 ...
- C# ASP.NET CSV文件导入数据库
原文:C# ASP.NET CSV文件导入数据库 using System; using System.Collections.Generic; using System.Text; using Sy ...
- 2014-08-01 ASP.NET中对SQLite数据库的操作——ADO.NET
今天是在吾索实习的第18天.我主要学习了如何在ASP.NET中对SQLite数据库的操作,其基本操作如下: 添加引用System.Data.SQLite.dll(PS:在网页里面任意找到适合的.NET ...
- 记录-java(jxl) Excel导入数据库
本内容主要包括(文件上传.excel2003数据导入数据库)excel导入数据库功能需要jxl jar包支持 下面是文件上传的前端测试代码 <%@ page language="ja ...
随机推荐
- hdoj 1162 Eddy's picture
并查集+最小生成树 Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- 谷歌下设置滚动条的css样式
.oLi-lists-scroll::-webkit-scrollbar { width:5px; padding:1px; background:url(../images/repeat-bar. ...
- prestashop二次开发 笔记(支付插件)
//主函数 public function __construct() { $this->name = 'CilPay'; //模块名称 $this->display ...
- Hive Metastore 代码简析
1. hive metastore 内部结构 1.1 包结构 从package结构来看,主要的5个package,让我们来看看这几个package的内容 (1) metastorepackage是m ...
- java properties 文件中书写相对路径
工程src下的properties 文件要引用发布到D:\work\apache-tomcat-7.0.52\webapps\项目名称\certs这个地址下的文件,properties 中的文件路径应 ...
- 编写一个Java应用程序,该程序包括3个类:Monkey类、People类和主类 E。要求: (1) Monkey类中有个构造方法:Monkey (String s),并且有个public void speak() 方法,在speak方法中输出“咿咿呀呀......”的信息。 (2)People类是Monkey类的子类,在People类中重写方法speak(),在speak方法 中输出“小样的,不
package homework1; public class Monkey { //构造方法 Monkey(String s) { } //成员方法 public void speak() { Sy ...
- 关于 JavaScript 数据类型判断
在 JavaScript 中,有 undefined.null.number.string.boolean 五种基本数据类型,另外,有一种复杂数据类型 object ,类似于 C# 中值类型.引用类型 ...
- java常见面试题
JAVA相关基础知识 1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分 ...
- 使用openssl库实现des,3des加密
原文地址: 使用openssl库实现des,3des加密 主要是调整了一下格式,以及一些变量的类型,以解决在VC2008下无法编译通过的问题. #include <stdio.h> #in ...
- 网络防火墙实战-基于pfsense(1)
pfSense是一个免费的,开源的FreeBSD作为防火墙和路由器专为使用自定义版本.除了 是一个功能强大的,灵活的防火墙和路由平台,它包括一个长长的清单相关的功能,并允许进一步的扩展包,不添加膨胀和 ...