private OleDbConnection conn = null;
//建立与Excel的连接
private void getConnection()
{
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + this.label8.Text.Trim() + ";Extended Properties=\"Excel 12.0;HDR=YES\"";
if (conn == null)
conn = new OleDbConnection(connectionString);
}
//
private OleDbDataReader dataReader(string sql)
{
this.getConnection();
OleDbDataReader dataReader = null;
OleDbCommand cmd = new OleDbCommand(sql, conn);
try
{
conn.Open();
dataReader = cmd.ExecuteReader();
}
catch (Exception ex) { }
return dataReader;
}
//填充发件箱
private void fillOutBox()
{
string sql = "SELECT * FROM [OutBox$]";
OleDbDataReader dataReader = this.dataReader(sql);
DataTable dt = new DataTable();
dt.Columns.AddRange(
new DataColumn[]{
new DataColumn("mobile",typeof(string)),
new DataColumn("message",typeof(string)),
new DataColumn("time",typeof(string))
});
int i = 0;
while (dataReader.Read())
{
i++;
if (i == 1)
continue;
DataRow dr = dt.NewRow();
dr["mobile"] = Convert.ToString(dataReader.GetValue(1));
dr["message"] = Convert.ToString(dataReader.GetValue(2));
dr["time"] = Convert.ToString(dataReader.GetValue(3));
dt.Rows.Add(dr);
}
this.close();
this.insertFillOutBox(dt);
}
private void insertFillOutBox(DataTable dt)
{
this.sHToutBoxBindingSource.DataMember = "SHToutBox";
this.sHToutBoxBindingSource.DataSource = dt;
this.sHToutBoxDataGridView.DataSource = this.sHToutBoxBindingSource;
//把导入的数据插入数据库
string str_sql = "";
string mobile = "";
string message = "";
string time = "";
int outBoxRowsCount = this.sHToutBoxDataGridView.Rows.Count;
for (int j = 0; j < outBoxRowsCount; j++)
{
mobile = this.sHToutBoxDataGridView.Rows[j].Cells[0].Value.ToString();
message = this.sHToutBoxDataGridView.Rows[j].Cells[1].Value.ToString();
time = this.sHToutBoxDataGridView.Rows[j].Cells[2].Value.ToString();
str_sql = "INSERT INTO SHToutBox(mobile, message, time)VALUES ('" + mobile + "','" + message + "','" + time + "')";
this.tableAdapterManager.SHToutBoxTableAdapter.Adapter.InsertCommand.CommandText = str_sql;
this.tableAdapterManager.SHToutBoxTableAdapter.Adapter.InsertCommand.Connection.Open();
this.tableAdapterManager.SHToutBoxTableAdapter.Adapter.InsertCommand.ExecuteNonQuery();
this.tableAdapterManager.SHToutBoxTableAdapter.Adapter.InsertCommand.Connection.Close();
}
}
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "Excel文件(*.xlsx,*.xls)|*.xlsx;*xls";//设置打开文件的格式
dialog.InitialDirectory = path;//指定初始化路径
if (File.Exists(path + @"\phone.xlsx"))
{
dialog.FileName = "phone.xlsx";
}
DialogResult result = dialog.ShowDialog();
if (result == DialogResult.OK)
{
this.label8.Text = dialog.FileName;
if (this.label8.Text.Trim() != "")
{
try
{
this.fillOutBox();
this.statusBarPanel1.Text = "导入数据成功";
this.statusBarPanel2.Text = "";
}
catch (Exception exc) { }
}
else
{
this.statusBarPanel1.Text = "请选择导出的Excel文件";
}
}
分享到:
相关推荐
C# winform 读取Excel表格中的数据,C# winform 读取Excel表格中的数据
用C#开发的一个读取Excel表格的Demo,无需安装office。
C# 读取Excel数据例子C# 读取Excel数据例子C# 读取Excel数据例子
c#利用EPPlus读取excel实例。 简介:Epplus是一个使用Open Office XML(Xlsx)文件格式,能读写Excel 2007/2010文件的开源组件 功效:支持对excel文档的汇入汇出,图表(excel自带的图表基本都可以实现)的列印
.Net C# 读取Excel表格数据,根据表格数据结构在数据库新建一张对应的数据表既可以上传导入SQlServer数据库,代码有上传文件功能和解析表格数据功能,这里是建数据转成DataTable保存,导入速度对循环导入效率高很多...
这是一个类库,可以对Excel 2007 进行读写操作. 开发环境 Visual Studio 2019. 需要安装ODTwithODAC1120320_32bit客户端组件. 可以在此...
先读取excel数据,然后用chart组件来根据数据绘制曲线图
C#软件源码\读取Excel 2007数据到DataGridView源代码
通过这个工具类可以读取Excel各个版本的Excel文件,同时可以创建 Excel文件获取标题 获取Excel表明
完整挨个读取excel文件数据,并添加进度条progressbar和当前读取条目数据
c#实现从Excel导入数据到textbox中
C#采用ADO.NET读取Excel 0307 数据不完整的问题
VS2005 C# 读取EXCEL图表
C#读取Excel的代码,根据不同的需求做简单的更改即可,希望对您有帮助
C#把excel表格中的数据读入到数组 #把excel表格中的数据读入到数组
使用vs2010开发,读取Excel文件(包含.xls,.xlsx),如果工程提示未在本地计算机上注册,请自行百度解决方案并安装缺失组件
直接打开Excel 工作簿 ,读取Excel中的数据
主要介绍了C#利用Openxml读取Excel数据的方法,包括使用中的注意点分析及疑难探讨,需要的朋友可以参考下