2013-02-02 69 views
-2

我有.DOC,.TXT,.DOCX的字節的數據,我想將其轉換爲字符串,我下面的事情,但沒有得到確切的結果:轉換字節數組字符串在VB.net

Public ByteData As Byte() = // my data 
Dim str As String = String.Empty 

str = System.Text.Encoding.UTF8.GetString(objCandidateInfo.ByteData, 0, objCandidateInfo.ByteData.Length) 
str = Convert.ToBase64String(objCandidateInfo.ByteData) 

編輯

所以現在我將同樣使用Word應用程序,該代碼工作 這是我的代碼

Private Shared ObjwordApp As Word.Application 
    Private Shared nullobj As Object = System.Reflection.Missing.Value 
    Private Shared doc As Word.Document 
    Shared Sub New() 
     ObjwordApp = New Word.Application() 
    End Sub 

    Public Shared Sub InitializeClass() 
     ObjwordApp.Visible = False 
    End Sub 

    Private Shared Sub OpenWordFile(ByVal StrFilePath As Object) 
     Try 
      ObjwordApp.Visible = False 
     Catch ex As Exception 
      ObjwordApp = New Word.Application() 
     End Try 
     Try 
      doc = ObjwordApp.Documents.Open(StrFilePath, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj) 
     Catch ex As Exception 
      CloseWordFile() 
      ObjwordApp.Visible = False 
     End Try 
    End Sub 

    Private Shared Sub CopyWordContent() 
     Try 
      doc.ActiveWindow.Selection.WholeStory() 
      doc.ActiveWindow.Selection.Copy() 
     Catch ex As Exception 
      Clipboard.Clear() 
     End Try 
    End Sub 

    Private Shared Sub CloseWordFile() 
     Try 
      doc.Close() 
     Catch ex As Exception 

     End Try 
    End Sub 

    Public Shared Function ReadWordFile(ByVal StrFilePath As String, ByVal StrDataFormat As String) As String 
     Dim StrFileContent = String.Empty 
     If (File.Exists(StrFilePath)) Then 
      Try 
       OpenWordFile(StrFilePath) 
       CopyWordContent() 
      Catch ex As Exception 

      Finally 
       CloseWordFile() 
      End Try 

      Try 
       Dim dataObj As IDataObject = Clipboard.GetDataObject() 
       If (dataObj.GetDataPresent(StrDataFormat)) Then 
        StrFileContent = dataObj.GetData(StrDataFormat) 
       Else 
        StrFileContent = "" 
       End If 
       Clipboard.Clear() 
      Catch ex As Exception 

      End Try 
     End If 
     Return StrFileContent 
    End Function 

當我保存字節數組DB,我撥打以下功能,將其轉換爲RTF,它不轉換,當我連接調試它dataObjNothing

碼1

Dim str As String = String.Empty 
       Try 
        'str = System.Text.Encoding.UTF8.GetString(objCandidateInfo.ByteData, 0, objCandidateInfo.ByteData.Length) 
        'str = Convert.ToBase64String(objCandidateInfo.ByteData) 
        'str = System.Text.Encoding.ASCII.GetString(objCandidateInfo.ByteData, 0, objCandidateInfo.ByteData.Length) 
        str = ClsDocumentManager.ReadContent(objCandidateInfo.ByteData, DataFormats.Rtf) 
       Catch ex As Exception 

       End Try 

我保存數據DB兩個字節和文本格式,所以當我把它從DB(字節的值,我保存並將其轉換爲RTF),其工作代碼

代碼2

rtbAttachment.Rtf = ClsDocumentManager.ReadContent(byteAttachment, DataFormats.Rtf) 

這些都是ClsDocumentManager

Public Shared Function GetRandomNo() As Integer 
     Dim RandomNo As New Random() 
     Return RandomNo.Next(Convert.ToInt32(DateTime.Now().Minute.ToString() & DateTime.Now().Second.ToString() & DateTime.Now().Hour.ToString())) 
    End Function 

    Public Shared Function ReadContent(ByVal byteArray As Byte(), ByVal StrReadFormat As String) As String 
     Dim StrFileContent As String = String.Empty 
     Try 
      If (Not IsNothing(byteArray)) Then 
       Dim StrFileName As String = GetRandomNo().ToString() & ".doc" 
       StrFileName = ClsSingleton.aTempFolderName & StrFileName 
       If (CreateWordFile(byteArray, StrFileName)) Then 
        StrFileContent = ClsWordManager.ReadWordFile(StrFileName, StrReadFormat) 
        If (File.Exists(StrFileName)) Then 
         File.Delete(StrFileName) 
        End If 
       End If 
      End If 
     Catch ex As Exception 

     End Try 
     Return StrFileContent 
    End Function 

