如果想将Excel中的数据转换到Access中,可以采取下面的直接导入法和建立链接法来完成。
一、直接导入法
2在“表”选项中,执行“文件→获取外部数据→导入”命令,打开“导入”对话框。
3按“文件类型”右侧的下拉按钮,选中“Microsoft Excel(xls)”选项,再定位到需要转换的工作簿文件所在的文件夹,选中相应的工作簿,按下“导入”按钮,进入“导入数据表向导”对话框
4选中需要导入的工作表(如“工程数据”),多次按“下一步”按钮作进一步的设置后,按“完成”按钮。
注意:如果没有特别要求,在上一步的 *** 作中直接按“完成”按钮就行了。
5此时系统会d出一个导入完成的对话框,按“确定”按钮。
至此,数据就从Excel中导入到Access中。
二、建立链接法
1启动Access,新建一数据库文件。
2在“表”选项中,执行“文件→获取外部数据→链接表”命令,打开“链接”对话框。
3以下 *** 作基本与上述“直接导入法”相似,在此不再赘述,请大家自行 *** 练。
注意:“直接导入法”和“建立链接法”均可以将Excel数据转换到Access中,两者除了在Access中显示的图标不同外,最大的不同是:前者转换过来的数据与数据源脱离了联系,而后者转换过来的数据会随数据源的变化而自动随时更新。
EXCEL只是简单的办公类电子表格数据库。
常见的数据库有:
access
mysql
mssql
sqlite
pgsql
MariaDB
dbf
……
1、这是因为数据库具有隐式转换功能。
2、所谓隐式转换是指:数据的类型的转换通常是由编译系统自动进行的,不需要人工干预,所以被称为隐式类型转换。
3、在数据导入时,如果不想把文本型数字转换为数值类型,可以提前定义数据库表是此列的数据类型为CHAR或VARCHAR类型。
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
}
}
其实这个就跟C#连接SQL读取表中的数据是一样的,只是在这数据库为Excel而已,通过连接字符串:
Provider=MicrosoftJetOLEDB40;Data Source=文件路径;Extended Properties=Excel 80
连接Excel即可,然后通过SQL语句:(类似)select from [Sheet1$] 把数据取出来即可,然后按照以前自己连接SQL数据库的方式把相应的字段存储起来即可。
需要留意的时03和07的连接字符串不一样,以上是03版的Excel连接方式
Excel 2007:
Provider=MicrosoftACEOLEDB120;Data Source=文件路径;Extended Properties=Excel 120
如有什么不明白的 可以留言 ^_^
以上就是关于如何将不同类型的EXCEL文件中的数据导入到数据库全部的内容,包括:如何将不同类型的EXCEL文件中的数据导入到数据库、数据库有哪些类型,EXCEL数据库属于哪种类型、excel表里的文本类型的数字导入数据库为什么会变成float类型等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)