我有.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,它不轉換,當我連接調試它dataObj
是Nothing
碼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
我希望我清楚我的查詢
一串什麼?你想要一個文本內的文本字符串? – R0MANARMY
我想將.doc字節數組轉換爲.doc文件的字符串平均文本 – vikas
.doc文件格式是一種專有的二進制格式,不能輕易將其內容轉換爲字符串。 – R0MANARMY