Public Shared Function CreateWordFile(ByVal byteArray As Byte(), ByVal StrFileName As String) As Boolean 
     Dim boolResult As Boolean = False 
     Try 
      If (Not IsNothing(byteArray)) Then 
       If (Not File.Exists(StrFileName)) Then 
        Dim objFileStream As New FileStream(StrFileName, FileMode.Create, FileAccess.Write) 
        objFileStream.Write(byteArray, 0, byteArray.Length) 
        objFileStream.Close() 
        boolResult = True 
       End If 
      End If 
     Catch ex As Exception 
      boolResult = False 
     End Try 
     Return boolResult 
    End Function 

錯誤代碼的方法,同時調試

Dim dataObj As IDataObject = Clipboard.GetDataObject() 
       If (dataObj.GetDataPresent(StrDataFormat)) Then 
        StrFileContent = dataObj.GetData(StrDataFormat) 
       Else 
        StrFileContent = "" 
       End If 

`dataObj` is `Nothing` only when calling from **Code 1** 

更新

**`ClsDocumentManager`** 



Imports System.IO 

Public Class ClsDocumentManager 
    Public Shared Function GetRandomNo() As Integer 
     Dim RandomNo As New Random() 
     Return RandomNo.Next(Convert.ToInt32(DateTime.Now().Minute.ToString() & DateTime.Now().Second.ToString() & DateTime.Now().Hour.ToString())) 
    End Function 

    Public Shared Function ReadContent(ByVal byteArray As Byte(), ByVal StrReadFormat As String) As String 
     Dim StrFileContent As String = String.Empty 
     Try 
      If (Not IsNothing(byteArray)) Then 
       Dim StrFileName As String = GetRandomNo().ToString() & ".doc" 
       StrFileName = ClsSingleton.aTempFolderName & StrFileName 
       If (CreateWordFile(byteArray, StrFileName)) Then 
        StrFileContent = ClsWordManager.ReadWordFile(StrFileName, StrReadFormat) 
        If (File.Exists(StrFileName)) Then 
         File.Delete(StrFileName) 
        End If 
       End If 
      End If 
     Catch ex As Exception 

     End Try 
     Return StrFileContent 
    End Function 


    Public Shared Function CreateWordFile(ByVal byteArray As Byte(), ByVal StrFileName As String) As Boolean 
     Dim boolResult As Boolean = False 
     Try 
      If (Not IsNothing(byteArray)) Then 
       If (Not File.Exists(StrFileName)) Then 
        Dim objFileStream As New FileStream(StrFileName, FileMode.Create, FileAccess.Write) 
        objFileStream.Write(byteArray, 0, byteArray.Length) 
        objFileStream.Close() 
        boolResult = True 
       End If 
      End If 
     Catch ex As Exception 
      boolResult = False 
     End Try 
     Return boolResult 
    End Function 
End Class 

這是我

ClsWordManager
Imports System.IO 
Imports System.Text 

Public Class ClsWordManager 
    Private Shared ObjwordApp As Word.Application 
    Private Shared nullobj As Object = System.Reflection.Missing.Value 
    Private Shared doc As Word.Document 
    Shared Sub New() 
     ObjwordApp = New Word.Application() 
    End Sub 

    Public Shared Sub InitializeClass() 
     ObjwordApp.Visible = False 
    End Sub 

    Private Shared Sub OpenWordFile(ByVal StrFilePath As Object) 
     Try 
      ObjwordApp.Visible = False 
     Catch ex As Exception 
      ObjwordApp = New Word.Application() 
     End Try 
     Try 
      doc = ObjwordApp.Documents.Open(StrFilePath, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj, nullobj) 
     Catch ex As Exception 
      CloseWordFile() 
      ObjwordApp.Visible = False 
     End Try 
    End Sub 

    Private Shared Sub CopyWordContent() 
     Try 
      doc.ActiveWindow.Selection.WholeStory() 
      doc.ActiveWindow.Selection.Copy() 
     Catch ex As Exception 
      Clipboard.Clear() 
     End Try 
    End Sub 

    Private Shared Sub CloseWordFile() 
     Try 
      doc.Close() 
     Catch ex As Exception 

     End Try 
    End Sub 

    Public Shared Function ReadWordFile(ByVal StrFilePath As String, ByVal StrDataFormat As String) As String 
     Dim StrFileContent = String.Empty 
     If (File.Exists(StrFilePath)) Then 
      Try 
       OpenWordFile(StrFilePath) 
       CopyWordContent() 
      Catch ex As Exception 

      Finally 
       CloseWordFile() 
      End Try 

      Try 
       Dim dataObj As IDataObject = Clipboard.GetDataObject() 
       If (dataObj.GetDataPresent(StrDataFormat)) Then 
        StrFileContent = dataObj.GetData(StrDataFormat) 
       Else 
        StrFileContent = "" 
       End If 
       Clipboard.Clear() 
      Catch ex As Exception 

      End Try 
     End If 
     Return StrFileContent 
    End Function 



