using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.Runtime.InteropServices;
namespace 考试管理系统
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private TcpListener tl;
private NetworkStream ns;
string importfilename;
string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" + System.Environment.CurrentDirectory + "\\serverdb.mdb\"";
string s1 = Environment.CurrentDirectory;
private void tabPage1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog f1 = new OpenFileDialog();
f1.InitialDirectory = "C:\\";
f1.Filter = "Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx";
if (f1.ShowDialog() == DialogResult.OK)
{
importfilename = f1.FileName;
label1.Text = f1.FileName;
button2.Visible = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
//导入excel
DataSet OleDsExcle = new DataSet();
try
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + importfilename + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
OleDbConnection OleConn = new OleDbConnection(strConn);
OleConn.Open();
String sql = "SELECT * FROM [Sheet1$]";//可是更改Sheet名称,比如sheet2,等等
OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
OleDaExcel.Fill(OleDsExcle, "Sheet1");
dataGridView2.DataSource = OleDsExcle.Tables[0];
OleConn.Close();
}
catch (Exception ex)
{
MessageBox.Show("名单导入失败,请检查EXCEL文件格式!");
}
//写入数据库
try
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = connstr;
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
for (int i = 0; i < OleDsExcle.Tables[0].Rows.Count; i++)
{
OleDbDataAdapter da = new OleDbDataAdapter("select * from main_Test where sno='" + OleDsExcle.Tables[0].Rows[i][0].ToString() + "' and sname='" + OleDsExcle.Tables[0].Rows[i][1].ToString() + "'", con);
DataSet dgv = new DataSet();
da.Fill(dgv);
int gets = dgv.Tables[0].Rows.Count;
//MessageBox.Show(gets.ToString());
if (gets == 0)
{
cmd.CommandText = "insert into main_Test(sno,sname) values('" + OleDsExcle.Tables[0].Rows[i][0].ToString() + "','" + OleDsExcle.Tables[0].Rows[i][1].ToString() + "')";
cmd.ExecuteNonQuery();
}
}
MessageBox.Show("导入成功!");
}
catch (Exception ex)
{
MessageBox.Show("导入数据库失败,请检查access文件");
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.Runtime.InteropServices;
namespace 考试管理系统
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private TcpListener tl;
private NetworkStream ns;
string importfilename;
string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" + System.Environment.CurrentDirectory + "\\serverdb.mdb\"";
string s1 = Environment.CurrentDirectory;
private void tabPage1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog f1 = new OpenFileDialog();
f1.InitialDirectory = "C:\\";
f1.Filter = "Excel文件(*.xls;*.xlsx)|*.xls;*.xlsx";
if (f1.ShowDialog() == DialogResult.OK)
{
importfilename = f1.FileName;
label1.Text = f1.FileName;
button2.Visible = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
//导入excel
DataSet OleDsExcle = new DataSet();
try
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + importfilename + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
OleDbConnection OleConn = new OleDbConnection(strConn);
OleConn.Open();
String sql = "SELECT * FROM [Sheet1$]";//可是更改Sheet名称,比如sheet2,等等
OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
OleDaExcel.Fill(OleDsExcle, "Sheet1");
dataGridView2.DataSource = OleDsExcle.Tables[0];
OleConn.Close();
}
catch (Exception ex)
{
MessageBox.Show("名单导入失败,请检查EXCEL文件格式!");
}
//写入数据库
try
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = connstr;
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
for (int i = 0; i < OleDsExcle.Tables[0].Rows.Count; i++)
{
OleDbDataAdapter da = new OleDbDataAdapter("select * from main_Test where sno='" + OleDsExcle.Tables[0].Rows[i][0].ToString() + "' and sname='" + OleDsExcle.Tables[0].Rows[i][1].ToString() + "'", con);
DataSet dgv = new DataSet();
da.Fill(dgv);
int gets = dgv.Tables[0].Rows.Count;
//MessageBox.Show(gets.ToString());
if (gets == 0)
{
cmd.CommandText = "insert into main_Test(sno,sname) values('" + OleDsExcle.Tables[0].Rows[i][0].ToString() + "','" + OleDsExcle.Tables[0].Rows[i][1].ToString() + "')";
cmd.ExecuteNonQuery();
}
}
MessageBox.Show("导入成功!");
}
catch (Exception ex)
{
MessageBox.Show("导入数据库失败,请检查access文件");
}
}