gpt4 book ai didi

c# - 将 DataTable 导出到 Excel 文件显示 ?关于数学符号

转载 作者:太空宇宙 更新时间:2023-11-03 14:40:08 25 4
gpt4 key购买 nike

我正在开发一个 WPF 应用程序,我需要将我的数据导出到 excel 中。我有分数、度数、幂符号等数学查询数据。我能够正确地看到我的数据,但是当我将数据导出到 excel 时,它会将这些符号更改为?

之前我对数据使用 Encoding.ASCII 然后我知道我们需要改变它的东西。我已经尝试过 UTF7、UTF8、Unicode 和 Default 但没有任何效果。虽然我能够使用 Encoding.Default 正确获取一些数据在搜索时,我了解了 Office Interlop,但要正确运行它,您需要安装 MS Office excel,我不希望我的项目有这种依赖性。

  int rowIndex = 1;
colIndex = 0;
foreach (DataRow row in dvRecords.Table.Rows)
{
foreach (DataColumn col in dvRecords.Table.Columns)
{
if (row[col] != null)
{

if (col.DataType == typeof(int))
this.WriteCell(rowIndex, colIndex, Convert.ToInt32(row[col]));
else if (col.DataType == typeof(double))
this.WriteCell(rowIndex, colIndex, Convert.ToDouble(row[col]));
else if (col.DataType == typeof(bool))
this.WriteCell(rowIndex, colIndex, (Convert.ToBoolean(row[col]) ? "Yes" : "No"));
else
this.WriteCell(rowIndex, colIndex, Convert.ToString(row[col]));

colIndex++;
}
}
rowIndex++;
colIndex = 0;
}

写单元格方法

 private BinaryWriter _writer;

private void WriteCell(int row, int col, string value)
{

ushort[] clData = { 0x0204, 0, 0, 0, 0, 0 };
byte[] plainText = Encoding.Default.GetBytes(value);
int iLen = plainText.Length;
clData[1] = (ushort)(8 + iLen);
clData[2] = (ushort)row;
clData[3] = (ushort)col;
clData[5] = (ushort)iLen;
WriteUshortArray(clData);
_writer.Write(plainText);
}

private void WriteUshortArray(ushort[] value)
{
for (int i = 0; i < value.Length; i++)
_writer.Write(value[i]);
}

我希望从我的 DataTable 数据中将符号正确导入到 excel 中,但我得到的数据是 "?" 符号

最佳答案

尝试使用这个:

plainText = "<html><head><meta charset=""" & Encoding.UTF8.WebName & """ /></head><body>" & plainText & "</body></html>";
_writer.Write(plainText);
_writer.End();

Excel无法阅读 UTF-8默认编码,但它使用 html <div> -s 和 table标签生成excel表格

关于c# - 将 DataTable 导出到 Excel 文件显示 ?关于数学符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57358000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com