ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 253|回复: 4

[求助] vb.net中打开文本文件,读取的汉字是乱码

[复制链接]

TA的精华主题

TA的得分主题

发表于 2023-2-17 23:42 | 显示全部楼层 |阅读模式
本帖最后由 prcstone 于 2023-2-17 23:47 编辑

vb.net中用“     File.ReadAllLines(fileName, Encoding.Default)    ”打开文本文件(文本文件的原编码为ANSI),读取的汉字是乱码,而且程序结束后,文本文件再打开就成UTF-8格式了。

有什么办法?正确读取,谢谢。



TA的精华主题

TA的得分主题

 楼主| 发表于 2023-2-18 10:35 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
解决办法好像只能被动地先把文本文件保存成UTF-8格式

TA的精华主题

TA的得分主题

发表于 2023-3-6 01:18 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
理论得逻辑应该是 先确认文档编码,然后在用指定编码打开把

TA的精华主题

TA的得分主题

发表于 2023-3-6 01:21 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
或者:

安装 nuget:
System.Text.Encoding.CodePages

  1. Encoding.RegisterProvider(CodePagesEncodingProvider.Instance)

  2. Dim sw As StreamReader = New StreamReader(fpath , encoding:=Encoding.GetEncoding("GB2312"))
复制代码

TA的精华主题

TA的得分主题

发表于 2023-4-17 23:10 | 显示全部楼层
#Region "BytesFormatText"
    ''' <summary>
    ''' BytesFormatText
    ''' </summary>
    ''' <param name="Bytes"></param>
    ''' <returns>String</returns>
    <Runtime.CompilerServices.Extension>
    Private Function BytesFormatText(Bytes() As Byte) As String
        Dim len As Integer = Bytes.Length
        If len >= 3 AndAlso Bytes(0) = &HEF AndAlso Bytes(1) = &HBB AndAlso Bytes(2) = &HBF Then Return Encoding.UTF8.GetString(Bytes, 3, len - 3)
        Dim cs() As Integer = {7, 5, 4, 3, 2, 1, 0, 6, 14, 30, 62, 126}
        For i As Integer = 0 To len - 1
            Dim bits As Integer = -1
            For j As Integer = 0 To 5
                If CInt(Bytes(i)) >> cs(j) = cs(j + 6) Then
                    bits = j
                    Exit For
                End If
            Next
            If bits = -1 Then Return Encoding.Default.GetString(Bytes)
            Do While bits > 0
                bits -= 1
                i += 1
                If i = len OrElse CInt(Bytes(i)) >> 6 <> 2 Then Return Encoding.Default.GetString(Bytes)
            Loop
        Next
        Return Encoding.UTF8.GetString(Bytes)
    End Function
#End Region
#Region "读取文件流"
    ''' <summary>
    ''' 读取文件流
    ''' </summary>
    ''' <param name="path">文件地址</param>
    ''' <returns>返回Byte文件流</returns>
    <Runtime.CompilerServices.Extension>
    Public Function ReadFile(path As String) As String
        Using FileStream As New FileStream(path, FileMode.Open, FileAccess.Read)
            Try
                With FileStream
                    Dim Buffur(.Length - 1) As Byte
                    .Read(Buffur, 0, .Length)
                    .Seek(0, SeekOrigin.Begin)
                    Return Buffur.BytesFormatText
                End With
            Catch ex As IOException
                Throw New IOException(ex.Message)
                Return Nothing
            End Try
        End Using
    End Function
#End Region
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-5-2 09:03 , Processed in 0.036745 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表