50分求excel和文本文件倒入数据到acess数据库中(都是固定格式)
各位老大帮帮忙阿 没做过这方面的 最好能给个比较 详细的例子 谢谢各位了 分不够了再加~!~!
各位老大帮帮忙阿 没做过这方面的 最好能给个比较 详细的例子 谢谢各位了 分不够了再加~!~!
解决方案 »
- combox的SelectedValue问题
- 怎么取得一个TXT文件的编码方式?
- C#中如何执行sql脚本?
- 调用C#类库中一个函数就自动运行一个功能是怎么做到的
- vs.net2003c# 在winfrm中如何实现OFFICE菜单效果
- 把产生的随机数进行排序
- 请问大家知道哪有提供c#试题的论坛或者网站不?
- 如何从一个datatable里生成由满足特定条件的行组成的datatable或dataview?
- 请问:我用C#做了一个程序,安装到其他机器时,不想装.net framework,可以把必要的系统文件Copy到程序目录中吗?有那些文件?
- 能否分享个图书管理系统?!
- 就要辞职离开天津去北京工作了,工作一年后的我该如何......
- 求救:::在ASP.Net Web应用程序项目中怎样利用C#调用exe应用程序,利用Process.Start("")为什么不行?
//创建一个数据链接
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = c:\\sample.xls;Extended Properties=Excel 8.0" ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom = " SELECT * FROM [Sheet1$] " ;
myConn.Open ( ) ;
file://打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
file://创建一个 DataSet对象
myDataSet = new DataSet ( ) ;
file://得到自己的DataSet对象
myCommand.Fill ( myDataSet , "[Sheet1$]" ) ;
file://关闭此数据链接
myConn.Close ( ) ;
怎么样读取Excel表格中的数据其实和读取数据库中的数据没有什么实质上的区别。 注释:这里读取的是C盘根目录下的"Sample.xls"文件。 (2).用DataGrid来显示得到的数据集: 在得到DataSet对象后,只需要通过下列二行代码,就可以把数据集用DataGrid显示出来了:DataGrid1.DataMember= "[Sheet1$]" ;
DataGrid1.DataSource = myDataSet ; (3).用Visual C#读取Excel表格,并用DataGrid显示出来的程序代码(Read.cs)和程序运行的界面: 掌握了上面二点,水到渠成就可以得到以下代码:using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
using System.Data.OleDb ;
public class Form1 : Form
{
private Button button1 ;
private System.Data.DataSet myDataSet ;
private DataGrid DataGrid1 ;
private System.ComponentModel.Container components = null ;public Form1 ( )
{
file://初始化窗体中的各个组件
InitializeComponent ( ) ;
file://打开数据链接,得到数据集
GetConnect ( ) ;
}
file://清除程序中使用过的资源
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}private void GetConnect ( )
{
file://创建一个数据链接
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = c:\\sample.xls;Extended Properties=Excel 8.0" ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom = " SELECT * FROM [Sheet1$] " ;
myConn.Open ( ) ;
file://打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
file://创建一个 DataSet对象
myDataSet = new DataSet ( ) ;
file://得到自己的DataSet对象
myCommand.Fill ( myDataSet , "[Sheet1$]" ) ;
file://关闭此数据链接
myConn.Close ( ) ;
}
private void InitializeComponent ( )
{
DataGrid1 = new DataGrid ( ) ;
button1 = new Button ( ) ;
SuspendLayout ( ) ;
DataGrid1.Name = "DataGrid1";
DataGrid1.Size = new System.Drawing.Size ( 400 , 200 ) ;button1.Location = new System.Drawing.Point ( 124 , 240 ) ;
button1.Name = "button1" ;
button1.TabIndex = 1 ;
button1.Text = "读取数据" ;
button1.Size = new System.Drawing.Size (84 , 24 ) ;
button1.Click += new System.EventHandler ( this.button1_Click ) ;this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ;
this.ClientSize = new System.Drawing.Size ( 400 , 280 ) ;
this.Controls.Add ( button1 ) ;
this.Controls.Add ( DataGrid1 ) ;
this.Name = "Form1" ;
this.Text = "读取Excle表格中的数据,并用DataGrid显示出来!" ;
this.ResumeLayout ( false ) ;}
private void button1_Click ( object sender , System.EventArgs e )
{
DataGrid1.DataMember= "[Sheet1$]" ;
DataGrid1.DataSource = myDataSet ;}
static void Main ( )
{
Application.Run ( new Form1 ( ) ) ;
}
}
怎么写入access 不用我说了吧。
再建一个Access的连接,得到Access的DataSet,
然后遍历Excel的DataSet中的数据并插入Access的DataSet,然后写回就可以了.
我就是这么干的,方法比较笨,不知谁有更好的方法.
先将Excel另存为XML文件,然后在Access中导入,以下是VB代码(打开Excel情况下)
ActiveWorkbook.SaveAs Filename:= _
"C:\Documents and Settings\Teacher\桌面\Book1.xml", FileFormat:= _
xlXMLSpreadsheet, ReadOnlyRecommended:=False, CreateBackup:=False
Application.ImportXML _
DataSource:="employees.xml", _
ImportOptions:=acStructureAndData
DataSource:="Book1.xml", _
ImportOptions:=acStructureAndData