简体   繁体   中英

Importing Excel having 2000 columns

I used OleDb for importing Excel file which is having 2000 columns. So by using this piece of code i can able to have only 255 columns in my DataTable. I want some other way or if possible this way to get 2000 columns to my DataTable.

DataTable dtExcelRecords = new DataTable();

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

      con.Open();
      DataTable dtSheet = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
      List<string> listSheet = new List<string>();
      foreach (DataRow drSheet in dtSheet.Rows)
      {
           if (drSheet["TABLE_NAME"].ToString().Contains("$"))
               {
               listSheet.Add(drSheet["TABLE_NAME"].ToString());
               }
      }

      string workSheetName = listSheet.FirstOrDefault(x => x.Contains(cCommon.sFDDetailsWorkSheetName));
      OleDbCommand cmd = new OleDbCommand(string.Format("SELECT * FROM [{0}]", workSheetName.Trim()), con);
      cmd.CommandType = System.Data.CommandType.Text;
      OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
      dAdapter.SelectCommand = cmd;
      dAdapter.Fill(dtExcelRecords);
      }

Using OleDb for accessing Excel is a possible way, but not the best. I suggest to consider using third party libraries for importing from Excel.

There are two very recommended:

  • NPOI , which is a .NET port of POI: Java API for MS Documents
  • ClosedXML , which a wrapper on top of OpenXML, which is a Microsoft open-source library for accessing Excel spreadsheets

Both are good and will do the job. In addition, they are free and do not require MS Office to be installed on the machine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM