Fckeditor 怎么添加导入word文档的功能???急急急。。。

shaoboyy 2009-10-20 09:10:57
做的东西里要有一个在fckeditor里导入文档的功能。

在这里我们要实现直接导入word文档,如果其中包含图片,则自动分离图片并上传到服务器后在fckeditor中显示.

搞了2天,网上有和ASP.NET的解决办法,

java的一点头绪都没有,

参考这个http://www.javaeye.com/topic/150487 还是搞不出来。

望高手解决一下呀。。。

不胜感激。。。。。。。。。。。。

急急急。。。。。。。。。



我的邮箱343269876@qq.com
...全文
3180 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
xwork1111 2010-10-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ghl2000ok 的回复:]
using System;
using System.Web;
using System.Web.UI.HtmlControls;
using Microsoft.Office.Interop.Word;
using System.IO;

public partial class ImportWord : System.Web.UI.Page
{
//Html文件名
……
[/Quote]
您好!这好像是.net的吧,有java的吗?
zerolost0413 2010-08-23
  • 打赏
  • 举报
回复
学习了!
liu13718696698 2010-05-25
  • 打赏
  • 举报
回复
检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80040154。
ghl2000ok 2009-11-25
  • 打赏
  • 举报
回复
把Microsoft.Office.Interop.Word.dll文件复制到你工程项目的bin目录。

在fckconfig.js中加载组件。

在fckeditor的fckconfig.js中加入如下内容

FCKConfig.Plugins.Add( 'ImportWord' , 'zh-cn') ; //加载导入Word组件

在工具栏中添加导入word按钮。

fckconfig.js中:
找到这行中 FCKConfig.ToolbarSets["Default"] = [...

'Paste','PasteText','PasteWord',在PasteWord后插入'ImportWord'.
ghl2000ok 2009-11-25
  • 打赏
  • 举报
回复
using System;
using System.Web;
using System.Web.UI.HtmlControls;
using Microsoft.Office.Interop.Word;
using System.IO;

public partial class ImportWord : System.Web.UI.Page
{
//Html文件名
private string _htmlFileName;

protected void Page_Load(object sender, EventArgs e)
{

}

/// <summary>
/// 上传Word文档
/// </summary>
/// <param name="inputFile"></param>
private string UpLoadFile(HtmlInputFile inputFile)
{
//建立上传对象
HttpPostedFile postedFile = inputFile.PostedFile;

string fileName = Path.GetFileName(postedFile.FileName);
string fileExtension = Path.GetFileNameWithoutExtension(fileName);

//上传图片保存Base路径 如果修改此路径需要同时修改图片地址替换路径(在212行附件)
string phyPath = Server.MapPath("~/") + "FckUpload\\ImportWord\\0\\";

//判断路径是否存在,若不存在则创建路径
DirectoryInfo upDir = new DirectoryInfo(phyPath);
if (!upDir.Exists)
{
upDir.Create();
}

//判断文件是否存在,存在则提示文件存在并退出
DirectoryInfo upPFName = new DirectoryInfo(phyPath + fileExtension + ".files\\");
if (upPFName.Exists)
{
return "false";
}
//保存文件
try
{
postedFile.SaveAs(phyPath + fileName);
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
return phyPath + fileName;
}

/// <summary>
/// word转成html
/// </summary>
/// <param name="wordFileName"></param>
private static string WordToHtml(object wordFileName)
{
//在此处放置用户代码以初始化页面
ApplicationClass word = new ApplicationClass();
Type wordType = word.GetType();
Documents docs = word.Documents;

//打开文件
Type docsType = docs.GetType();
Document doc = (Document)docsType.InvokeMember("Open",
System.Reflection.BindingFlags.InvokeMethod, null, docs, new[] { wordFileName, true, true });

//转换格式,另存为
Type docType = doc.GetType();

string wordSaveFileName = wordFileName.ToString();
string strSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.Length - 3) + "html";
object saveFileName = strSaveFileName;
//下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成如下
/*
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[]{saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML});
*/
///其它格式:
///wdFormatHTML
///wdFormatDocument
///wdFormatDOSText
///wdFormatDOSTextLineBreaks
///wdFormatEncodedText
///wdFormatRTF
///wdFormatTemplate
///wdFormatText
///wdFormatTextLineBreaks
///wdFormatUnicodeText
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new[] { saveFileName, WdSaveFormat.wdFormatHTML });

docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
null, doc, null);

//退出 Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod,
null, word, null);

return saveFileName.ToString();
}

/// <summary>
/// 上传并处理word文档
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnUpload_Click(object sender, EventArgs e)
{
string strFilename = UpLoadFile(fileWord);
if (strFilename == "false")
{
Page.ClientScript.RegisterStartupScript(Page.ClientScript.GetType(), "ok ", "alert('word文档已经存在,请换文件名上传') ", true);
return;
}
_htmlFileName = WordToHtml(strFilename);

FindUsedFromHtml(GetHtml(_htmlFileName),strFilename);

//删除文件
File.Delete(_htmlFileName);
File.Delete(strFilename);
}

