1

我正在使用此代码导入 excel (.xls) 数据,但列中的所有方括号都替换为圆括号。例如列 dm[kg]:

进口前:dm[kg]

进口后:dm(kg)

我会很感激任何帮助。

public static DataTable ImportWorkSheet(string excelFile, bool hasHeaderRow, bool allText, string tabName)
    {

        string connectionString = GetExcelConnectionString(excelFile, hasHeaderRow, allText);



        using (OleDbConnection connection = new OleDbConnection(connectionString))
        {

            connection.Open();

            if (Path.GetExtension(excelFile).Equals(".csv", StringComparison.OrdinalIgnoreCase))

                tabName = Path.GetFileName(excelFile);

            else

                tabName = EnsureTableName(connection, tabName);

            string selectString = string.Format("SELECT * FROM [{0}]", tabName);



            DataTable table = new DataTable(tabName);

            using (OleDbDataAdapter adapter = new OleDbDataAdapter(selectString, connection))
            {
                adapter.Fill(table);

            }



            return table;

        }

    }
private static string EnsureTableName(DbConnection connection, string tabName)
    {

        if (string.IsNullOrEmpty(tabName))
        {

            //use GetSchema to find the first sheet == table name

            DataTable worksheets = connection.GetSchema("Tables");

            foreach (DataRow row in worksheets.Rows)
            {

                //this can also return Excel named ranges

                tabName = (string)row["TABLE_NAME"];

                //so look for sheets (excel puts $ after the name and may single-quote the name)

                if (tabName.EndsWith("$") || tabName.EndsWith("$'"))

                    return tabName;

                //otherwise we'll fall through with whatever we find

            }

        }

        //they supplied a worksheet name; ensure always has $ suffix

        else if (!tabName.EndsWith("$"))

            tabName += "$";

        return tabName;

    }



    private static string GetExcelConnectionString(string filePath, bool hasHeaderRow, bool allText)
    {

        //http://www.connectionstrings.com/?carrier=excel

        string connectionString;

        string ext = Path.GetExtension(filePath);

        if (ext.Equals(".csv", StringComparison.OrdinalIgnoreCase))
        {

            //uses directory path, not file path

            //for format definition, write a schema.ini in the folder: http://msdn.microsoft.com/en-us/library/ms709353.aspx

            connectionString = string.Format(

                @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; Extended Properties=""text;FMT=Delimited;{1}{2}""",

                filePath.Remove(filePath.IndexOf(Path.GetFileName(filePath))),

                hasHeaderRow ? "HDR=YES;" : "HDR=NO;",

                allText ? "IMEX=1" : string.Empty);

        }

        else if (ext.Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
        {

            //if this fails, install 2007 providers from http://www.microsoft.com/downloads/details.aspx?FamilyID=7554F536-8C28-4598-9B72-EF94E038C891&displaylang=en

            connectionString = string.Format(

                @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=""Excel 12.0 Xml;{1}{2}""",

                filePath,

                hasHeaderRow ? "HDR=YES;" : "HDR=NO;",

                allText ? "IMEX=1" : string.Empty);

        }

        else //assume normal excel
        {

            connectionString = string.Format(

                @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; Extended Properties=""Excel 8.0;{1}{2}""",

                filePath,

                hasHeaderRow ? "HDR=YES;" : "HDR=NO;",

                allText ? "IMEX=1" : string.Empty);

        }

        return connectionString;

    }
4

0 回答 0