/// <summary>
/// 导入
/// </summary>
private void btn_In_Click(object sender, EventArgs e)
{
int i = DataTableToDB();
MessageBox.Show("成功导入" + i + "条商品信息!");
} /// <summary>
/// 获取后缀名为*.xlsx的文件
/// </summary>
public void GetFile()
{
System.IO.DirectoryInfo dir = new DirectoryInfo(VPath);
if (dir.Exists)//判读是否存在改文件
{
fiList = dir.GetFiles("*.xlsx"); //获取后缀名为*.xlsx的文件
}
} /// <summary>
/// Excel数据转化为DataTable
/// </summary>
/// <param name="strSheetName"></param>
/// <param name="strExcelFileName">文件路径</param>
/// <returns>返回DataTable</returns>
public DataTable ExcelToDataTable(string strExcelFileName, string strSheetName)
{
string strConn = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=NO;IMEX=1;'", strExcelFileName);
string strExcel = string.Format("select * from [{0}$]", strSheetName);
DataSet ds = new DataSet(); using (OleDbConnection conn = new OleDbConnection(strConn))
{
conn.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
adapter.Fill(ds, strSheetName);
conn.Close();
} return ds.Tables[strSheetName];
} public int DataTableToDB()
{
GetFile();
int count = ;
string _strExcelFileName = "";
for (int i = ; i < fiList.Length; i++)
{
_strExcelFileName = dir + "\\" + fiList[i]; DataTable dtExcel = Global.g_objDb.ExcelToDataTable(_strExcelFileName, "Sheet1");
for (int j = ; j < dtExcel.Rows.Count; j++)
{
if ((ReturnSqlResultCount("select * from A where a1='" + dtExcel.Rows[j][].ToString() + "'")) > )
{
continue;
}
else
{
Global.g_objDb.InsertDataToAccess(dtExcel.Rows[j][].ToString(), dtExcel.Rows[j][].ToString(), dtExcel.Rows[j][].ToString(), dtExcel.Rows[j][].ToString(), dtExcel.Rows[j][].ToString(), dtExcel.Rows[j][].ToString(), dtExcel.Rows[j][].ToString(), dtExcel.Rows[j][].ToString()); count++;
}
}
} return count;
} String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Access_DataBase.mdb;Jet OLEDB:Database Password=123456"";  OleDbConnection Connection = new OleDbConnection(connectionString); /// <summary>
/// 执行一查询语句语句,同时返回bool值
/// </summary>
public bool InsertDataToAccess(string col1, string col2, string col3, string col4, string col5, string col6, string col7, string col8)
{
bool resultState = false; Connection.Open();
string strSQL = "insert into spdm(a,b,c,d,e,f,g,h) values('" + col1 + "','" + col1 + "','" + col1 + "','" + col1 + "','" + col1 + "','" + col1 + "','" + col1 + "','" + col1 + "')";
OleDbTransaction myTrans = Connection.BeginTransaction();
OleDbCommand command = new OleDbCommand(strSQL, Connection, myTrans); try
{
command.ExecuteNonQuery();
myTrans.Commit();
resultState = true;
}
catch
{
myTrans.Rollback();
resultState = false;
}
finally
{
Connection.Close();
}
return resultState;
} /// <summary>
/// 执行一查询语句,同时返回查询结果数目
/// </summary>
/// <param name="strSQL"></param>
/// <returns></returns>
public int ReturnSqlResultCount(string strSQL)
{
int sqlResultCount = ; try
{
Connection.Open();
OleDbCommand command = new OleDbCommand(strSQL, Connection);
OleDbDataReader dataReader = command.ExecuteReader(); while (dataReader.Read())
{
sqlResultCount++;
}
dataReader.Close();
}
catch
{
sqlResultCount = ;
}
finally
{
Connection.Close();
}
return sqlResultCount;
}

