怎样将Excel的内容嵌入到程序中?

zhuyj441 2003-10-09 09:31:36
请问,怎样才能把Excel的内容嵌入到程序中进行显示?
谢谢
...全文
192 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
curdle 2003-10-09
  • 打赏
  • 举报
回复
调用excel.dll,将你要显示的文件显示出来就可以啊
wangj2001 2003-10-09
  • 打赏
  • 举报
回复
我还是贴出来吧,供大家讨论,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);
donkey567 2003-10-09
  • 打赏
  • 举报
回复
我明白楼主的意思。你是想加入一个excel窗口,
很简单,有一个control 是 spreadsheet,可以满足你的要求。实际上是com.
自己加进去就行了。
precipitant 2003-10-09
  • 打赏
  • 举报
回复
ie可以调用excel来显示excel文件内容呀。
precipitant 2003-10-09
  • 打赏
  • 举报
回复
用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>
precipitant 2003-10-09
  • 打赏
  • 举报
回复
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;
}
wangj2001 2003-10-09
  • 打赏
  • 举报
回复
我这又word的,不过我觉得,Excel和这差不多
如果需要给出你的邮箱,我的Email:wj_wj_2001@163.com
zhuyj441 2003-10-09
  • 打赏
  • 举报
回复
谢谢各位!
但是,我想我是希望把Excel的文档在程序中显示出来,好像使用OLE之类的东东吧。我不是太懂,所以还要麻烦各位帮帮忙。
acewang 2003-10-09
  • 打赏
  • 举报
回复
把Excel文件中的数据读入到DataGrid中 (转孟子)

使用Excel文件做为DataGrid的数据源是非常简单的,一旦数据被装载进来,就可以把数据再保存进SQL Server或XML中。我们只需要简单地使用OLE DB Provider 来访问Excel文件,然后返回DataSet即可。
下面是要显示的Excel数据contact.xls:


姓名 性别 地址
net_lover Male amxh@21cn.com
amxh Male amxh@21cn.com
孟子 E 章 Male amxh@21cn.com


只需要指定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>

zhuyj441 2003-10-09
  • 打赏
  • 举报
回复
只是把Excel文档原封不动的显示的程序的窗体中。
aawolf 2003-10-09
  • 打赏
  • 举报
回复
不知道你是想操作Excel还是Excel数据,如果是前者,你可以看下

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp09182003.asp
foxflying 2003-10-09
  • 打赏
  • 举报
回复
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;
}
foxflying 2003-10-09
  • 打赏
  • 举报
回复
定义一个dataset,从excel文件里面读出数据来,然后就可以直接把dataset绑定到datagrid里面显示了。当然,若是你知道excel的内容,那么可以操纵dataset显示你想要的数据而不是全部数据。
zhuyj441 2003-10-09
  • 打赏
  • 举报
回复
对 wangj2001(乡村酒吧) 发的代码,我不是很懂.
请问,代码中的Word关键字(引用?)从何而来?
zhuyj441 2003-10-09
  • 打赏
  • 举报
回复
不好意思。
虽然大家说了这么多。
可是我还是搞不定。

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