Reading data from an Excel worksheet using C #

how to read information inside excel sheet using c # code ......

+4
source share
6 answers

You can use oledb

using System.Data; using System.Data.OleDb; OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Book1.xls;Extended Properties=Excel 8.0"); OleDbDataAdapter da = new OleDbDataAdapter("select * from MyObject", con); DataTable dt = new DataTable(); da.Fill(dt); 

or are you using Office Interop

 this.openFileDialog1.FileName = "*.xls"; if (this.openFileDialog1.ShowDialog() == DialogResult.OK) { Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open( openFileDialog1.FileName, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true); Excel.Sheets sheets = theWorkbook.Worksheets; Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1); for (int i = 1; i <= 10; i++) { Excel.Range range = worksheet.get_Range("A"+i.ToString(), "J" + i.ToString()); System.Array myvalues = (System.Array)range.Cells.Value; string[] strArray = ConvertToStringArray(myvalues); } } 
+4
source

If the data is tabular, use OleDB, otherwise you can use Automation to automate Excel and copy the values ​​to your application.

Or if this is the new excel XML format, you can do this through the framework classes.

Excel Automation Information: A Practical Guide. Using COM Interop to Create an Excel Spreadsheet

Excel information through OleDB: How to use ADO.NET to retrieve and modify records in an Excel workbook using Visual Basic.NET

0
source

Excel has an API for programming. You can use it to get a range of data using C #. You can use something like:

  Excel.Application oXL; Excel._Workbook oWB; Excel._Worksheet oSheet; oSheet.get_Range(RangeStart,RangeEnd) 
0
source

NPOI is the way to go.

Using service interaction requires that Office (and the correct version) is installed on the computer on which the application is running. If it's a web abbreviation, it probably means no, and in any case, it is not strong enough for a production environment. OLEDB has serious limitations, unless it is one-time, and the data is really simple, I would not use it.

0
source
  • Excel COM Interop - via Microsoft.Office.Interop.Excel + Microsoft.Office.Interop.Excel.Extensions
  • ADO.Net through the OLEDB data provider for Excel.
  • Using System.XML and / or Linq for XML and / or Open XML SDK (can also be used to create new books programmatically from scratch).
  • Third party library such as NPOI.
  • A third-party vendor package such as spreadsheetgear
0
source

All Articles