就是我想问问你情为何物W iF i开 放 平 台这 种要怎样加入啊?

您好,我是河北工程大学电子信息专业的学生,我们现在正做课程设计,题目是人员分组生成工具,我想问问,就是怎么用C#把excel数据导入到数据库啊,希望您帮忙解答,谢谢
您好,我是河北工程大学电子信息专业的学生,我们现在正做课程设计,题目是人员分组生成工具,我想问问,就是怎么用C#把excel数据导入到数据库啊,希望您帮忙解答,谢谢 5
不区分大小写匿名
代码如下:前台:&%@ Page Language="C#" &AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %&&!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&&html xmlns="http://www.w3.org/1999/xhtml" &&head runat="server"&& & &title&Excel导入SQL数据库&/title&&/head&&body style="text-align: center"&& & &form id="form1" runat="server"&& & &div&& & & & &table style="width: 576 border-collapse: text-align: center"&& & & & & & &tr&& & & & & & & & &td colspan="3"&& & & & & & & & & & &Excel导入SQL数据库&/td&& & & & & & &/tr&& & & & & & &tr&& & & & & & & & &td colspan="3" style="text-align: left"&& & & & & & & & & & &asp:FileUpload ID="FileUpload1" runat="server" Width="305px" /&& & & & & & & & & & & && & & & & & & & & & &asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="导入SQL" /&&/td&& & & & & & &/tr&& & & & & & &tr&& & & & & & & & &td colspan="3"&& & & & & & & & & & &asp:GridView ID="GridView1" runat="server" Height="133px" Width="100%"&& & & & & & & & & & &/asp:GridView&& & & & & & & & &/td&& & & & & & &/tr&& & & & && & & & &&& & & & &/table&& &&& & &/div&& & &/form&&/body&&/html&后台:using Susing System.Dusing System.Cusing System.Cusing System.Wusing System.Web.Susing System.Web.UI;using System.Web.UI.WebCusing System.Web.UI.WebControls.WebPusing System.Web.UI.HtmlCusing System.Data.OleDb;using System.Data.SqlC//51a/spxpublic partial class _Default : System.Web.UI.Page{& & string strConn = "Data Source=.;DataBase=zhenqiDB;Uid=Pwd=123456"; //链接SQL数据库& & protected void Page_Load(object sender, EventArgs e)& & {& & & & SqlConnection cn = new SqlConnection(strConn);& & & & cn.Open();& & & & SqlDataAdapter sda = new SqlDataAdapter("select * from Employee", cn);& & & & DataSet ds = new DataSet();& & & & sda.Fill(ds, "Employee");& & & & this.GridView1.DataSource = ds.Tables["Employee"];& & & & this.GridView1.DataKeyNames = new string[] { "EmployeeID" };& & & & this.GridView1.DataBind();& & }& & /// &summary&& & /// 查询EXCEL电子表格添加到DATASET& &/// &/summary&& & /// &param name="filenameurl"&服务器路径&/param&& & /// &param name="table"&表名&/param&& &&& & public DataSet ExecleDs(string filenameurl, string table)& & {& & & & string strConn = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filenameurl + ";Extended Properties='Excel 8.0; HDR=YES; IMEX=1'";& & & & OleDbConnection conn = new OleDbConnection(strConn);& & & & conn.Open();& & & & DataSet ds = new DataSet();& & & & OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$]", conn);& & & & odda.Fill(ds, table);& & & && & }& & protected void Button1_Click(object sender, EventArgs e)& & {& & & & &if (FileUpload1.HasFile == false)//HasFile用来检查FileUpload是否有指定文件& & & &{& & & & & & Response.Write("&script&alert('请您选择Excel文件')&/script& ");& & & & & &//当无文件时,返回& & & }& & & string IsXls=System.IO.Path.GetExtension(FileUpload1.FileName).ToString().ToLower();//System.IO.Path.GetExtension获得文件的扩展名& & & &if (IsXls != ".xls")& & & &{& & & & & &Response.Write("&script&alert('只可以选择Excel文件')&/script&");& & & & & &//当选择的不是Excel文件时,返回& & & &}& & & & SqlConnection cn = new SqlConnection(strConn);&& & & & cn.Open();& & & & string filename = DateTime.Now.ToString("yyyymmddhhMMss") + FileUpload1.FileN & & & & & & &//获取Execle文件名 &DateTime日期函数& & & & string savePath = Server.MapPath(("~\\upfiles\\") + filename);//Server.MapPath 获得虚拟服务器相对路径& & & & &FileUpload1.SaveAs(savePath); & & & & & & & & & & & &//SaveAs 将上传的文件内容保存在服务器上& & & & &DataSet ds = ExecleDs(savePath, filename); & & & & & //调用自定义方法& & & & DataRow[] dr = ds.Tables[0].Select(); & & & & & &//定义一个DataRow数组& & & &int rowsnum = ds.Tables[0].Rows.C& & & & if (rowsnum == 0) & & & & & & & & & & & & & & & &&& & & & {& & & & & & Response.Write("&script&alert('Excel表为空表,无数据!')&/script&"); & //当Excel表为空时,对用户进行提示& & & & }& & & & else& & & &{& & & & & for (int i = 0; i & dr.L i++)& & & & & &{& & & & & & & &Guid EmployeeID = Guid.NewGuid();& & & & & & & &string UserName = dr[i]["UserName"].ToString();& & & & & & & &string Pwd = dr[i]["Pwd"].ToString();& & & & & & & &string Name = dr[i]["Name"].ToString();& & & & & & & &int Sex = int.Parse(dr[i]["Sex"].ToString());& & & & & & & &string Tel = dr[i]["Tel"].ToString();& & & & & & & &string Phone = dr[i]["Phone"].ToString();& & & & & & & &string Email = dr[i]["Email"].ToString();& & & & & & & &int QQ = int.Parse(dr[i]["QQ"].ToString());& & & & & & & &string sqlcheck = "select count(*) from Employee where UserName='" + UserName + "'And Name='" + Name + "'"; &//检查用户是否存在& & & & & & & &SqlCommand sqlcmd = new SqlCommand(sqlcheck,cn);& & & & & & & &int count = Convert.ToInt32(sqlcmd.ExecuteScalar());& & & & & & & &if (count & 1)& & & & & & & &{& & & & & & & & & &string insertstr = "insert into Employee (EmployeeID,UserName,Pwd,Name,Sex,Tel,Phone,Email,QQ) values('"+EmployeeID+"','" + UserName + "','" + Pwd + "','" + Name + "','" + Sex + "','" + Tel + "','" + Phone + "','" + Email + "','" + QQ + "')";& & & & & & & & & &SqlCommand cmd = new SqlCommand(insertstr, cn);& & & & & & & & & &try& & & & & & & & & &{& & & & & & & & & & & &cmd.ExecuteNonQuery();& & & & & & & & & &}& & & & & & & & & &catch (MembershipCreateUserException ex) & & & //捕捉异常& & & & & & & & & &{& & & & & & & & & & & &Response.Write("&script&alert('导入内容:" + ex.Message + "')&/script&");& & & & & & & & & &}& & & & & & & &}& & & & & & & &else& & & & & & & &{& & & & & & & & & &Response.Write("&script&alert('内容重复!禁止导入');location='default.aspx'&/script&&/script& ");& & & & & & & & & && & & & & & & &}& & & & & &}& & & & & &Response.Write("&script&alert('Excle表导入成功!');location='default.aspx'&/script&");& & & }& & & cn.Close();& }& & }还有个xmL文件&?xml version="1.0" encoding="UTF-8"?&&?xml-stylesheet type='text/xsl' href='_UpgradeReport_Files/UpgradeReport.xslt'?&&UpgradeLog&&Properties&&Property Name="Solution" Value="Excel导入"&&/Property&&Property Name="解决方案文件" Value="C:\Documents and Settings\Administrator\桌面\DAORU\Excel导入.sln"&&/Property&&Property Name="Date" Value="日"&&/Property&&Property Name="Time" Value="16:13"&&/Property&&/Properties&&Event ErrorLevel="0" Project="" Source="Excel导入.sln" Description="文件已成功备份为 C:\Documents and Settings\Administrator\桌面\DAORU\Backup\Excel导入.sln"&&/Event&&Event ErrorLevel="1" Project="DAORU" Source="..\DAORU" Description="警告: 正将项目文件备份到与原始解决方案相对路径不同的相对路径中。文件夹层次结构上的差异可能会导致在打开或生成备份的解决方案和项目时出现问题。"&&/Event&&Event ErrorLevel="0" Project="" Source="Excel导入.sln" Description="成功转换解决方案"&&/Event&&Event ErrorLevel="3" Project="" Source="Excel导入.sln" Description="Converted"&&/Event&&/UpgradeLog&还要在解决方案里建一个upfiles的文件夹如有不懂 可以追问
能不能加您QQ号啊,希望能及时得到您的回复
,行吗,谢了
大哥,需要验证啊
相关知识等待您来回答
编程领域专家亣姍的喜欢 | LOFTER(乐乎)
LOFTER for ipad —— 记录生活,发现同好
亣姍 的喜欢
{list photos as photo}
{if photo_index==0}{break}{/if}
品牌${make||'-'}
型号${model||'-'}
焦距${focalLength||'-'}
光圈${apertureValue||'-'}
快门速度${exposureTime||'-'}
ISO${isoSpeedRatings||'-'}
曝光补偿${exposureBiasValue||'-'}
镜头${lens||'-'}
{if defined('posts')&&posts.length>0}
{list posts as post}
{if post_index < 3}
{if post.type == 1 || post.type == 5}
{if !!post.title}${post.title|escape}{/if}
{if !!post.digest}${post.digest}{/if}
{if post.type == 2}
{if post.type == 3}
{if post.type == 4}
{if data.msgRank == 1}{/if}
{if data.askSetting == 1}{/if}
{if defined('posts')&&posts.length>0}
{list posts as post}
{if post.type == 2}
{if post.type == 3}
{if post.type == 4}
this.p={ dwrMethod:'queryLikePosts',fpost:'2aba35_c14122',userId:2488404,blogListLength:30};白色夜晚的推荐 | LOFTER(乐乎)
LOFTER for ipad —— 记录生活,发现同好
白色夜晚 的推荐
{list photos as photo}
{if photo_index==0}{break}{/if}
品牌${make||'-'}
型号${model||'-'}
焦距${focalLength||'-'}
光圈${apertureValue||'-'}
快门速度${exposureTime||'-'}
ISO${isoSpeedRatings||'-'}
曝光补偿${exposureBiasValue||'-'}
镜头${lens||'-'}
{if defined('posts')&&posts.length>0}
{list posts as post}
{if post_index < 3}
{if post.type == 1 || post.type == 5}
{if !!post.title}${post.title|escape}{/if}
{if !!post.digest}${post.digest}{/if}
{if post.type == 2}
{if post.type == 3}
{if post.type == 4}
{if data.msgRank == 1}{/if}
{if data.askSetting == 1}{/if}
{if defined('posts')&&posts.length>0}
{list posts as post}
{if post.type == 2}
{if post.type == 3}
{if post.type == 4}
this.p={ dwrMethod:'querySharePosts', fpost:'2dd0ae_ca8943',userId:3212954,blogListLength:25};}

我要回帖

更多关于 多想问问你情为何物 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信