程序的主要功能是将Excel数据读取入DataGrid
自己建立的Excel可以正常读出
可是和迅给我们公司的数据库不知道是什么格式的,试了各种办法都没法读取!
和迅给的数据库下载地址:http://dongyu.59i.net/0606.xls
自己建立的Excel:http://dongyu.59i.net/test.xls
程序代码如下
///////////////////////////////////////////////////
建立
一个Button(Text:读取Excel)
一个DataGrid(Name:MyDataGrid)
---------------------------------------------------
private void button1_Click(object sender, System.EventArgs e)
{
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =d:\\0606.xls;Extended Properties=Excel 8.0" ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom = " SELECT * FROM [sheet1$] " ;
myConn.Open ( ) ;
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
DataSet myDataSet = new DataSet ( ) ;
myCommand.Fill ( myDataSet) ;
MyDataGrid.DataMember = "table";
MyDataGrid.DataSource = myDataSet;
}
---------------------------------------------
调试程序时请修改 strCon 中DataSource的正确位置
我帮你测过了,确实不行,我想可能是它的表示结果计算处理的,它的第三行数据是通过计算公式得到,不是正规的表格数据,所以认不出来,你可以考虑使用:添加引用-〉com组件-〉excel 9.0对象库
试试这个.
不过由于对DataGrid的操作不是很熟,速度有点慢.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.Reflection;
using Excel;
namespace myExcel
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.DataGrid mydataGrid;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.mydataGrid = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.mydataGrid)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(200, 216);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// mydataGrid
//
this.mydataGrid.DataMember = "";
this.mydataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.mydataGrid.Location = new System.Drawing.Point(56, 24);
this.mydataGrid.Name = "mydataGrid";
this.mydataGrid.Size = new System.Drawing.Size(400, 176);
this.mydataGrid.TabIndex = 1;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(544, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.mydataGrid,
this.button1});
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.mydataGrid)).EndInit();
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
System.Windows.Forms.Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
Excel.ApplicationClass myE=new Excel.ApplicationClass();
Excel.Workbook myEb=myE.Workbooks.Open("d:\\0606.xls",new object[]{3} ,new object[]{false},new object[]{5},new object[]{""},new object[]{""},new object[]{false},Excel.XlPlatform.xlWindows,null,new object[]{false},new object[]{true},Excel.XlFileFormat.xlExcel9795,new object[]{true});
System.Type myCell=myE.Cells[1,1].GetType();
System.Data.DataTable myTb=new System.Data.DataTable("sheet");
System.Data.DataColumn col1=new DataColumn("股票编号");
System.Data.DataColumn col2=new DataColumn("股票名称");
System.Data.DataColumn col3=new DataColumn("股票消息");
myTb.Columns.Add(col1);
myTb.Columns.Add(col2);
myTb.Columns.Add(col3);
string[,] mySource=new string[3,100];
try
{
for (int i=0;i<100;i++)
{
System.Data.DataRow myRow=myTb.NewRow();
for (int j=0;j<3;j++)
{
object a=myCell.InvokeMember("Value",BindingFlags.GetProperty,null,myE.Cells[i+1,j+1],new Object[]{});
if (a!=null)
{
myRow[j]=a.ToString();
}
else
{
myRow[j]=" ";
}
}
myTb.Rows.Add(myRow);
}
}
catch(Exception ef)
{
System.Console.WriteLine (ef.ToString());
}
myEb.Close(new object[]{false},null,null);
myE.Quit();
DataSet myDataSet = new DataSet ( ) ;
myTb.AcceptChanges();
myDataSet.Tables.Add(myTb);
myDataSet.AcceptChanges();
mydataGrid.DataMember="sheet";
mydataGrid.DataSource=myDataSet;
}
}
}