2

次のコードを使用して、データをExcelファイルからテーブルに保存します

private void RetrieveAndStoreExcelData(String filePath)
{
    String excelConnectionStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source='" + filePath + "'; Extended Properties='Excel 8.0;'";
    OleDbConnection excelConnection = new OleDbConnection(excelConnectionStr);
    excelConnection.Open();

    try
    {
        //Get the name of the first worksheet
        DataTable schema = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        if (schema == null || schema.Rows.Count < 1)
        {
            throw new Exception("Error: Could not determine the name of the first worksheet.");
        }
        string firstSheetName = schema.Rows[0]["TABLE_NAME"].ToString();

        //Retrieve data from worksheet into reader
        string query = "SELECT * FROM [" + firstSheetName + "]";
        OleDbCommand command = new OleDbCommand(query, excelConnection);
        OleDbDataReader dbReader = command.ExecuteReader();  //IEnumerable

        //populate IEnumerable
        if (dbReader.HasRows)
        {
            populateRecords(dbReader);
        }
    }
    finally
    {
        excelConnection.Close();
    }
}

これはうまくいきます。ただし、フィールドの長さの 1 つが 255 文字を超える場合、文字列は 255 文字に切り捨てられます。これは、その行が Excel シートの 10 行目の後に表示される場合でもあります。

したがって、最初の 10 行の長さが 255 未満の場合、すべての行の長さが 255 文字未満であると想定されます。

それで、これを解決する方法はありますか?

4

0 に答える 0