Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1265710
  • 博文数量: 482
  • 博客积分: 13297
  • 博客等级: 上将
  • 技术积分: 2890
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-12 16:25
文章分类

全部博文(482)

文章存档

2012年(9)

2011年(407)

2010年(66)

分类: WINDOWS

2011-11-25 11:38:10

一、AccessExcel中导入数据

1.用到的Excel表的格式及内容

 

实现

 

  1. OleDbConnection con = new OleDbConnection();  
  2.             try  
  3.             {  
  4.                 OpenFileDialog openFile = new OpenFileDialog();//打开文件对话框。  
  5.                 openFile.Filter = ("Excel 文件(*.xls)|*.xls");//后缀名。  
  6.                 if (openFile.ShowDialog() == DialogResult.OK)  
  7.                 {  
  8.                     string filename = openFile.FileName;  
  9.                     int index = filename.LastIndexOf("//");//截取文件的名字  
  10.                     filename = filename.Substring(index + 1);  
  11.                     conExcel.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" +   
  12.                                                 Application.StartupPath + "//Appdata.mdb";  
  13.                     //将excel导入access  
  14.                     //distinct :删除excel重复的行.  
  15.                     //[excel名].[sheet名] 已有的excel的表要加$  
  16.                     //where not in : 插入不重复的记录。  
  17.                       
  18.                     string sql = "insert into Users2(用户编号,用户姓名) select distinct * from [Excel 8.0;database=" +  
  19.                                 filename + "].[name$] where 用户编号 not in (select 用户编号 from Users2) ";  
  20.                     OleDbCommand com = new OleDbCommand(sql, con);  
  21.                     con.Open();  
  22.                     com.ExecuteNonQuery();  
  23.                     MessageBox.Show("导入数据成功""导入数据", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  24.                 }  
  25.             }  
  26.             catch (Exception ex)  
  27.             {  
  28.                 MessageBox.Show(ex.ToString());  
  29.             }  
  30.             finally  
  31.             {  
  32.                 con.Close();  
  33.             }  

 

二、Access导出Excel

  1. OleDbConnection con = new OleDbConnection();  
  2.             try  
  3.             {  
  4.                 SaveFileDialog saveFile = new SaveFileDialog();  
  5.                 saveFile.Filter = ("Excel 文件(*.xls)|*.xls");//指定文件后缀名为Excel 文件。  
  6.                 if (saveFile.ShowDialog() == DialogResult.OK)  
  7.                 {  
  8.                     string filename = saveFile.FileName;  
  9.                     if (System.IO.File.Exists(filename))  
  10.                     {  
  11.                         System.IO.File.Delete(filename);//如果文件存在删除文件。  
  12.                     }  
  13.                     int index = filename.LastIndexOf("//");//获取最后一个/的索引  
  14.                     filename = filename.Substring(index + 1);//获取excel名称(新建表的路径相对于SaveFileDialog的路径)  
  15.                     //select * into 建立 新的表。  
  16.                     //[[Excel 8.0;database= excel名].[sheet名] 如果是新建sheet表不能加$,如果向sheet里插入数据要加$.   
  17.                     //sheet最多存储65535条数据。  
  18.                     string sql = "select top 65535 *  into   [Excel 8.0;database=" + filename + "].[用户信息] from Users2";  
  19.                     con.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + Application.StartupPath + "//Appdata.mdb";//将数据库放到debug目录下。  
  20.                     OleDbCommand com = new OleDbCommand(sql, con);  
  21.                     con.Open();  
  22.                     com.ExecuteNonQuery();  
  23.   
  24.                     MessageBox.Show("导出数据成功""导出数据", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  25.                 }  
  26.             }  
  27.             catch (Exception ex)  
  28.             {  
  29.                 MessageBox.Show(ex.ToString());  
  30.             }  
  31.             finally  
  32.             {  
  33.                 con.Close();  
  34.             }  

====

http://blog.csdn.net/licl19870605/article/details/5260795

阅读(2209) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~