问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

怎么用C#语言实现读取EXCEL的表格结构,在把表格中的数据导入到数据库中...

发布网友 发布时间:2024-02-21 15:52

我来回答

3个回答

热心网友 时间:2024-07-24 06:58

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.OleDb;

namespace Excel
{
/// <summary>
/// Excel数据交换类
/// </summary>
public class Excel : IDisposable
{
#region 自定义类型
/// <summary>
/// 是否将第一行作为表头
/// </summary>
public enum HDR
{
/// <summary>
/// 将第一行作为表头
/// </summary>
Yes,
/// <summary>
/// 不用第一行作为表头
/// </summary>
No
};
/// <summary>
/// Excel文件格式
/// </summary>
public enum ExcelFileFormat
{
/// <summary>
/// Excel97/2003格式
/// </summary>
Excel97OR2003,
/// <summary>
/// Excel2007格式
/// </summary>
Excel2007
};
#endregion

#region 变量
private HDR _excelHDR = HDR.No;
private ExcelFileFormat _excelformat = ExcelFileFormat.Excel97OR2003;
private string _connectionString2003 = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0;HDR={1};IMEX=1\";data source=\"{0}\"";
private string _connectionString2007 = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\"{0}\";Extended Properties=\"Excel 12.0;HDR={1};IMEX=1\";";
private string _connectionString = "";
private string _filename;
private OleDbConnection _connection;
private OleDbTransaction _tran;
#endregion

#region 属性

#region ConnectionString
/// <summary>
/// 获取系统的连接字符串
/// </summary>
public string ConnectionString
{
get
{
return this._connectionString;
}
}
#endregion

#region 是否将第一行作为表头
/// <summary>
/// 获取或设置是否将第一行作为表头
/// </summary>
public HDR ExcelHDR
{
get
{
return this._excelHDR;
}
set
{
this._excelHDR = value;
}
}
#endregion

#region Excel文件格式
/// <summary>
/// 获取或设置当前Excel文件的格式
/// </summary>
public ExcelFileFormat ExcelFormat
{
get
{
return this._excelformat;
}
set
{
this._excelformat = value;
}
}
#endregion

#endregion

#region 构造函数
/// <summary>
/// 创建一个Excel文件链接对象
/// </summary>
/// <param name="filename">Excel文件完整路径</param>
/// <param name="excelFormat">Excel文件格式</param>
public Excel(string filename, ExcelFileFormat excelFormat)
{
this._filename = filename;
if (excelFormat == ExcelFileFormat.Excel97OR2003)
{
this._connectionString = string.Format(this._connectionString2003, filename, this._excelHDR);
this._connection = new OleDbConnection(this._connectionString);
}
else if (excelFormat == ExcelFileFormat.Excel2007)
{
this._connectionString = string.Format(this._connectionString2007, filename, this._excelHDR);
this._connection = new OleDbConnection(this._connectionString);
}
}
/// <summary>
/// 创建一个Excel文件链接对象
/// </summary>
/// <param name="filename">Excel文件完整路径</param>
/// <param name="excelFormat">Excel文件格式</param>
/// <param name="hdr">是否将第一行作为表头</param>
public Excel(string filename, ExcelFileFormat excelFormat, HDR hdr)
{
this._filename = filename;
this._excelHDR = hdr;
if (excelFormat == ExcelFileFormat.Excel97OR2003)
{
this._connectionString = string.Format(this._connectionString2003, filename, this._excelHDR);
this._connection = new OleDbConnection(this._connectionString);
}
else if (excelFormat == ExcelFileFormat.Excel2007)
{
this._connectionString = string.Format(this._connectionString2007, filename, this._excelHDR);
this._connection = new OleDbConnection(this._connectionString);
}
}

~Excel()
{
this.Dispose();
}
#endregion

#region 方法

#region 事务

#region 开始一个Excel文件事务
/// <summary>
/// 开始一个Excel文件事务
/// </summary>
public void BeginTransaction()
{
if (this._connection.State != ConnectionState.Open && this._connection.State != ConnectionState.Connecting)
{
this._connection.Open();
}
this._tran = this._connection.BeginTransaction();
}
#endregion

#region 提交一个Excel文件事务
/// <summary>
/// 提交一个Excel文件事务
/// </summary>
public void CommitTransaction()
{
if (this._tran != null)
{
this._tran.Commit();
}
this.Dispose();
}
#endregion

#region 回滚一个Excel文件事务
/// <summary>
/// 回滚一个Excel文件事务
/// </summary>
public void RollbackTransaction()
{
if (this._tran != null)
{
this._tran.Rollback();
}
this.Dispose();
}
#endregion

#region 关联一个事务
/// <summary>
/// 关联一个事务
/// </summary>
/// <param name="tran">事务对象</param>
/// <param name="comm">命令对象</param>
private void AddTransactionToCommand(OleDbTransaction tran, OleDbCommand comm)
{
if (tran != null)
{
comm.Transaction = tran;
}
}
#endregion

#endregion

#region 查询分析

#region DataSet

#region DataSet QueryDataSet(string sql)
/// <summary>
/// 通过一个Excel-SQL语句查询
/// </summary>
/// <param name="sql">sql</param>
/// <returns>DataSet结果集</returns>
public DataSet QueryDataSet(string sql)
{
OleDbCommand sc = new OleDbCommand(sql, this._connection);
if (this._connection.State != ConnectionState.Open && this._connection.State != ConnectionState.Connecting)
{
this._connection.Open();
}
this.AddTransactionToCommand(this._tran, sc);
OleDbDataAdapter sda = new OleDbDataAdapter(sc);
DataSet ds = new DataSet();
try
{
sda.Fill(ds);
sda.Dispose();
sc.Dispose();
}
catch (Exception e)
{
this.LogException(e);
}
return ds;
}
#endregion

#region DataSet QueryDataSet(string ProcedureName,string[] Parameters,object[] Values)
/// <summary>
/// 通过存储过程与参数进行查询
/// </summary>
/// <param name="ProcedureName">存储过程名</param>
/// <param name="Paramters">参数数组</param>
/// <param name="Values">值数组</param>
/// <returns>DataSet数据集</returns>
public DataSet QueryDataSet(string ProcedureName, string[] Parameters, object[] Values)
{
OleDbCommand sc = new OleDbCommand();
sc.Connection = this._connection;
if (this._connection.State != ConnectionState.Open && this._connection.State != ConnectionState.Connecting)
{
this._connection.Open();
}
this.AddTransactionToCommand(this._tran, sc);
sc.CommandText = ProcedureName;
sc.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < Parameters.Length; i++)
{
sc.Parameters.Add(new OleDbParameter(Parameters[i], Values[i]));
}

OleDbDataAdapter sda = new OleDbDataAdapter(sc);
DataSet ds = new DataSet();
try
{
sda.Fill(ds);
sda.Dispose();
sc.Dispose();
}
catch (Exception e)
{
this.LogException(e);
}
return ds;
}
#endregion

#endregion

#region DataTable

#region DataTable QueryDataTable(string sql)
/// <summary>
/// 通过一个Excel-SQL语句查询
/// </summary>
/// <param name="sql">sql</param>
/// <returns>DataTable结果集</returns>
public DataTable QueryDataTable(string sql)
{
OleDbCommand sc = new OleDbCommand(sql, this._connection);
if (this._connection.State != ConnectionState.Open && this._connection.State != ConnectionState.Connecting)
{
this._connection.Open();
}
this.AddTransactionToCommand(this._tran, sc);
OleDbDataAdapter sda = new OleDbDataAdapter(sc);
DataTable dt = new DataTable();
try
{
sda.Fill(dt);
sda.Dispose();
sc.Dispose();
}
catch (Exception e)
{
this.LogException(e);
}
return dt;
}
#endregion

#region DataTable QueryDataTable(string ProcedureName,string[] Parameters,object[] Values)
/// <summary>
/// 通过存储过程与参数进行查询
/// </summary>
/// <param name="ProcedureName">存储过程名</param>
/// <param name="Paramters">参数数组</param>
/// <param name="Values">值数组</param>
/// <returns>DataTable数据集</returns>
public DataTable QueryDataTable(string ProcedureName, string[] Parameters, object[] Values)
{
OleDbCommand sc = new OleDbCommand();
sc.Connection = this._connection;
if (this._connection.State != ConnectionState.Open && this._connection.State != ConnectionState.Connecting)
{
this._connection.Open();
}
this.AddTransactionToCommand(this._tran, sc);
sc.CommandText = ProcedureName;
sc.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < Parameters.Length; i++)
{
sc.Parameters.Add(new OleDbParameter(Parameters[i], Values[i]));
}
OleDbDataAdapter sda = new OleDbDataAdapter(sc);
DataTable dt = new DataTable();
try
{
sda.Fill(dt);
sda.Dispose();
sc.Dispose();
}
catch (Exception e)
{
this.LogException(e);
}
return dt;
}
#endregion