End Class 

所以,現在的問題是,當我把它轉換在下面的代碼:在arguement看ByteAttachmets,它字節轉換爲字符串

Public Function UpdateCandidateAttachment(ByVal CandidateID As Integer, ByVal ByteAttachmets As Byte(), ByVal StrExtension As String) As Integer 
     Dim Result As Integer = -1 
     Try 
      Dim objDataLayer As New ClsDataLayer() 
      Dim str As String = Nothing 
      Try 
       'str = System.Text.Encoding.UTF8.GetString(objCandidateInfo.ByteData, 0, objCandidateInfo.ByteData.Length) 
       'str = Convert.ToBase64String(objCandidateInfo.ByteData) 
       'str = System.Text.Encoding.ASCII.GetString(objCandidateInfo.ByteData, 0, objCandidateInfo.ByteData.Length) 
       str = ClsDocumentManager.ReadContent(ByteAttachmets, DataFormats.Rtf) 
      Catch ex As Exception 

      End Try 
      objDataLayer.AddParameter("@CANDIDATE_ID", CandidateID) 
      objDataLayer.AddParameter("@ATTACHMENT_DATA", ByteAttachmets) 
      objDataLayer.AddParameter("@CREATED_BY", ClsCommons.IntUserId) 
      objDataLayer.AddParameter("@EXTENSION", StrExtension) 
      Result = objDataLayer.ExecuteNonQuery("TR_PROC_UpdateCandidateAttachment") 
     Catch ex As Exception 
      MsgBox(ex.Message) 
     End Try 
     Return Result 
    End Function 

當我把它從以下代碼按屬性排列:看看objCandidateInfo.ByteData,它不起作用。

Public Function AddUpdateCandidate(ByVal objCandidateInfo As ClsCandidateInfo) As Integer 
     Dim Result As Integer = -1 
     Try 
      If (ClsCommons.IsValidEmail(objCandidateInfo.StrEmail)) Then 
       Dim str As String = Nothing 
       Try 
        'str = System.Text.Encoding.UTF8.GetString(objCandidateInfo.ByteData, 0, objCandidateInfo.ByteData.Length) 
        'str = Convert.ToBase64String(objCandidateInfo.ByteData) 
        'str = System.Text.Encoding.ASCII.GetString(objCandidateInfo.ByteData, 0, objCandidateInfo.ByteData.Length) 
        Dim byteAttachment As Byte() = objCandidateInfo.ByteData 
        str = ClsDocumentManager.ReadContent(byteAttachment, DataFormats.Rtf) 
       Catch ex As Exception 

       End Try 
       Dim objDataLayer As New ClsDataLayer() 
       objDataLayer.AddParameter("@REQUIREMENT_ID", objCandidateInfo.RequirementId) 
       objDataLayer.AddParameter("@Candidate_Name", objCandidateInfo.StrCandidateName) 
       objDataLayer.AddParameter("@Current_Organization", objCandidateInfo.StrCurrentCompany) 
       objDataLayer.AddParameter("@Current_Designation", objCandidateInfo.StrCurrentDesignation) 
       If (objCandidateInfo.StrExp.Trim() = "") Then 
        objDataLayer.AddParameter("@Overall_Exp", DBNull.Value) 
       Else 
        Dim DecExp As Decimal = -1 
        If (Decimal.TryParse(objCandidateInfo.StrExp, DecExp)) Then 
         objDataLayer.AddParameter("@Overall_Exp", DecExp) 
        Else 
         objDataLayer.AddParameter("@Overall_Exp", DBNull.Value) 
        End If 
       End If 
       objDataLayer.AddParameter("@Qualification", objCandidateInfo.StrQualification) 
       objDataLayer.AddParameter("@Location", objCandidateInfo.StrCurrentLocation) 
       objDataLayer.AddParameter("@Current_CTC", objCandidateInfo.StrCurrentCTC) 
       objDataLayer.AddParameter("@Expected_CTC", objCandidateInfo.StrExpectedCTC) 
       objDataLayer.AddParameter("@Phone_No", objCandidateInfo.StrPhoneNo) 
       objDataLayer.AddParameter("@Mobile", objCandidateInfo.StrMobile) 
       objDataLayer.AddParameter("@Notice_Period", objCandidateInfo.StrNoticePeriod) 
       objDataLayer.AddParameter("@Remarks", objCandidateInfo.StrRemarks) 
       If (objCandidateInfo.StrYearofExp.Trim() = "") Then 
        objDataLayer.AddParameter("@Years_of_Experience", DBNull.Value) 
       Else 
        Dim DecExp As Decimal = -1 
        If (Decimal.TryParse(objCandidateInfo.StrYearofExp, DecExp)) Then 
         objDataLayer.AddParameter("@Years_of_Experience", DecExp) 
        Else 
         objDataLayer.AddParameter("@Years_of_Experience", DBNull.Value) 
        End If 
       End If 
       objDataLayer.AddParameter("@Address", objCandidateInfo.StrAddress) 

       objDataLayer.AddParameter("@Email", objCandidateInfo.StrEmail) 
       If (objCandidateInfo.intIndustry > 0) Then 
        objDataLayer.AddParameter("@Industry", objCandidateInfo.intIndustry) 
       Else 
        objDataLayer.AddParameter("@Industry", DBNull.Value) 
       End If 
       If (objCandidateInfo.intFunctionalArea > 0) Then 
        objDataLayer.AddParameter("@Functional_Area", objCandidateInfo.intFunctionalArea) 
       Else 
        objDataLayer.AddParameter("@Functional_Area", DBNull.Value) 
       End If 
       If (objCandidateInfo.StrDob.Trim() = "") Then 
        objDataLayer.AddParameter("@DOB", DBNull.Value) 
       Else 
        Try 
         objDataLayer.AddParameter("@DOB", Convert.ToDateTime(objCandidateInfo.StrDob)) 
        Catch ex As Exception 
         objDataLayer.AddParameter("@DOB", DBNull.Value) 
        End Try 
       End If 
       If (objCandidateInfo.intSourceBy > 0) Then 
        objDataLayer.AddParameter("@Source", objCandidateInfo.intSourceBy) 
       Else 
        objDataLayer.AddParameter("@Source", DBNull.Value) 
       End If 
       objDataLayer.AddParameter("@SKILL_SET", objCandidateInfo.strSkillSet) 
       objDataLayer.AddParameter("@ATTACHMENT_DATA", objCandidateInfo.ByteData) 
       objDataLayer.AddParameter("@EXTENSION", objCandidateInfo.StrExtension) 
       objDataLayer.AddParameter("@CREATED_BY", ClsCommons.IntUserId) 

       Result = objDataLayer.ExecuteNonQuery("TR_PROC_AddUpdateFullCandidateData") 
      Else 
       MsgBox("Data is not extracted, Some Error Occured, please update your software.") 
      End If 
     Catch ex As Exception 
      MsgBox(ex.Message) 
     End Try 
     Return Result 
    End Function 