/// <summary>
/// 读取html文件,返回字符串
/// </summary>
/// <param name="strHtmlFileName"></param>
/// <returns></returns>
private static string GetHtml(string strHtmlFileName)
{
System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("gb2312");

StreamReader sr = new StreamReader(strHtmlFileName, encoding);

string str = sr.ReadToEnd();

sr.Close();

return str;
}
/// <summary>
///
/// </summary>
/// <param name="strHtml"></param>
/// <param name="strFileName"></param>
/// <returns></returns>
private void FindUsedFromHtml(string strHtml, string strFileName)
{
// stytle 部分
int index = 0;
int intStyleStart = 0;
int intStyleEnd = 0;

while (index < strHtml.Length)
{
int intStyleStartTmp = strHtml.IndexOf("<style>", index);
if (intStyleStartTmp == -1)
{
break;
}
int intContentStart = strHtml.IndexOf("<!--", intStyleStartTmp);
if (intContentStart - intStyleStartTmp == 9)
{
intStyleStart = intStyleStartTmp;
break;
}
index = intStyleStartTmp + 7;
}

index = 0;
while (index < strHtml.Length)
{
int intContentEndTmp = strHtml.IndexOf("-->", index);
if (intContentEndTmp == -1)
{
break;
}
int intStyleEndTmp = strHtml.IndexOf("</style>", intContentEndTmp);
if (intStyleEndTmp - intContentEndTmp == 5)
{
intStyleEnd = intStyleEndTmp;
break;
}
index = intContentEndTmp + 4;
}

string strStyle = strHtml.Substring(intStyleStart, intStyleEnd - intStyleStart + 8);

// Body部分
int bodyStart = strHtml.IndexOf("<body");
int bodyEnd = strHtml.IndexOf("</body>");

string strBody = strHtml.Substring(bodyStart, bodyEnd - bodyStart + 7);

//替换图片地址
string fullName = strFileName.Substring(strFileName.LastIndexOf("\\") + 1);
string fullExtension = Path.GetExtension(fullName);
string strOld = string.Empty;
switch (fullExtension)
{
case ".docx":
strOld = fullName.Replace("docx", "files").Replace(" ", "%20");
break;
case ".doc":
strOld = fullName.Replace("doc", "files").Replace(" ", "%20");
break;
case ".DOCX":
strOld = fullName.Replace("DOCX", "files").Replace(" ", "%20");
break;
case ".DOC":
strOld = fullName.Replace("DOC", "files").Replace(" ", "%20");
break;
}
string strNew = Page.Request.ApplicationPath + "/FckUpload/ImportWord/0/" + strOld;

strBody = strBody.Replace(strOld, strNew);
strBody = strBody.Replace("v:imagedata", "img");
strBody = strBody.Replace("</v:imagedata>", "");

//Sgxcn临时调试用-但不可去掉
TextArea1.InnerText = strStyle;
Textarea2.InnerText = strBody;

//更改信息
pnlOk.Visible = true;
pnlUpload.Visible = false;
}
}
ghl2000ok 2009-11-25
  • 打赏
  • 举报
回复
在fckeditor-editor-plugins下添加ImportWord文件夹
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ImportWord.aspx.cs" Inherits="ImportWord" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>

<script src="../../dialog/common/fck_dialog_common.js" type="text/javascript"></script>

<script type="text/javascript">

var oEditor = window.parent.InnerDialogLoaded() ;

// Gets the document DOM
var oDOM = oEditor.FCK.EditorDocument ;

var ImportOk = false;

// Fired when the window loading process is finished. It sets the fields with the
// actual values if a table is selected in the editor.
window.onload = function()
{
//oEditor.FCKLanguageManager.TranslatePage(document);

window.parent.SetOkButton( true ) ;
window.parent.SetAutoSize( true ) ;
}

// Fired when the user press the OK button
function Ok()
{
if(!ImportOk)
{
if(document.getElementById("fileWord").value == "")
{
alert("请先选择Word文件。");
return false;
}
else
{
alert("请单击“开始导入”按钮。");
return false;
}
}

var oActiveEl = oEditor.FCK.EditorDocument.createElement( 'SPAN' ) ;
oActiveEl.innerHTML = document.getElementById('TextArea1').value + document.getElementById('TextArea2').value;
oEditor.FCKUndo.SaveUndoStep() ;
oActiveEl = oEditor.FCK.InsertElement( oActiveEl ) ;

return true;
}
</script>

</head>
<body>
<form id="form1" method="post" runat="server">
<asp:Panel ID="pnlUpload" runat="server">
请选择您要导入的word文件:
<div style="margin:10px">
<input id="fileWord" runat="server" type="file" style="width: 300px"/>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="fileWord"
Display="Dynamic" ErrorMessage="您还没有选择要上传的Word文档。" SetFocusOnError="True"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="fileWord"
Display="Dynamic" ErrorMessage="您选择的文件不是.doc(Word)文档。" SetFocusOnError="True"

ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.doc|.DOC|.docx|.DOCX)$"></asp:RegularExpressionValidator><br />
</div>

<asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="开始导入" />
</asp:Panel>
<asp:Panel ID="pnlOk" runat="server" Visible="False">
Word文件已经导入成功,单击“确定”导入到编辑器中,单击“取消”将放弃导入操作。
<script type="text/javascript">ImportOk = true;</script>
</asp:Panel>

<div style="display:none">
<textarea id="TextArea1" style="width: 648px; height: 220px" runat="server"></textarea><br />
<textarea id="Textarea2" style="width: 648px; height: 201px" runat="server"></textarea>
</div>

</form>
</body>
</html>
shaoboyy 2009-11-22
  • 打赏
  • 举报
回复
无功而返······
guodongbingtuan 2009-10-21
  • 打赏
  • 举报
回复
也在弄这个,郁闷中........
出来告一声..

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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