#endregion

#region void

#region void Query(string sql)
/// <summary>
/// 通过一个Excel-SQL语句查询
/// </summary>
/// <param name="sql">sql</param>
public void Query(string sql)
{
OleDbCommand sc = new OleDbCommand(sql, this._connection);
if (this._connection.State != ConnectionState.Open && this._connection.State != ConnectionState.Connecting)
{
this._connection.Open();
}
this.AddTransactionToCommand(this._tran, sc);
try
{
sc.ExecuteNonQuery();
sc.Dispose();
}
catch (Exception e)
{
this.LogException(e);
}
}
#endregion

#region void Query(string ProcedureName,string[] Parameters,object[] Values)
/// <summary>
/// 通过存储过程与参数进行查询
/// </summary>
/// <param name="ProcedureName">存储过程名</param>
/// <param name="Paramters">参数数组</param>
/// <param name="Values">值数组</param>
/// <returns>DataSet数据集</returns>
public void Query(string ProcedureName, string[] Parameters, object[] Values)
{
OleDbCommand sc = new OleDbCommand();
sc.Connection = this._connection;
if (this._connection.State != ConnectionState.Open && this._connection.State != ConnectionState.Connecting)
{
this._connection.Open();
}
this.AddTransactionToCommand(this._tran, sc);
sc.CommandText = ProcedureName;
sc.CommandType = CommandType.StoredProcedure;
for (int i = 0; i < Parameters.Length; i++)
{
sc.Parameters.Add(new OleDbParameter(Parameters[i], Values[i]));
}
try
{
sc.ExecuteNonQuery();
sc.Dispose();
}
catch (Exception e)
{
this.LogException(e);
}
}
#endregion

