求一个完整的excel导入sql server的程序,用winform的,有的给个参考参考,谢谢
解决方案 »
- 两个时间相减如何得到差值?
- 谁有自己写的.net生成数据库实体类的代码?或是可下载工具
- 如何格式化 DateTime.Now 为 2011年05月11日
- 每点一下treeview就会加一个节点,这是怎么回事
- 2个系统数据同步问题
- 急!散50求解一个关于查询结果分页的问题!(我的“下一页”不能工作,不晓得错在哪里)望高手们帮帮忙!在线等~~~~
- Literal的用法
- ASP.net逆向工程
- asp中的语句:set objHttp = Server.CreateObject("MSXML2.ServerXMLHTTP") 用vb.net怎么写啊,我直接用报错
- 两个表连接后,绑定到DataGrid,模板列里的显示问题
- asp.net网站怎么进入后台比较好?比如首页有一个按钮点击进入还是怎么进入
- 关于递归实现树结构的问题
我是想用winform的
if (FileUpload1.HasFile == false)
{
Response.Write("<script>alert('请选择文件上传!')</script>");
return;
}
string FileEx = Path.GetExtension(FileUpload1.PostedFile.FileName).ToString().ToLower();
if (FileEx != "xls")
{
Response.Write("<script>alert('只可以选择Excel文件')</script>");
return;
}
SqlConnection Conn = new SqlConnection(Connstr);
Conn.Open();
string filename = DateTime.Now.ToString("yyyymmddhhMMss") + FileUpload1.FileName;
string serverpath = Server.MapPath("upfiles/")+filename;
FileUpload1.PostedFile.SaveAs(serverpath);
DataSet ds = ExcelDS(serverpath, filename);
DataRow[] dr = ds.Tables[0].Select();
if (ds.Tables[0].Rows.Count == 0)
{
Response.Write("<script>alert('Excel表为空表,无数据!')</script>");
}
else
{
for (int i = 0; i <dr.Length;i++)
{
string hhaspx_bh = dr[i]["编号"].ToString();//编号 列名 以下类似
string hhaspx_xm = dr[i]["姓名"].ToString();
string hhaspx_dx = dr[i]["底薪"].ToString();
string hhaspx_kh = dr[i]["考核"].ToString();
string hhaspx_jl = dr[i]["奖励"].ToString();
string hhaspx_jt = dr[i]["津贴"].ToString();
string hhaspx_jb = dr[i]["加班"].ToString();
string hhaspx_zb = dr[i]["值班"].ToString();
string hhaspx_jx = dr[i]["绩效"].ToString();
string hhaspx_hj = dr[i]["合计"].ToString();
string sqlcheck = "select count(*) from hhaspx_gz where hhaspx_rq='" + hhaspx_rq + "'And hhaspx_xm='" + hhaspx_xm + "'";
SqlCommand Cmd = new SqlCommand(sqlcheck,Conn);
int count = Convert.ToInt32(Cmd.ExecuteScalar());
if (count < 1)
{
string insertstr = "insert into hhaspx_gz (hhaspx_rq,hhaspx_bh,hhaspx_xm,hhaspx_dx,hhaspx_kh,hhaspx_jl,hhaspx_jt,hhaspx_jb,hhaspx_zb,hhaspx_jx,hhaspx_hj) values('" + hhaspx_rq + "','" + hhaspx_bh + "','" + hhaspx_xm + "','" + hhaspx_dx + "','" + hhaspx_kh + "','" + hhaspx_jl + "','" + hhaspx_jt + "','" + hhaspx_jb + "','" + hhaspx_zb + "','" + hhaspx_jx + "','" + hhaspx_hj + "')";
SqlCommand Cmd = new SqlCommand();
Cmd.CommandText = insertstr;
Cmd.Connection = Conn;
try
{
Cmd.ExecuteNonQuery(); }
catch (MembershipCreateUserException ex)
{
Response.Write("<script>alert(" + ex.Message.ToString() + ")</script>");
}
finally
{
Conn.Close();
}
}
else
{
Response.Write("<script>alert('导入内容重复!');window.location.href='Default.aspx'</script>");
continue;
}
}
Response.Write("<script>alert('Excle表导入成功!');location='default.aspx'</script>");
} Conn.Close();
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.SqlClient;namespace ExcelToSql
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} //把datatable读入sql数据库
private void btnImport_Click(object sender, EventArgs e)
{
string _strExcelFileName = @"D:\程序\ExcelToSql\区域.xls";
DataTable dtExcel = ExcelToDataTable(_strExcelFileName, "Sheet1");
for (int i = 0; i < dtExcel.Rows.Count; i++)
{
InsertDataToSql(dtExcel.Rows[i][0].ToString(), dtExcel.Rows[i][1].ToString(), int.Parse(dtExcel.Rows[i][2].ToString()));
}
} //把excel读入datatable
public static DataTable ExcelToDataTable(string strExcelFileName, string strSheetName)
{
string strConn = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + strExcelFileName + ";Extended Properties=\"Excel 8.0;HDR= YES\";";
string strExcel = string.Format("select * from [Sheet1$]", strSheetName);
DataSet ds = new DataSet(); using (OleDbConnection conn = new OleDbConnection(strConn))
{
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
adapter.Fill(ds, strSheetName);
conn.Close();
} return ds.Tables[strSheetName];
} //向sql数据库插入数据
public static void InsertDataToSql(string _ShortPara, string _FullPara, int _Order)
{
SqlConnection SqlConn = new SqlConnection();
SqlConn.ConnectionString = @"Provider=Microsoft.JET.OLEDB.4.0;integrated security=true;Data Source=QC1818_V2_Public";
SqlConn.Open(); string strInsert = string.Format("insert into T_Region (RegionShortName,RegionFullName,DisplayOrder) values ({0},{1},{2})",
"@RegionShortName",
"@RegionFullName",
"@DisplayOrder");
SqlCommand SqlComm = new SqlCommand(strInsert, SqlConn);
SqlComm.Parameters.Add("@RegionShortName", SqlDbType.NVarChar,64);
SqlComm.Parameters["@RegionShortNam"].Value = _ShortPara;
SqlComm.Parameters.Add("@RegionFullName", SqlDbType.NVarChar,64);
SqlComm.Parameters["@RegionFullName"].Value = _FullPara;
SqlComm.Parameters.Add("@DisplayOrder",SqlDbType.Int);
SqlComm.Parameters["@DisplayOrder"].Value = _Order;
SqlComm.ExecuteNonQuery();
SqlConn.Close();
}
}
}
有错误;adapter.Fill(ds, strSheetName)-----Microsoft Jet 数据库引擎找不到对象'Sheet1$'。请确定对象是否存在,并正确地写出它的名称和路径。为什么