using System;
using SystemCollectionsGeneric;
using SystemText;
using SystemConfiguration;
using SystemData;
using SystemDataOleDb;
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 = HDRNo;
private ExcelFileFormat _excelformat = ExcelFileFormatExcel97OR2003;
private string _connectionString2003 = "Provider=MicrosoftJetOLEDB40;Extended Properties=\"Excel 80;HDR={1};IMEX=1\";data source=\"{0}\"";
private string _connectionString2007 = "Provider=MicrosoftACEOLEDB120;Data Source=\"{0}\";Extended Properties=\"Excel 120;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 == ExcelFileFormatExcel97OR2003)
{
this_connectionString = stringFormat(this_connectionString2003, filename, this_excelHDR);
this_connection = new OleDbConnection(this_connectionString);
}
else if (excelFormat == ExcelFileFormatExcel2007)
{
this_connectionString = stringFormat(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 == ExcelFileFormatExcel97OR2003)
{
this_connectionString = stringFormat(this_connectionString2003, filename, this_excelHDR);
this_connection = new OleDbConnection(this_connectionString);
}
else if (excelFormat == ExcelFileFormatExcel2007)
{
this_connectionString = stringFormat(this_connectionString2007, filename, this_excelHDR);
this_connection = new OleDbConnection(this_connectionString);
}
}
~Excel()
{
thisDispose();
}
#endregion
#region 方法
#region 事务
#region 开始一个Excel文件事务
/// <summary>
/// 开始一个Excel文件事务
/// </summary>
public void BeginTransaction()
{
if (this_connectionState != ConnectionStateOpen && this_connectionState != ConnectionStateConnecting)
{
this_connectionOpen();
}
this_tran = this_connectionBeginTransaction();
}
#endregion
#region 提交一个Excel文件事务
/// <summary>
/// 提交一个Excel文件事务
/// </summary>
public void CommitTransaction()
{
if (this_tran != null)
{
this_tranCommit();
}
thisDispose();
}
#endregion
#region 回滚一个Excel文件事务
/// <summary>
/// 回滚一个Excel文件事务
/// </summary>
public void RollbackTransaction()
{
if (this_tran != null)
{
this_tranRollback();
}
thisDispose();
}
#endregion
#region 关联一个事务
/// <summary>
/// 关联一个事务
/// </summary>
/// <param name="tran">事务对象</param>
/// <param name="comm">命令对象</param>
private void AddTransactionToCommand(OleDbTransaction tran, OleDbCommand comm)
{
if (tran != null)
{
commTransaction = 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_connectionState != ConnectionStateOpen && this_connectionState != ConnectionStateConnecting)
{
this_connectionOpen();
}
thisAddTransactionToCommand(this_tran, sc);
OleDbDataAdapter sda = new OleDbDataAdapter(sc);
DataSet ds = new DataSet();
try
{
sdaFill(ds);
sdaDispose();
scDispose();
}
catch (Exception e)
{
thisLogException(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();
scConnection = this_connection;
if (this_connectionState != ConnectionStateOpen && this_connectionState != ConnectionStateConnecting)
{
this_connectionOpen();
}
thisAddTransactionToCommand(this_tran, sc);
scCommandText = ProcedureName;
scCommandType = CommandTypeStoredProcedure;
for (int i = 0; i < ParametersLength; i++)
{
scParametersAdd(new OleDbParameter(Parameters[i], Values[i]));
}
OleDbDataAdapter sda = new OleDbDataAdapter(sc);
DataSet ds = new DataSet();
try
{
sdaFill(ds);
sdaDispose();
scDispose();
}
catch (Exception e)
{
thisLogException(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_connectionState != ConnectionStateOpen && this_connectionState != ConnectionStateConnecting)
{
this_connectionOpen();
}
thisAddTransactionToCommand(this_tran, sc);
OleDbDataAdapter sda = new OleDbDataAdapter(sc);
DataTable dt = new DataTable();
try
{
sdaFill(dt);
sdaDispose();
scDispose();
}
catch (Exception e)
{
thisLogException(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();
scConnection = this_connection;
if (this_connectionState != ConnectionStateOpen && this_connectionState != ConnectionStateConnecting)
{
this_connectionOpen();
}
thisAddTransactionToCommand(this_tran, sc);
scCommandText = ProcedureName;
scCommandType = CommandTypeStoredProcedure;
for (int i = 0; i < ParametersLength; i++)
{
scParametersAdd(new OleDbParameter(Parameters[i], Values[i]));
}
OleDbDataAdapter sda = new OleDbDataAdapter(sc);
DataTable dt = new DataTable();
try
{
sdaFill(dt);
sdaDispose();
scDispose();
}
catch (Exception e)
{
thisLogException(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_connectionState != ConnectionStateOpen && this_connectionState != ConnectionStateConnecting)
{
this_connectionOpen();
}
thisAddTransactionToCommand(this_tran, sc);
try
{
scExecuteNonQuery();
scDispose();
}
catch (Exception e)
{
thisLogException(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();
scConnection = this_connection;
if (this_connectionState != ConnectionStateOpen && this_connectionState != ConnectionStateConnecting)
{
this_connectionOpen();
}
thisAddTransactionToCommand(this_tran, sc);
scCommandText = ProcedureName;
scCommandType = CommandTypeStoredProcedure;
for (int i = 0; i < ParametersLength; i++)
{
scParametersAdd(new OleDbParameter(Parameters[i], Values[i]));
}
try
{
scExecuteNonQuery();
scDispose();
}
catch (Exception e)
{
thisLogException(e);
}
}
#endregion
#endregion
#endregion
#region 附加功能
#region 获取所有表名称
/// <summary>
/// 获取所有表名称
/// </summary>
/// <returns>string[] 表名称</returns>
public string[] GetShemaTableName()
{
//获取数据表
if (this_connectionState != ConnectionStateOpen && this_connectionState != ConnectionStateConnecting)
{
this_connectionOpen();
}
try
{
DataTable shemaTable = this_connectionGetOleDbSchemaTable(OleDbSchemaGuidTables, new object[] { null, null, null, "TABLE" });
int n = shemaTableRowsCount;
string[] strTable = new string[n];
int m = shemaTableColumnsIndexOf("TABLE_NAME");
for (int i = 0; i < n; i++)
{
DataRow m_DataRow = shemaTableRows[i];
strTable[i] = m_DataRowItemArrayGetValue(m)ToString();
}
return strTable;
}
catch (Exception e)
{
thisLogException(e);
return null;
}
}
#endregion
#endregion
#region Excel文件 *** 作异常日志
/// <summary>
/// Excel文件错误日志记录
/// </summary>
/// <param name="e">异常信息对象</param>
private void LogException(Exception e)
{
throw new Exception(eMessage);
}
#endregion
#endregion
#region IDisposable 成员
public void Dispose()
{
#region 注销Excel文件事务
if (this_tran != null)
{
this_tranDispose();
}
#endregion
if (this_connection != null)
{
#region 关闭Excel文件连接
if (this_connectionState != ConnectionStateClosed)
{
this_connectionClose();
this_connectionDispose();
}
}
#endregion
}
#endregion
}
}
1、首先进入到sqlserver应用程序的 *** 作页面中,鼠标右键单击想要放入Excel表格的数据库。
2、接下来需要点击任务,再点击任务中的导入数据选项,
3、接下来就会出现如下方长的页面,点击进行下一步。
4、接下来就需要在新出现的界面中,根据要求选择数据源 Excel,文件路径,以及Excel版本,进行下一步。
5、然后是目标数据库。选择“ msslserver”作为目标,无需更改服务器名称,输入对应的密码进行认证,选择对应的数据库作为数据库,最后单击“下一步”,如图所示。
6、所示界面中的默认选项就足够了。点击下一步。
7、图形界面,可以自定义目标数据库(该表可能不存在于数据库中),
8、单击预览以查看导入后的表状态,如图所示。
9、接下来在新出现的页面中,进行下一步。
10、最后,单击“完成”以成功导入。
表单引用数据库的具体方法会因使用的工具和平台而有所不同,以下是一些通用的步骤:
1 创建一个数据库并添加数据:在表单中引用数据库之前,您需要确保已创建了一个包含相关数据的数据库,并且该数据库已连接到您的表单。
2 打开表单设计器:在表单设计器中,找到您要引用数据库的位置。这可能是一个文本框、下拉列表或其他控件。
3 添加数据源:在设计器中,找到“数据源”的选项并添加您的数据库。此时可以选择在设计时预览或者运行时再加载数据。
4 配置控件:选择您想要配置为引用数据库的控件,然后将其绑定到适当的数据源和字段。对于文本框和下拉列表等控件,可以通过选择“数据绑定”选项来完成此 *** 作。
5 测试表单:保存并运行表单,以确保它正确地引用了您的数据库中的数据。如果需要,在测试过程中可以进行调整和修改。
需要注意的是,在定义表单与数据库之间的关系时,确保遵循最佳实践以确保安全性和性能方面的考虑。另外,在开发生产级别应用程序之前,请务必进行详尽测试以确保其稳定性和可靠性。
以上就是关于怎么用C#语言实现读取EXCEL的表格结构,在把表格中的数据导入到数据库中全部的内容,包括:怎么用C#语言实现读取EXCEL的表格结构,在把表格中的数据导入到数据库中、如何将Excel表导入现有的SQL SEVER数据表里面、表单引用数据库怎么用等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)