我希望我清楚我的查詢

+0

一串什麼?你想要一個文本內的文本字符串? – R0MANARMY

+0

我想將.doc字節數組轉換爲.doc文件的字符串平均文本 – vikas

+0

.doc文件格式是一種專有的二進制格式,不能輕易將其內容轉換爲字符串。 – R0MANARMY

回答

1

(幾經修改編輯質疑。)

如果你只是想獲得文件的文本內容,你需要處理文本文件和二進制文件不同。如果輸入文件格式是基於文本(.txt,.htm等)的文件格式,那麼您可以將其視爲字符串,儘管您仍然需要知道要使用哪種編碼。

但是,如果輸入文件格式是二進制文件(如.doc,.docx等),則不能直接將字節數組轉換爲字符串,因爲文件內容不僅僅表示文本 - 字節描述佈局,格式和有關文件的其他信息。在這種情況下,您需要使用Word或其他第三方庫來爲您處理文件數據。

要使用自動化獲取Word文檔的內容,只需創建Word.Application的實例,打開文檔,選擇其活動窗口中的所有文本,然後使用Selection.Text屬性將文本轉換爲字符串。喜歡的東西:

oDocument.ActiveWindow.Selection.WholeStory() 
sText = oDocument.ActiveWindow.Selection.Text 

Selection對象是在Word Range一個實例。這爲您提供了文檔的簡單的,未格式化的內容。您可以將其轉換爲字節數組或將其用作字符串。要將其轉換爲字節數組,您需要使用編碼,因爲內存中的字符必須轉換爲字節。

如果要將內容轉換爲RTF格式,則需要第三方工具(或自己實現RTF格式) - RTF不是純文本格式,它具有相當複雜的結構。

您還可以使用Word以RTF格式保存文檔 - 查找Document.SaveAs2()方法來執行此操作。這將文檔以RTF格式保存到磁盤。如果您需要數據庫中的這些數據,只需讀取.rtf文件(File.ReadAllBytes()),然後將字節保存到數據庫。

+0

對於延遲迴復感到抱歉,請閱讀我編輯過的問題,並請幫助我 – vikas

+0

@vikas你的ClsDocumentManager類是什麼? – xxbbcc

+0

添加ClsDocumentManager類的方法請回顧 – vikas

相關問題