【问题标题】:How to specify encoding in .NET Interop.Excel.Workbooks.OpenText如何在 .NET Interop.Excel.Workbooks.OpenText 中指定编码
【发布时间】:2015-06-07 04:19:57
【问题描述】:

在使用 excel.Workbooks.OpenText 方法打开文本文件时,我无法找到有关指定编码(尤其是 utf-8)可能性的任何信息。我的问题是我正在尝试打开一个以 UTF-8 编码的 CSV 文件,如果没有此设置,它会作为一堆曲线加载。

非常感谢!

【问题讨论】:

  • 好的,我发现 Origin 是要走的路,但是将其设置为 65001 (utf-8) 不起作用...

标签: c# .net excel utf-8 interop


【解决方案1】:

我发现这是导入 UTF-8 csv 文件的有效解决方案: (可以稍微优化一下,但这样你就可以朝着正确的方向努力)

Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
Workbook wb;
Worksheet ws;
Range range;
int[,] a = new int[,] { { 1, 1 }, { 2, 2 }, { 3, 2 }, { 4, 2 }, { 5, 2 }, { 6, 2 }, { 7, 2 }, { 8, 2 }, { 9, 2 }, { 10, 2 }, { 11, 2 }, { 12, 2 }, { 13, 2 }, { 14, 2 }, { 15, 2 }, { 16, 2 }, { 17, 2 }, { 18, 2 }, { 19, 2 }, { 20, 2 }, { 21, 2 }, { 22, 2 } };
excelApp.Workbooks.OpenText(ofdGetExcel.FileName,
                                            65001,
                                            1,
                                            XlTextParsingType.xlDelimited,
                                            XlTextQualifier.xlTextQualifierDoubleQuote,
                                            false,
                                            false,
                                            false,
                                            true,
                                            false,
                                            false,
                                            false,
                                            a,
                                            Type.Missing,//",",
                                            Type.Missing,//".",
                                            Type.Missing,//false,
                                            Type.Missing);//false);
wb = excelApp.ActiveWorkbook;
ws = wb.Worksheets.get_Item(1);
range = ws.get_Range("A2");

Microsoft.Office.Interop.Excel.QueryTable QT = ws.QueryTables.Add("TEXT;" + ofdGetExcel.FileName, range);
QT.Name = ofdGetExcel.FileName;
QT.FieldNames = true;
QT.RowNumbers = true;
QT.FillAdjacentFormulas = false;
QT.PreserveFormatting = true;
QT.RefreshOnFileOpen = false;
QT.RefreshStyle = Microsoft.Office.Interop.Excel.XlCellInsertionMode.xlInsertDeleteCells;
QT.SavePassword = false;
QT.SaveData = true;
QT.AdjustColumnWidth = true;
QT.RefreshPeriod = 0;
QT.TextFilePromptOnRefresh = false;
QT.TextFilePlatform = 65001;
QT.TextFileStartRow = 1;
QT.TextFileParseType = Microsoft.Office.Interop.Excel.XlTextParsingType.xlDelimited;
QT.TextFileTextQualifier = Microsoft.Office.Interop.Excel.XlTextQualifier.xlTextQualifierDoubleQuote;
QT.TextFileConsecutiveDelimiter = false;
QT.TextFileTabDelimiter = false;
QT.TextFileSemicolonDelimiter = false;
QT.TextFileCommaDelimiter = true;
QT.TextFileSpaceDelimiter = false;
QT.TextFileDecimalSeparator = ",";
QT.TextFileThousandsSeparator = ".";

ws.QueryTables[1].Destination.EntireColumn.AutoFit();

ws.QueryTables[1].Refresh(false);

ws.QueryTables[1].Delete();

【讨论】:

    猜你喜欢
    • 2016-04-05
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    • 2023-01-17
    • 2021-10-16
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    相关资源
    最近更新 更多