Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在将Excel导入C#(WPF)时替换列名中的特定字符_C#_Wpf_Excel - Fatal编程技术网

为什么在将Excel导入C#(WPF)时替换列名中的特定字符

为什么在将Excel导入C#(WPF)时替换列名中的特定字符,c#,wpf,excel,C#,Wpf,Excel,将Microsoft Excel文件导入C#(WPF)时,列名(列标题)中的特定字符将被替换。 例如它变为,而变为。如何阻止它 连接字符串: Provider=Microsoft.ACE.OLEDB.12.0; Data Source="+filePath+";Extended Properties="Excel 8.0;HDR=YES;CharacterSet=65001;"; 方法: public DataSet ImportExcel(string fileName) {

将Microsoft Excel文件导入C#(WPF)时,列名(列标题)中的特定字符将被替换。 例如
它变为
,而
变为
。如何阻止它

连接字符串:

Provider=Microsoft.ACE.OLEDB.12.0; Data Source="+filePath+";Extended Properties="Excel 8.0;HDR=YES;CharacterSet=65001;";
方法:

 public DataSet ImportExcel(string fileName)
    {
        tableList = null;
        DataSet ds = new DataSet();

        string connectionString = GetConnectionString(fileName);

        using (OleDbConnection conn = new OleDbConnection(connectionString))
        {
            conn.Open();
            OleDbCommand cmd = new OleDbCommand();

            cmd.Connection = conn;


            // Get all Sheets in Excel File
            DataTable dtSheet = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            tableList = new string[dtSheet.Rows.Count];
            // Loop through all Sheets to get data
            foreach (DataRow dr in dtSheet.Rows)
            {

                string sheetName = dr["TABLE_NAME"].ToString();
                tableList[dtSheet.Rows.IndexOf(dr)] = sheetName;
                Console.WriteLine("TABLE_NAME: " + sheetName);
                if (!sheetName.EndsWith("$"))
                    continue;

                // Get all rows from the Sheet
                cmd.CommandText = "SELECT * FROM [" + sheetName + "]";

                DataTable dt = new DataTable();

                dt.TableName = sheetName;

                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                da.Fill(dt);
                rowsCount += dt.Rows.Count;
                ds.Tables.Add(dt);
            }

            cmd = null;
            conn.Close();
        }

        return ds;
    }

基本上,您仅限于列名中允许使用哪些字符,这些字符包括字母、数字和字符:@#$u请参见。$symbol和数字也不允许作为第一个字符。

基本上,您仅限于列名中允许的字符,这些字符是字母、数字和字符:@#$\u请参阅。$符号和数字也不允许作为第一个字符