c# - 在 VS 上用 C# 读写希伯来语字符串

标签 c# .net excel

我正在构建一个 Windows 窗体 C# 应用程序。我需要从 Excel 文档中读取希伯来语文本,然后做一些事情,然后将一些希伯来语文本写入 word 文档。目前我无法从 Excel 中读取希伯来语(将其打印到控制台时出现问号)。我知道这个问题与编码问题有关。

编码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;

namespace MLDRApplication
{
    public class ExcelReader
    {
        public static List<Customer> ExcelReadergetCustomers(string path)
        {
            List<Customer> customersList = new List<Customer>();
            //Create COM Objects. Create a COM object for everything that is referenced
            Excel.Application excelApp = new Excel.Application();
            Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(@"C:\Users\fares\OneDrive\Documents\customersInitValues.xlsx");
            Excel.Worksheet customersWorksheet = excelWorkbook.Sheets[1];
            Excel.Worksheet laundryUnitsWorksheet = excelWorkbook.Sheets[2];


            Excel.Range customersRange = customersWorksheet.UsedRange;
            Excel.Range laundryUnitsRange = laundryUnitsWorksheet.UsedRange;

            int customersSheetRowCount = customersRange.Rows.Count;
            int customersSheetColCount = customersRange.Columns.Count;
            int laundryUnitsSheetRowCount = laundryUnitsRange.Rows.Count;
            int laundryUnitsSheetColCount = laundryUnitsRange.Columns.Count;



            //iterate over the rows and columns and print to the console as it appears in the file
            //excel is not zero based!!
            for (int i = 1; i <= customersSheetRowCount; i++)
            {
                for (int j = 1; j <= customersSheetColCount; j++)
                {
                    //new line
                    if (j == 1)
                        Console.Write("\r\n");

                    //write the value to the console
                    //Console.OutputEncoding = System.Text.Encoding.GetEncoding("Windows-1255");
                    if (customersRange.Cells[i, j] != null && customersRange.Cells[i, j].Value2 != null)
                    {
                        //customersList.Add()
                        Console.Write(customersRange.Cells[i, j].Value2.ToString() + "\t");
                    }
                }

            }

            for (int i = 1; i <= laundryUnitsSheetRowCount; ++i)
            {
                for (int j = 1; j <= laundryUnitsSheetColCount; ++j)
                {
                    //new line
                    if (j == 1)
                        Console.Write("\r\n");

                    //write the value to the console
                    //Console.OutputEncoding = System.Text.Encoding.GetEncoding("Windows-1255");
                    if (laundryUnitsRange.Cells[i, j] != null && laundryUnitsRange.Cells[i, j].Value2 != null)
                    {
                        //customersList.Add()
                        Console.Write(laundryUnitsRange.Cells[i, j].Value2.ToString() + "\t");
                    }
                }

            }

            //cleanup
            GC.Collect();
            GC.WaitForPendingFinalizers();

            //rule of thumb for releasing com objects:
            //  never use two dots, all COM objects must be referenced and released individually
            //  ex: [somthing].[something].[something] is bad

            //release com objects to fully kill excel process from running in the background
            Marshal.ReleaseComObject(customersRange);
            Marshal.ReleaseComObject(customersWorksheet);
            Marshal.ReleaseComObject(laundryUnitsRange);
            Marshal.ReleaseComObject(laundryUnitsWorksheet);


            //close and release
            excelWorkbook.Close();
            Marshal.ReleaseComObject(excelWorkbook);

            //quit and release
            excelApp.Quit();
            Marshal.ReleaseComObject(excelApp);

            return customersList;



        }
    }
}

我读了这个 session here ,但不了解并获得此问题的正确解决方案。

最佳答案

你的问题不在阅读中。
这是因为控制台应用程序默认编码不支持希伯来语。

在控制台应用程序中,您可以更改编码,在代码开头添加以下行:

Console.OutputEncoding = Encoding.GetEncoding("Windows-1255");

编辑:
显然

You can't print Unicode characters in the console, it only supports the characters that are available in the current code page. Characters that are not available are converted to the closest equivalent, or a question mark.



所以你可以做一个小“黑客”,只需将默认输出流更改为文件:
   Console.SetOut(new StreamWriter(File.Create("d:/your_output.txt"), Encoding.UTF8) { AutoFlush=true});

并在运行结束时将所有内容写入那里。

这是 “又快又脏” 永久解决方案的解决方案考虑使用日志库作为 log4net。

关于c# - 在 VS 上用 C# 读写希伯来语字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54841942/

相关文章:

c# - 如果交换逻辑,具有三元运算的 LINQ 运算会导致测试失败

c# - 在 C# 中使用原始套接字

c# - 当存在 500 个实时实例时,ColorConvertedBitmap 的构造失败

c# - 使用 Lambda 表达式遍历键值对的通用列表

c# - 如何创建仅在手动指定时运行的单元测试?

c# - 在 .NET 中使用 ConfigureAwait

.net - 强制 PowerShell 使用 .NET CLR 版本 2

vba - 如何使用VBA检查文本文件的时间戳

vba - 从另一个工作表中查找值(循环中的循环)

excel - 针对 COUNTIF > 255 个字符的建议解决方法出现问题