#endregion

#endregion

#region 附加功能

#region 获取所有表名称
/// <summary>
/// 获取所有表名称
/// </summary>
/// <returns>string[] 表名称</returns>
public string[] GetShemaTableName()
{

//获取数据表
if (this._connection.State != ConnectionState.Open && this._connection.State != ConnectionState.Connecting)
{
this._connection.Open();
}
try
{
DataTable shemaTable = this._connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
int n = shemaTable.Rows.Count;
string[] strTable = new string[n];
int m = shemaTable.Columns.IndexOf("TABLE_NAME");
for (int i = 0; i < n; i++)
{
DataRow m_DataRow = shemaTable.Rows[i];
strTable[i] = m_DataRow.ItemArray.GetValue(m).ToString();
}
return strTable;
}
catch (Exception e)
{
this.LogException(e);
return null;
}
}
#endregion

#endregion

#region Excel文件操作异常日志
/// <summary>
/// Excel文件错误日志记录
/// </summary>
/// <param name="e">异常信息对象</param>
private void LogException(Exception e)
{
throw new Exception(e.Message);
}
#endregion

#endregion

#region IDisposable 成员

public void Dispose()
{
#region 注销Excel文件事务
if (this._tran != null)
{
this._tran.Dispose();
}
#endregion

if (this._connection != null)
{
#region 关闭Excel文件连接
if (this._connection.State != ConnectionState.Closed)
{
this._connection.Close();
this._connection.Dispose();
}
}
#endregion
}

#endregion
}
}

热心网友 时间:2024-07-24 06:52

就是数据的导入导出啊,很麻烦的一个事情,你必须会数据库的导入和导出的做法

热心网友 时间:2024-07-24 06:55

我的博客里面有:http://blog.163.com/kymdidicom@126/blog/static/114702287201011102648891/
看看能不能帮到你所要求的
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
请问信用卡暂未批准是什么意思 短发搭配什么帽子能吸睛?get下文各种短发的搭配攻略 入门智能机vivoy200t怎么样? 直短发配什么帽子好看 老狗狗为什么总低头 脚跟骨裂做了手术还装了钢板,请问要多长时间才能恢复。 脚跟粉碎性骨折术后多久能走路 脚跟骨折多久可以正常走路 如图,AD是∠BAC的平分线,点E在AB上,且AE=AC,EF ∥ BC交AC于点F.试说明... 如图,AD是角BAC的平分线,点E在AB上,且AE=AC,EF//BC交AC于点F.求证:EC... 改错了未满一年怎么改回来 如何强制二次修改? 目前中国有生产硬盘的厂商吗 关于微信绑定上限的问题:你的手机号在最近24小时内已绑定过3个... 关于微信绑定上限的问题:你的手机号在最近24小时内已绑定过3个... ...号在最近24小时内绑定过三个,已达到限制,...24小时后可以再次... 一年内第二次修改技巧 教师会议校长讲话稿 ...请各位帮我看看报告,24小时蛋白定量正常,肾膀胱B超正 不到一年怎么改第二次 ...上限的问题:你的手机号在最近24小时内已绑定过3个,已达到限制... 怎么在一年内修改两次 索尼RX100M6好用吗 一年内怎么改第二次? ...号在最近24小时内绑定过三个,已达到限制,...24小时后可以再次... 关于微信绑定上限的问题:你的手机号在最近24小时内已绑定过3个... 如何强制二次修改 怎么一年改两次 一年内改第二次的吗? 您的手机号在最近24小时内绑定过三个,已达到限制,...24小时后... 怎么一年内修改两次? 如何强制二次修改 如何修改第二次? 如何一年内强制二次修改 您的手机号在最近24小时内绑定过三个,已达到限制,...24小时后... ...号在最近24小时内绑定过三个,已达到限制,...24小时后可以再次... 如何强制二次修改 如何一年内强制二次修改? 长沙8岁男孩捡烟花燃放炸烂手,家长该对孩子进行怎样的安全教育?_百度... 微信没满一年怎么改? 索尼相机RX100M6 能拍视频吗? 索尼RX100M6拍视频清晰吗,最近想入手一个拍照录像两用效果好的机... ...上限的问题:你的手机号在最近24小时内已绑定过3个,已达到限制... 改错了未满一年怎么改回来 微信手机号在24小时内,已绑定两个,已达到限制,不能在绑定其他微信... 您的手机号在最近24小时内绑定过三个,已达到限制,...24小时后... 如何强制二次修改 一年内第二次修改技巧 汉川汉正服装城上河坊怎么样 成都华府大道欧尚到双丰西路咋走?