Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In my C# applications I use the EPPLUS library (<a href="http://epplus.codeplex.com/" rel="nofollow">http://epplus.codeplex.com/</a>) It is really good and fast I would say.</p> <p>Here is my code to open a sheet of a file using EPPLUS and load the data into a DataTable:</p> <pre><code>using System; using System.Collections.Generic; using System.Drawing; using System.Reflection; using System.Text; using System.Globalization; using System.Diagnostics; using System.Data; using System.Windows; using System.Windows.Controls; using System.IO; using OfficeOpenXml; using OfficeOpenXml.Style; using OfficeOpenXml.Drawing.Chart; using System.Xml; public class ProjectLoad { /// &lt;summary&gt; /// /// &lt;/summary&gt; /// &lt;param name="filePath"&gt;&lt;/param&gt; public void Load ( string filePath ) { // Get the file we are going to process FileInfo existingFile = new FileInfo( filePath ); try { if ( existingFile.Exists == true ) { // Open and read the XlSX file. using ( ExcelPackage package = new ExcelPackage( existingFile ) ) { // Get the work book in the file ExcelWorkbook workBook = package.Workbook; if ( workBook != null ) { if ( workBook.Worksheets.Count &gt; 0 ) { // read some data int sheet_number = 0; DataTable table = load_sheet_toDataGrid( workBook.Worksheets[sheet_number] ); } } } catch ( System.IO.IOException ) { //error message } } /// &lt;summary&gt; /// loads the content of a sheet into a datatable /// &lt;/summary&gt; /// &lt;param name="currentWorksheet"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; private DataTable load_sheet_toDataGrid ( ExcelWorksheet currentWorksheet ) { DataTable dt = new DataTable( ); int rows = currentWorksheet.Dimension.End.Row; int cols = currentWorksheet.Dimension.End.Column; //Add columns for ( int c = 0 ; c &lt; cols ; c++ ) { dt.Columns.Add( currentWorksheet.Cells[0 + 1 , c + 1].Value.ToString( ) ); } //add values for ( int r = 1 ; r &lt; rows ; r++ ) { object[] ob = new object[cols]; for ( int c = 0 ; c &lt; cols ; c++ ) { double value; bool isDouble = Double.TryParse( currentWorksheet.Cells[r + 1 , c + 1].Value.ToString( ) , out value ); bool isDate = false; DateTime date = new DateTime( ); if ( c == 0 &amp;&amp; !isDouble ) { isDate = DateTime.TryParse( currentWorksheet.Cells[r + 1 , c + 1].Value.ToString( ) , out date ); } if ( isDouble ) { ob[c] = value; } else if ( isDate ) { ob[c] = date; } else { ob[c] = currentWorksheet.Cells[r + 1 , c + 1].Value; } } dt.Rows.Add( ob ); } return dt; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload