如何设置列的长度和types

我正在从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); } 

其中一列是string,在255个字符之后被切断。

如何在填充DataSet之前设置列? 也许这是一个Excel的问题,我需要改变在Excel中的列?

您可以将定义的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);