C# Excel导入Access的更多相关文章

  1. [办公自动化] 再读《让EXCEL飞》(从excel导入access数据时,union联合查询,数据源中没有包含可见的表格)

    一年多以前就买了@Mrexcel的<让excel飞>这本书.整体思路是利用access结合excel,大幅度提高数据分析效率. 最近又拿出来看了看.第十五章,比高级筛选更“高级”,P241 ...

  2. C#中实现excel文件批量导入access数据表中

    一 .界面简单设计如下: 二 .代码如下: using System; using System.Collections.Generic; using System.ComponentModel; u ...

  3. 解决SQL Server 2008 64位系统无法导入Access/Excel的问题 2012/08/01

    操作系统Windows Server 2008 X64,数据库SQL Server 2008 X64,Office 2007(好像只有32位),在存储过程执行OpenDatasource导入Acces ...

  4. 终于解决SQL Server 2008 64位系统无法导入Access/Excel的问题 2012/08/01

    最近更换了新服务器,操作系统Windows Server 2008 X64,数据库SQL Server 2008 X64,Office 2007(好像只有32位),在存储过程执行OpenDatasou ...

  5. 解决SQL Server 2008 64位系统无法导入Access/Excel的问题

    最近更换了新服务器,操作系统Windows Server 2008 X64,数据库SQL Server 2008 X64,Office 2007(好像只有32位),在存储过程执行OpenDatasou ...

  6. 微软BI 之SSIS 系列 - 在 SSIS 中导入 ACCESS 数据库中的数据

    开篇介绍 来自 天善学院 一个学员的问题,如何在 SSIS 中导入 ACCESS 数据表中的数据. 在 SSIS 中导入 ACCESS 数据库数据 ACCESS 实际上是一个轻量级的桌面数据库,直接使 ...

  7. winform之excel导入和导出

    引用命名空间   using Microsoft.Office.Interop.Excel;DataGridView 导出到Excel public static void SaveAs(DataGr ...

  8. ASP.NET 将Excel导入数据库

    将Excel导入数据库大致流程:  Excel数据->DataSet->数据库 需要做的准备:1.FileUpload控件一个,按钮一个,如果需要即时显示那么GridView或DataGr ...

  9. 从Excel转Access的一个方法说开去(DataRow的state状态)

    因为客户对access不太熟悉,更喜欢玩EXCEL.但是系统要求导入ACCESS.所以我们得做个把EXCEL转换成Access的小工具.(别问我为啥不让系统直接导入excel....我不知道!),然后 ...

随机推荐

  1. vi/vim基本使用命令

    vi/vim基本使用命令 一.总结 一句话总结:1.记住三种模式:命令行模式.插入模式.底行模式:2.记住两个按键功能:i和esc 二.vi/vim基本使用命令 vi/vim 基本使用方法本文介绍了v ...

  2. MFC获取各个窗体(体)之间的指针(对象)

    MFC在非常多的对话框操作中,我们常常要用到在一个对话框中调用还有一个对话框的函数或变量.能够用例如以下方法来解决.    HWND hWnd=::FindWindow(NULL,_T("S ...

  3. scala读写文件 comparing values of types Unit and Int using `!=' will always yield true

    由于scala没有对写入文件的支持,所以写文件时通常借助java进行IO操作 //方式一(小文件) /* val s1 = Source.fromFile("D:\\inputword\\h ...

  4. java phoenix 连接hbase

    <dependency> <groupId>org.apache.phoenix</groupId> <artifactId>phoenix-core& ...

  5. Erlang OTP编程初体验——gen_server和行为模式

    http://blog.sina.com.cn/s/blog_3fe961ae0101k4p6.html 行为模式其实非常类似于面向对象语言中的接口,至少笔者是这么理解的.OTP行为模式将一些反复出现 ...

  6. 开源:通用的日志分析工具(LogViewer)

    工具介绍 本工具最早是制作出来查看我的 FTL(Fast Trace Log) 二进制日志文件的, 后来因为去做Java后台,经常看 SpringBoot, Tomcat 等的日志, 就简单重构了一下 ...

  7. Spring boot传统部署

    使用spring boot很方便,一个jar包就可以启动了,因为它里面内嵌了tomcat等服务器. 但是spring boot也提供了部署到独立服务器的方法. 如果你看文档的话,从jar转换为war包 ...

  8. jquery 源码学习(三)

    jQuery源码分析-03构造jQuery对象-源码结构和核心函数,需要的朋友可以参考下.   作者:nuysoft/高云 QQ:47214707 EMail:nuysoft@gmail.com 毕竟 ...

  9. CUDA一维纹理内存

    纹理一词来源于GPU图形世界,GPU通用并行计算"盗用"了纹理一词,定义了一个纹理内存的概念.纹理内存缓存在 设备上,在某些情况下能减少对内存的请求并降低内存带宽的使用,是专门为那 ...

  10. 使用 Microsoft.UI.Xaml 解决 UWP 控件和对老版本 Windows 10 的兼容性问题

    原文 使用 Microsoft.UI.Xaml 解决 UWP 控件和对老版本 Windows 10 的兼容性问题 虽然微软宣称 Windows 10 将是最后一个 Windows 版本,但由于年代跨越 ...