winform中嵌入WORD或EXCEL,谁有例程?(至少100分)
winform中嵌入WORD或EXCEL,谁有例程?(至少100分) --------------------编程问答-------------------- Visual C# 编程操作Excelhttp://soft.yesky.com/SoftChannel/72342380468109312/20040819/1844435.shtml
详细看
http://topic.csdn.net/t/20040831/14/3325945.html# --------------------编程问答-------------------- 多谢 --------------------编程问答-------------------- 不过,说的方法不好用 --------------------编程问答-------------------- 楼主准备在WinForm里嵌入Word和Excel吗
可以在COM里添加Microsoft Web Browser 组件
现在也在研究中
在该控件中可以打开PPT,Word,Excel,Txt,网页,Project和Visio文档
目前还只可以通过文件的全路径打开 --------------------编程问答-------------------- C#如何在Form中嵌入并且操作Excel表格
http://blog.csdn.net/ttfleet/archive/2004/10/25/151041.aspx
但嵌入后不能直接操作。
比如我从treeview 里拖个字段过去。excle不肯认。汗... --------------------编程问答-------------------- http://dotnet.aspx.cc/article/4eb79f05-b9a4-4e8a-836f-864393f40405/read.aspx --------------------编程问答-------------------- 多谢各位.chenguangxi() ,我用了Web Browser ,可是工具栏怎么出不来,也没有打印的菜单, 如何解决? --------------------编程问答-------------------- blogs.msdn.com/erikaehrli/archive/2005/08/26/hostofficeinnet.aspx --------------------编程问答-------------------- 你可以点击右键菜单选择常用工具
在这个控件里嵌入Office文档比较容易
可是对Office文档的操作就有些麻烦
还有一个问题就是在嵌入要打开的Office文档是会有一个下载的提示框
不知道楼主解决没,解决了告诉下 --------------------编程问答-------------------- 正如jiangsheng(蒋晟.Net[MVP]) 所说
下载 Microsoft Developer Support Office Framer Control就可以解决问题了
http://www.microsoft.com/downloads/details.aspx?familyid=CE2CA4FD-2169-4FAC-82AF-770AA9B60D77&displaylang=en
--------------------编程问答-------------------- IE所有的工具按钮Web Browser都有方法webBrowser1.Refresh();webBrowser1.Stop();webBrowser1.GoForward();webBrowser1.GoBack(); --------------------编程问答-------------------- http://msdn2.microsoft.com/zh-cn/library/w1702h4a(VS.80).aspx
有一些表格操作的说明
--------------------编程问答-------------------- 直接就可以在WinForm程序中打开Word,也就是把Word嵌入到WinForm程序当中.
你到下面这个地址去下如个Demo程序.我原来做过.还可以用它控制Word编程.
http://www.codeproject.com/cs/miscctrl/winwordcontrol.asp --------------------编程问答-------------------- 以下是vb.net代易做图
樓主改一下就ok
Public Sub DtToXls(ByVal Table As DataTable, ByVal DefFileName As String)
Dim MyOleDbCn As New System.Data.OleDb.OleDbConnection
Dim MyOleDbCmd As New System.Data.OleDb.OleDbCommand
Dim MyTable As New DataTable
Dim intRowsCnt, intColsCnt As Integer
Dim strSql As String, strFlName As String
Dim Fso As New System.Object
If Table Is Nothing Then
MessageBox.Show("未取得數據,無法導出", "導出錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
MyTable = Table
If MyTable.Rows.Count = 0 Then
MessageBox.Show("未取得數據,無法導出", "導出錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If
Dim FileName As String
Dim SaveFileDialog As New SaveFileDialog
SaveFileDialog.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
SaveFileDialog.Title = "保存為"
SaveFileDialog.Filter = ".xls|*.xls"
SaveFileDialog.FileName = DefFileName
If (SaveFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK) Then
FileName = SaveFileDialog.FileName
' TODO: 在此加入開啟檔案的程式易做图。
End If
If FileName = "" Then Exit Sub
strFlName = FileName
If Dir(FileName) <> "" Then
Kill(FileName)
End If
Try
Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
MyOleDbCn.ConnectionString = "Provider=Microsoft.Jet.OleDb.4.0;" & _
"Data Source=" & strFlName & ";" & _
"Extended ProPerties=""Excel 8.0;HDR=Yes;"""
MyOleDbCn.Open()
MyOleDbCmd.Connection = MyOleDbCn
MyOleDbCmd.CommandType = CommandType.Text
'第一行插入列标题
strSql = "CREATE TABLE " & DefFileName & "("
For intColsCnt = 0 To MyTable.Columns.Count - 1
If intColsCnt <> MyTable.Columns.Count - 1 Then
strSql = strSql & ChangeChar(MyTable.Columns(intColsCnt).Caption) & " text,"
Else
strSql = strSql & ChangeChar(MyTable.Columns(intColsCnt).Caption) & " text)"
End If
Next
MyOleDbCmd.CommandText = strSql
MyOleDbCmd.ExecuteNonQuery()
'插入各行
For intRowsCnt = 0 To MyTable.Rows.Count - 1
strSql = "INSERT INTO " & DefFileName & " VALUES('"
For intColsCnt = 0 To MyTable.Columns.Count - 1
If intColsCnt <> MyTable.Columns.Count - 1 Then
strSql = strSql & ChangeChar(MyTable.Rows(intRowsCnt).Item(intColsCnt)) & "','"
Else
strSql = strSql & ChangeChar(MyTable.Rows(intRowsCnt).Item(intColsCnt)) & "')"
End If
Next
MyOleDbCmd.CommandText = strSql
MyOleDbCmd.ExecuteNonQuery()
Next
MessageBox.Show("数据已经成功导入EXCEL文件" & strFlName, "数据导出", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ErrCode As Exception
MsgBox("错误信息:" & ErrCode.Message & vbCrLf & vbCrLf & _
"引发事件:" & ErrCode.TargetSite.ToString, MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "错误来源:" & ErrCode.Source)
Exit Sub
Finally
MyOleDbCmd.Dispose()
MyOleDbCn.Close()
MyOleDbCn.Dispose()
'Me.Cursor.Current = System.Windows.Forms.Cursors.Default
End Try
End Sub
Public Function ChangeChar(ByVal Sqlchar) As String
If Convert.IsDBNull(Sqlchar) Then
ChangeChar = " "
Exit Function
End If
Dim tStr As String
tStr = Replace(Sqlchar, "'", Chr(39) + Chr(39))
tStr = Replace(tStr, "|", "_")
ChangeChar = tStr
End Function --------------------编程问答-------------------- 楼主的问题解决了吗?解决了一定要公布出来啊! --------------------编程问答-------------------- http://bbs.msproject.cn/default.aspx?g=posts&t=292 --------------------编程问答-------------------- 用Web Browser可以轻松搞定 --------------------编程问答-------------------- up --------------------编程问答-------------------- up~ --------------------编程问答-------------------- 我就用Web Browser的 --------------------编程问答-------------------- 关注 --------------------编程问答-------------------- 这是我们项目中的代码,WORD和EXCEL有一定区别,不过是用Web Browser实现的
-------word--------------------
private void InitWeb()
{
if (webBrowser1 == null)
{
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.webBrowser1.AllowWebBrowserDrop = false;
this.webBrowser1.IsWebBrowserContextMenuEnabled = false;
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser_" + this.Name;
this.webBrowser1.WebBrowserShortcutsEnabled = false;
this.webBrowser1.Location = new System.Drawing.Point(0, 0);
this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
fm.fm.Controls.Add(webBrowser1);
}
this.webBrowser1.Size = new System.Drawing.Size(Rect.Width + 20, Rect.Height + 20);
}
public void InitPage(bool bFlag)
{
InitWeb();
if (!File.Exists(TempHttp))
{
bFlag = true;
}
if (bFlag && File.Exists(FilePath))
{
#region 保存网页副本
Word.ApplicationClass word = new Word.ApplicationClass();
object missingObj = Type.Missing;
object readOnly = false;
object file = FilePath;
object show = true;
Word.Document doc = word.Documents.Open(ref file,
ref missingObj,
ref readOnly,
ref missingObj,
ref missingObj,
ref missingObj,
ref missingObj,
ref missingObj,
ref missingObj,
ref missingObj,
ref missingObj,
ref show,
ref missingObj,
ref missingObj,
ref missingObj,
ref missingObj);
FilePath = FilePath.ToLower();
TempHttp = FilePath.Replace(".doc", "_word.htm");
object tmpFile = TempHttp;
object saveType = Word.WdSaveFormat.wdFormatHTML;
doc.SaveAs(ref tmpFile,
ref saveType,
ref missingObj,
ref missingObj,
ref missingObj,
ref missingObj,
ref missingObj,
ref missingObj,
ref missingObj,
ref missingObj,
ref missingObj,
ref missingObj,
ref missingObj,
ref missingObj,
ref missingObj,
ref missingObj);
object notSaveChange = false;
word.Quit(ref notSaveChange, ref missingObj, ref missingObj);
#endregion
LoadFlag = false;
}
if (LoadFlag == false)
{
webBrowser1.Navigate((string)TempHttp);
LoadFlag = true;
}
}
----------------------excel-------------
private void InitWeb()
{
if (webBrowser1 == null)
{
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.webBrowser1.AllowWebBrowserDrop = false;
this.webBrowser1.IsWebBrowserContextMenuEnabled = false;
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser_" + this.Name;
this.webBrowser1.WebBrowserShortcutsEnabled = false;
this.webBrowser1.Location = new System.Drawing.Point(0, 0);
this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
fm.fm.Controls.Add(webBrowser1);
}
this.webBrowser1.Size = new System.Drawing.Size(Rect.Width + 20, Rect.Height + 20);
}
public void InitPage(bool bFlag)
{
InitWeb();
if (!File.Exists(TempHttp))
{
bFlag = true;
}
if (bFlag && File.Exists(FilePath))
{
#region 保存网页副本
Excel.ApplicationClass execl = new Excel.ApplicationClass();
object missingObj = Type.Missing;
object readOnly = false;
string file = FilePath;
object show = true;
Excel.Workbook doc = execl.Workbooks.Open(file,
missingObj,
readOnly,
missingObj,
missingObj,
missingObj,
missingObj,
missingObj,
missingObj,
missingObj,
missingObj,
missingObj,
missingObj,
missingObj,
missingObj);
FilePath = FilePath.ToLower();
TempHttp = FilePath.Replace(".xls", "_excel.htm");
try
{
if (File.Exists(TempHttp))
{
File.Delete(TempHttp);
}
}
catch { }
object tmpFile = TempHttp;
object saveType = Excel.XlFileFormat.xlHtml;
doc.SaveAs(tmpFile,
saveType,
missingObj,
missingObj,
missingObj,
missingObj,
Excel.XlSaveAsAccessMode.xlNoChange,
missingObj,
missingObj,
missingObj,
missingObj,
missingObj);
object notSaveChange = false;
execl.Quit();
#endregion
LoadFlag = false;
}
if (LoadFlag == false)
{
webBrowser1.Navigate((string)TempHttp);
LoadFlag = true;
}
} --------------------编程问答-------------------- mark --------------------编程问答-------------------- mark
--------------------编程问答-------------------- 关注 --------------------编程问答-------------------- 我有例子,代码没几行.要的话到我这下载.带说明的 --------------------编程问答-------------------- 方案:
1.如果说,想编辑Excel文件,并且又像在可视化操作下编辑,就类型用着Excel感觉的话,最好用第三方控件:FarPoint.Win.Spread.FpSpread(就叫:FpSpread控件)
2.如果说,只是想编辑,处理Excel文件在程序后台,看不到操作,的话,最好使用:NPOI .
3.条件同上,也可以使用Office Excel 的DLL类库,不过不太建使用这个。
本人目前较熟悉FpSpread,NPOI还一般般,了解中。 --------------------编程问答-------------------- market --------------------编程问答-------------------- market 求解 --------------------编程问答-------------------- 用微软的控件DSOFramer,最重要的免费开源,效果还不错。
补充:.NET技术 , C#