【问题标题】:How to set column length and type如何设置列长和类型
【发布时间】:2015-01-28 15:58:14
【问题描述】:

我正在从xls/xlsx 文件读取到DataSet

string connstring;
            if (currFileExtension == ".xlsx")
            {
                connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + currFilePath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
            }
            else
            {
                connstring = "Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + currFilePath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';";
            }

            using (OleDbConnection conn = new OleDbConnection(connstring))
            {
                conn.Open();
                DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "Table" });  //Get All Sheets Name
                string firstSheetName = sheetsName.Rows[0][2].ToString();   //Get the First Sheet Name
                string sql = string.Format("SELECT * FROM [{0}]", firstSheetName);  //Query String
                OleDbDataAdapter ada = new OleDbDataAdapter(sql, connstring);
                DataSet set = new DataSet();

                ada.Fill(set);

}

其中一列是字符串,在 255 个字符后被截断。

如何在填充数据集之前设置列? 可能是excel的问题,需要改一下excel的列?

【问题讨论】:

    标签: c# asp.net excel datatable ado.net


    【解决方案1】:

    您可以将已定义的 DataTable 添加到 DataSet。

    代码如下:

    DataSet dataSet = new DataSet();
    
    DataTable customTable = new DataTable();
    DataColumn dcName = new DataColumn("Name", typeof(string));
    dcName.MaxLength= 500;
    customTable.Columns.Add(dcName);
    
    dataSet.Tables.Add(customTable);
    

    【讨论】:

      猜你喜欢
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      • 2012-04-09
      • 1970-01-01
      • 2015-11-21
      • 2017-10-19
      • 1970-01-01
      相关资源
      最近更新 更多