定义一个dataset,从excel文件里面读出数据来,然后就可以直接把dataset绑定到datagrid里面显示了。当然,若是你知道excel的内容,那么可以操纵dataset显示你想要的数据而不是全部数据。

解决方案 »

  1.   

    private DataSet doImport(string strFileName)
    {
    if (strFileName=="") return null;
      
    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
    "Data Source=" +  strFileName + ";" +
    "Extended Properties=Excel 8.0;";
    OleDbDataAdapter ExcelDA = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn); DataSet ExcelDs = new DataSet();
    try
    {
    ExcelDA.Fill(ExcelDs, "ExcelInfo");

    }
    catch(Exception err)
    {
    System.Console.WriteLine( err.ToString() );
    }
    return ExcelDs;
    }
      

  2.   

    不知道你是想操作Excel还是Excel数据,如果是前者,你可以看下http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp09182003.asp
      

  3.   

    只是把Excel文档原封不动的显示的程序的窗体中。
      

  4.   

    把Excel文件中的数据读入到DataGrid中 (转孟子)使用Excel文件做为DataGrid的数据源是非常简单的,一旦数据被装载进来,就可以把数据再保存进SQL Server或XML中。我们只需要简单地使用OLE DB Provider 来访问Excel文件,然后返回DataSet即可。
    下面是要显示的Excel数据contact.xls:
    姓名 性别 地址 
    net_lover Male [email protected] 
    amxh Male [email protected] 
    孟子 E 章 Male [email protected] 
    只需要指定Excel路径,并用[]选择一个工作表即可。 完整代码如下: 
    <%@ Page Language="C#" Debug="true" %>
    <%@ Import Namespace="System.Data"%>
    <%@ Import Namespace="System.Data.OleDb"%><script runat="server">private DataSet CreateDataSource(){
    string strConn;
    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
    "Data Source=C:\\Inetpub\\wwwroot\\contacts.xls;"+
    "Extended Properties=Excel 8.0;";
    OleDbConnection conn = new OleDbConnection(strConn);
    OleDbDataAdapter myCommand = new OleDbDataAdapter("SELECT * FROM [ContactList$]", strConn);
    DataSet myDataSet = new DataSet();
    myCommand.Fill(myDataSet);
    return myDataSet;
    }
    public void Page_Load(Object sender, EventArgs e){
    if (!IsPostBack) {
    mygrid.DataSource = CreateDataSource();
    mygrid.DataBind();
    }}
    </script>
    <center>
    <form runat="server">
    <asp:datagrid runat="server" AutoGenerateColumns="false"
    width="500" id="mygrid"><HeaderStyle BorderColor="White" BackColor="black"
    ForeColor="White"
    Font-Bold="True"
    Font-Name="Arial"
    Font-Size="9" HorizontalAlign="Center"/><ItemStyle   BorderColor=""
    BackColor="#FFFFF0"
    ForeColor="Black"
    Font-Name="Arial"
    Font-Size="8"
    Font-Bold="False" HorizontalAlign="Center"/><Columns><asp:BoundColumn HeaderText="姓名" ReadOnly="true" DataField="姓名"/>
    <asp:BoundColumn HeaderText="性别" ReadOnly="true" DataField="性别"/>
    <asp:BoundColumn HeaderText="Email" ReadOnly="true" DataField="地址"/>
    </Columns></asp:datagrid>
    </form>
      

  5.   

    谢谢各位!
    但是,我想我是希望把Excel的文档在程序中显示出来,好像使用OLE之类的东东吧。我不是太懂,所以还要麻烦各位帮帮忙。
      

  6.   

    我这又word的,不过我觉得,Excel和这差不多
    如果需要给出你的邮箱,我的Email:[email protected]
      

  7.   

    private DataSet doImport(string strFileName,string strSheetName)
    {
    if (strFileName=="" || strSheetName=="") return null;
      
    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
    "Data Source=" +  strFileName + ";" +
    "Extended Properties=Excel 8.0;";
    OleDbDataAdapter ExcelDA = new OleDbDataAdapter("SELECT * FROM [" + strSheetName + "$]", strConn); DataSet ExcelDs = new DataSet();
    try
    {
    ExcelDA.Fill(ExcelDs, "ExcelInfo");

    }
    catch(Exception err)
    {
    System.Console.WriteLine( err.ToString() );
    }
    return ExcelDs;
    }
      

  8.   

    用ado.net连接excel表格,
    读取其中的指定sheet然后将数据填充到dataset中。
    再将datagrid与dataset绑定即可。注意,第一行的内容别当做了列名
    <%@Page Language="C#"%>
    <%@Import NameSpace="System.Data"%>
    <%@Import NameSpace="System.Data.OleDb"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>WebForm3</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <FONT face="宋体">
    <asp:DataGrid id="DataGrid1" style="Z-INDEX: 104; LEFT: 256px; POSITION: absolute; TOP: 56px"
    runat="server" Height="48px" Width="88px"></asp:DataGrid></FONT>
    <asp:Button id="Button1" OnClick="Button1_Click" style="Z-INDEX: 101; LEFT: 120px; POSITION: absolute; TOP: 136px" runat="server"
    Text="Button" Width="72px" Height="32px"></asp:Button>
    </form> <script language="C#" runat="server">
          public void Button1_Click(object sender,EventArgs e)
    {
     string liu=Server.MapPath("eee.xls"); 
     Response.Write(liu + "的数据内容:");
     DataSet jin=doImport(liu,"TEMP_LJC");
     DataGrid1.DataSource=jin;
     DataGrid1.DataBind();
    }
      
      
      
      private DataSet doImport(string strFileName,string strSheetName)
    {
    if (strFileName=="" || strSheetName=="") return null;
      
    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
    "Data Source=" +  strFileName + ";" +
    "Extended Properties=Excel 8.0;";
    OleDbDataAdapter ExcelDA = new OleDbDataAdapter("SELECT * FROM [" + strSheetName + "$]", strConn); DataSet ExcelDs = new DataSet();
    try
    {
    ExcelDA.Fill(ExcelDs, "ExcelInfo");

    }
    catch(Exception err)
    {
    System.Console.WriteLine( err.ToString() );
    }
    return ExcelDs;
    }
      
    </script>
    </body>
    </HTML>
      

  9.   

    ie可以调用excel来显示excel文件内容呀。
      

  10.   

    我明白楼主的意思。你是想加入一个excel窗口,
    很简单,有一个control 是 spreadsheet,可以满足你的要求。实际上是com.
    自己加进去就行了。
      

  11.   

    我还是贴出来吧,供大家讨论,word的
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    namespace WinWordControl
    {
    public class DocumentInstanceException : Exception
    {}
    public class ValidDocumentException : Exception
    {}
    public class WordInstanceException : Exception
    {}
    public class WinWordControl : System.Windows.Forms.UserControl
    { [DllImport("user32.dll")]
    public static extern int FindWindow(string strclassName, string strWindowName);
    [DllImport("user32.dll")]
    static extern int SetParent( int hWndChild, int hWndNewParent);
    [DllImport("user32.dll", EntryPoint="SetWindowPos")]
    static extern bool SetWindowPos(int hWnd,int hWndInsertAfter,    
    int X,int Y,int cx,int cy,uint uFlags);
    [DllImport("user32.dll", EntryPoint="MoveWindow")]
    static extern bool MoveWindow(int hWnd,int X,int Y,int nWidth,int nHeight,bool bRepaint);
    const int SWP_DRAWFRAME = 0x20;
    const int SWP_NOMOVE = 0x2;
    const int SWP_NOSIZE = 0x1;
    const int SWP_NOZORDER = 0x4;
    private Word.Document document;
    private static Word.ApplicationClass wd = null;
    public  static int wordWnd = 0;
    private static string filename = null;
    private static bool deactivateevents = false; private System.ComponentModel.Container components = null;
    public WinWordControl()
    {
    InitializeComponent();
    }
    protected override void Dispose( bool disposing )
    {
    CloseControl();
    if( disposing )
    {
    if( components != null )
    components.Dispose();
    }
    base.Dispose( disposing );
    }
    private void InitializeComponent()
    {
    this.Name = "WinWordControl";
    this.Size = new System.Drawing.Size(440, 336);
    this.Resize += new System.EventHandler(this.OnResize);
    }
    public void PreActivate()
    {
    if(wd == null) wd = new Word.ApplicationClass();
    }
    public void CloseControl()
    {
       try{
         deactivateevents = true;
         object dummy=null;
         document.Close(ref dummy, ref dummy, ref dummy);
        document.Application.Quit(ref dummy, ref dummy, ref dummy);
    deactivateevents = false;
    }
    catch 
    {}
    }
    private void OnClose(Word.Document doc, ref bool chancel)
    {
    if(!deactivateevents)
    chancel=true;
    }
    private void OnOpenDoc(Word.Document doc)
    {
    OnNewDoc(doc);
    }
    private void OnNewDoc(Word.Document doc)
    {
    if(!deactivateevents)
    {
    deactivateevents=true;
    object dummy = null;
    doc.Close(ref dummy,ref dummy,ref dummy);
    deactivateevents=false;
    }
    }
    private void OnQuit()
    {
    //wd=null;
    }
    public void LoadDocument(string t_filename)
    {
    deactivateevents = true;
    filename = t_filename;

    if(wd == null) wd = new Word.ApplicationClass();
    try 
    {
    wd.CommandBars.AdaptiveMenus = false;
    wd.DocumentBeforeClose += new Word.ApplicationEvents2_DocumentBeforeCloseEventHandler(OnClose);
    wd.NewDocument += new Word.ApplicationEvents2_NewDocumentEventHandler(OnNewDoc);
    wd.DocumentOpen+= new Word.ApplicationEvents2_DocumentOpenEventHandler(OnOpenDoc);
    wd.ApplicationEvents2_Event_Quit += new Word.ApplicationEvents2_QuitEventHandler(OnQuit);
    }
    catch{} if(document != null) 
    {
    try
    {
    object dummy=null;
    wd.Documents.Close(ref dummy, ref dummy, ref dummy);
    }
    catch{}
    } if( wordWnd==0 ) wordWnd = FindWindow( "Opusapp", null);
    if (wordWnd!=0)
    {
    SetParent( wordWnd, this.Handle.ToInt32());

    object fileName = filename;
    object newTemplate = false;
    object docType = 0;
    object readOnly = true;
    object isVisible = true;
    object missing = System.Reflection.Missing.Value;
    try
    {
    if( wd == null )
    {
    throw new WordInstanceException();
    } if( wd.Documents == null )
    {
    throw new DocumentInstanceException();
    }

    if( wd != null && wd.Documents != null )
    {
    document = wd.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);
    }

    if(document == null)
    {
    throw new ValidDocumentException();
    }
    }
    catch
    {
    }
    try
    {
    wd.ActiveWindow.DisplayRightRuler=false;
    wd.ActiveWindow.DisplayScreenTips=false;
    wd.ActiveWindow.DisplayVerticalRuler=false;
    wd.ActiveWindow.DisplayRightRuler=false;
    wd.ActiveWindow.ActivePane.DisplayRulers=false;
    wd.ActiveWindow.ActivePane.View.Type = Word.WdViewType.wdWebView; // .wdNormalView;
    }
    catch
    {} int counter = wd.ActiveWindow.Application.CommandBars.Count;
    for(int i = 0; i < counter;i++)
    {
    try
    { wd.ActiveWindow.Application.CommandBars[i].Enabled=false;
    }
    catch
    {}
    }
    try
    {
    wd.Visible = true;
    wd.Activate();

    SetWindowPos(wordWnd,this.Handle.ToInt32(),0,0,this.Bounds.Width+20,this.Bounds.Height+20, SWP_NOZORDER | SWP_NOMOVE | SWP_DRAWFRAME);
    MoveWindow(wordWnd,-5,-33,this.Bounds.Width+10,this.Bounds.Height+57,true);
    }
    catch
    {
    MessageBox.Show("Error: do not load the document into the control until the parent window is shown!");
    }
    this.Parent.Focus();
    }
    deactivateevents = false;
    }
    public void RestoreWord()
    {
     try
    {int counter = wd.ActiveWindow.Application.CommandBars.Count;
      for(int i = 0; i < counter;i++)
     {
       try{wd.ActiveWindow.Application.CommandBars[i].Enabled=true;
    } catch
    {}}
    }
    catch{};
    }
    private void OnResize(object sender, System.EventArgs e)
    {
    MoveWindow(wordWnd,-5,-33,this.Bounds.Width+10,this.Bounds.Height+57,true);
    }
    }
    }在外部调用
    winWordControl1.LoadDocument(openFileDialog1.FileName);
      

  12.   

    调用excel.dll,将你要显示的文件显示出来就可以啊
      

  13.   

    对 wangj2001(乡村酒吧) 发的代码,我不是很懂.
    请问,代码中的Word关键字(引用?)从何而来?