문제

help me I dint know how to combine this code.Hope anyone can help me. I using c# in asp.net and try to convert .doc and .docx to html to view in webpage.

This is my code:

public bool WriteViewRow(DataRowView drv)
    {
        string strFileLink = null;
        string strFileName = Convert.ToString(drv["Name"]);
        string strFilePath = WebPathCombine(WebPath(), strFileName);
        bool blnFolder = IsDirectory(drv);

        if (blnFolder)
        {
            if (!string.IsNullOrEmpty(_strHideFolderPattern) && Regex.IsMatch(strFileName, _strHideFolderPattern, RegexOptions.IgnoreCase))
            {
                return false;
            }
            strFileLink = PageUrl(strFilePath) + strFileName + "</A>";
        }
        else
        {
            if (!string.IsNullOrEmpty(_strHideFilePattern) && Regex.IsMatch(strFileName, _strHideFilePattern, RegexOptions.IgnoreCase))
            {
                return false;
            }
              strFileLink = "<A href=\"" + strFilePath + "\" target = \"iframe01\">" + strFileName + "</A>";         //link to open the file
        }

And I want to use this code to join in my code, I don't want to upload the file but want to use the link in code above to integrate in this code:

//To check the file extension if it is word document or something else
string strFileName = fUpload.FileName;
string[] strSep = fUpload.FileName.Split('.');
int arrLength = strSep.Length - 1;
string strExt = strSep[arrLength].ToString().ToUpper(); //Save the uploaded file to the folder
strPathToUpload = Server.MapPath("Datadir");  //Map-path to the folder where html to be saved
strPathToConvert = Server.MapPath("WordToHtml");
object FileName = strPathToUpload + "\\" + fUpload.FileName;
object FileToSave = strPathToConvert + "\\" + fUpload.FileName + ".htm";

if (strExt.ToUpper() == "DOCX" || strExt.ToUpper() == "DOC" )
{

    fUpload.SaveAs(strPathToUpload + "\\" + fUpload.FileName);
    lblMessage.Text = "File uploaded successfully";
    //open the file internally in word. In the method all the parameters should be passed by object reference

    objWord.Documents.Open(ref FileName, ref readOnly, ref missing, ref missing, ref missing, ref missing,
    ref missing, ref  missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing,
    ref missing, ref missing);

    //Do the background activity
    objWord.Visible = false;
    Microsoft.Office.Interop.Word.Document oDoc = objWord.ActiveDocument;

    oDoc.SaveAs(ref FileToSave, ref fltDocFormat, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

    lblMessage.Text = fUpload.FileName + " converted to HTML successfully";
    docPreview.Attributes["src"] = "../WordToHtml/" + fUpload.FileName + ".htm";

}

Anyone have some advice?Actually I want to develop some webpage like webmanager and user be able to upload,delete,view,edit the file.. It's all have done if file .txt..but I fail to convert this.

올바른 솔루션이 없습니다

다른 팁

The code you want to use is using word automation. This is bad practice to do on a server environment (like yours asp.net) because of the many issues and it's strongly discouraged. Read why here.

Instead, look for a tool that will do that job for you, something like this answer. In most cases the tools don't give you a verbatim look of the document in html, but this depends on the structure and simplicity of the document.

hope, this code helpful for u.and code use Microsoft.Office.Interop.Word class

    public static string ReadWordFile(string strFilePath, Extension objExtension)
    {
        string strFileContent = string.Empty;
        try
        {
            if (objExtension == Extension.WebPage)
            {
                try
                {
                    Open(strFilePath);
                    strFileContent = ClsCommon.HTMLBody(ClsCommon.ReadFile(SaveAs(strFilePath, HtmExtension, WdSaveFormat.wdFormatFilteredHTML), true));
                }
                catch
                {
                }
            }
        }
        catch
        {
        }
        return strFileContent;
    }

      private static string SaveAs(string FilePath, string strFileExtension,    WdSaveFormat objSaveFormat)
    {
        try
        {
            FilePath = System.IO.Path.ChangeExtension(FilePath, strFileExtension);
            doc.SaveAs(FilePath, objSaveFormat, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing, objMissing);
        }
        catch
        {

        }
        finally
        {
            Close();
        }
        return FilePath;
    }

    public static string HTMLBody(string strHTML)
    {
        strHTML = ClearHTMLContent(strHTML);
        if (strHTML.ToLower().IndexOf("<body") > 0 && strHTML.ToLower().IndexOf("</body>") > 0)
        {
            strHTML = strHTML.Substring(strHTML.ToLower().IndexOf("<body") + 5, strHTML.ToLower().IndexOf("</body>") - (strHTML.ToLower().IndexOf("<body") + 5));
            strHTML = strHTML.Substring(strHTML.IndexOf(">") + 1, strHTML.Length - (strHTML.IndexOf(">") + 1));
        }
        return strHTML;
    }

    public static string ClearHTMLContent(string Str)
    {
        if (Str.ToLower().IndexOf("<base") > 0)
        {
            Str = Str.Replace(Str.Substring(Str.ToLower().IndexOf("<base"), Str.Substring(Str.ToLower().IndexOf("<base")).IndexOf(">") + 1), "");
        }
        return Str.Replace("Â", "").Replace("�", "");
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top