.doc、.txt、.docx のバイト データがあり、それを文字列に変換したいのですが、次のことを行いましたが、正確な結果が得られませんでした:
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
だから今問題は次のコードでそれを変換するときです:引数を見て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
私は私の質問をクリアしたいと思います