【问题标题】:Why excel change characters automatically? Trying to read excel in c# using OleDbDataAdapter为什么excel会自动换字符?尝试使用 OleDbDataAdapter 在 c# 中读取 excel
【发布时间】:2016-12-13 03:13:29
【问题描述】:

我正在尝试使用 OleDbDataAdapter 将数据从 excel 提取到数据库 SQLite。 但如下图所示,“-”号自动变为方形符号。 实际上,它在 excel 甚至在 sqlite 数据库中显示“-”破折号,但如果我尝试搜索该模型,它不会显示。然后我通过复制另一个文本来解决这个问题,这个标志正在变成正方形,这就是为什么如果我搜索“AKAA1-571”它无法搜索。

但我不明白为什么它会改变?它显示了一些东西,但实际上是其他东西。我应该如何防止这种情况,因为我需要“-”而不是其他东西,以便我可以触发查询来搜索

对不起,我是 excel、Unicode、字符的新手。请帮帮我。

谢谢。

代码:- #region Excel 连接 私有字符串 ExcelConnectionOptions() { 字符串 strOpts = ""; 如果(this.MixedData == true) strOpts += "Imex=1;"; 如果(this.Headers == true) strOpts += "HDR=是;"; 别的 strOpts += "HDR=否;"; 返回strOpts; }

    private string ExcelConnection()
    {

        string provider = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
         @"Data Source=" + _strExcelFilename + ";" +
         @"Extended Properties=" + Convert.ToChar(34).ToString() +
         @"Excel 8.0;" + ExcelConnectionOptions() + Convert.ToChar(34).ToString();

        return provider;
    }
    #endregion

私有布尔 SetSheetQuerySelect() { 尝试 { 如果(_oleConn == null) { throw new Exception("连接未分配或关闭。"); }

            if (_strSheetName.Length == 0)
                throw new Exception("Sheetname was not assigned.");

            _oleCmdSelect = new OleDbCommand(
                @"SELECT * FROM ["
                + _strSheetName
                + "$"
                + "]", _oleConn);

            return true;
        }

public System.Data.DataTable GetTable(string strTableName)

    {
        try
        {
            //Open and query
            if (_oleConn == null) Open();
            if (_oleConn.State != ConnectionState.Open)
                throw new Exception("Connection cannot open error.");
            if (SetSheetQuerySelect() == false) return null;

            //Fill table
            OleDbDataAdapter oleAdapter = new OleDbDataAdapter();
            oleAdapter.SelectCommand = _oleCmdSelect;
            System.Data.DataTable dt = new System.Data.DataTable(strTableName);
            oleAdapter.FillSchema(dt, SchemaType.Source);
            oleAdapter.Fill(dt);

}

****经过搜索,我发现在 excel 文件中,字符属于不同的字体/字符类别。但看起来很相似,所以当我通过 OLEDBAdapter 阅读时,我想用简单的“-”替换这些特殊字符,但是为了阅读,我使用的是 OLEDbadapter,如上,如何使用替换字符串? ****

【问题讨论】:

  • 您在代码中做了一些事情,结果不正确,然后您寻求帮助...没有显示代码。请帮助我们为您提供帮助。
  • 我同意 KC Wong 的观点。在没有代码的情况下显示您的输入和输出对任何人都没有帮助。我的测试给了我预期的破折号。我会调查你是如何读取数据的。
  • 您好,我添加了用于读取 OleDbDataAdapterexcel 的代码。

标签: c# excel sqlite


【解决方案1】:

现在您知道发生了什么 - Excel 数据不包含简单的减号/破折号,而是包含其他内容(可能是软连字符)。

然后您可以简单地使用 String.Replace 将字符交换为该列中的数据: https://msdn.microsoft.com/en-us/library/czx8s9ts(v=vs.110).aspx

编辑:

由于您使用的是 OleDbDataAdapter,因此您可能需要这些:

https://msdn.microsoft.com/en-us/library/ms180059.aspx(UNICODE 和 CONVERT 函数)

https://msdn.microsoft.com/en-us/library/ms186862.aspx(替换函数)

修改您的 SQL 语句以将非破折号替换为破折号。

【讨论】:

    【解决方案2】:

    不知何故,我尝试使用 OLedbadapater 首先在 Datatable 中解决这个获取数据的问题。 然后逐行更改该数据列中的数据。如下所示

    公共数据表 trasnformTable(DataTable dt) { 数据表 new_dt = dt.Copy(); DataRow dr = new_dt.Rows[0];

            foreach(DataRow row in new_dt.Rows)
            {
                string Part_no = dr[0].ToString().Trim();
                string replaces_string = Regex.Replace(Part_no, @"[^a-zA-Z0-9\s]+", "-");
                dr[0] = replaces_string;
            }
    

    返回 new_dt; }

    我不知道在从 excel 中获取时如何实现这一点,因为 oledb 只是使用 select 命令直接批量获取数据表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-18
      • 2013-03-18
      相关资源
      最近更新 更多