4
9,890,640 members (52,431 online) 2K Sign out manuu home articles discussions features community help read excel spreadsheet in c# and display in Ask a Question All Questions All Unanswered Mine FAQ Next Improve question Permalink Posted 23 Aug '12 - 0:55 pranathis012 5 solutions Top Rated Most Recent Vote: Solution 1 Read Excel Sheet Data into DataTable Add a Solution Have a Question or Comment? follow this process.. Collapse | Copy Code public static DataTable exceldata(string filePath) { DataTable dtexcel = new DataTable(); bool hasHeaders = false; string HDR = hasHeaders ? "Yes" : "No"; string strConn; if (filePath.Substring(filePath.LastIndexOf('.')).ToLower() == ".xlsx") strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\""; else strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\""; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); //Looping Total Sheet of Xl File /*foreach (DataRow schemaRow in schemaTable.Rows) { }*/ //Looping a first Sheet of Xl File DataRow schemaRow = schemaTable.Rows[0]; string sheet = schemaRow["TABLE_NAME"].ToString(); if (!sheet.EndsWith("_")) { string query = "SELECT * FROM [" + sheet3 + "]"; OleDbDataAdapter daexcel = new OleDbDataAdapter(query, conn); dtexcel.Locale = CultureInfo.CurrentCulture; daexcel.Fill(dtexcel); } Vote: See more: ASP.NET C#4.0 Hi everyone.. I have an Excel Sheet ,in that work sheet names are Sheet1,Sheet2,Sheet3... I want to load Sheet3 data in to my DataTable. How to do this can any one help me to do this . Thank's in advance quick answers Your Filters Interested Ignored Save Filters Top Experts in 24hrs 0 Sergey Alexandrovich Kryukov 443 1 Arun Vasu 381 2 OriginalGriff 345 3 Maciej Los 191 4 Aarti Meswania 190 Top Experts this month 0 Sergey Alexandrovich Kryukov 9,755 1 OriginalGriff 7,559 2 CPallini 4,018 3 Rohan Leuva 3,362 4 Maciej Los 2,951 Your status enables you to edit this question. Alternatively, if the question is incomplete or simply isn't a question, then please report it. Read Excel Sheet Data into DataTable - CodeProject http://www.codeproject.com/Questions/445400/Read-Excel-Sheet-Data... 1 of 4 25/05/2013 01:10

Read Excel Sheet Data Into DataTable - CodeProject

Embed Size (px)

DESCRIPTION

Read Excel Sheet Data Into DataTable - CodeProject

Citation preview

Page 1: Read Excel Sheet Data Into DataTable - CodeProject

9,890,640 members (52,431 online) 2K Sign out

manuu

home articles discussions features community help read excel spreadsheet in c# and display into datatable

Ask a Question All Questions All Unanswered Mine FAQ Next

Improve question Permalink Posted 23 Aug '12 - 0:55pranathis012

5 solutions Top Rated Most Recent

Vote: Solution 1

Read Excel Sheet Data into DataTable

Add a Solution

Have a Question or Comment?

follow this process..

Collapse | Copy Code

public static DataTable exceldata(string filePath) { DataTable dtexcel = new DataTable(); bool hasHeaders = false; string HDR = hasHeaders ? "Yes" : "No"; string strConn; if (filePath.Substring(filePath.LastIndexOf('.')).ToLower() == ".xlsx") strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\""; else strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\""; OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); //Looping Total Sheet of Xl File /*foreach (DataRow schemaRow in schemaTable.Rows) { }*/ //Looping a first Sheet of Xl File DataRow schemaRow = schemaTable.Rows[0]; string sheet = schemaRow["TABLE_NAME"].ToString(); if (!sheet.EndsWith("_")) { string query = "SELECT * FROM [" + sheet3 + "]"; OleDbDataAdapter daexcel = new OleDbDataAdapter(query, conn); dtexcel.Locale = CultureInfo.CurrentCulture; daexcel.Fill(dtexcel); }

Vote: See more: ASP.NET C#4.0

Hi everyone.. I have an Excel Sheet ,in that work sheet names are Sheet1,Sheet2,Sheet3... I want to load Sheet3 data in to my DataTable. How to do this can any one help me to do this .

Thank's in advance

quick answers

Your Filters

Interested

Ignored

Save Filters

Top Experts in 24hrs

0 Sergey Alexandrovich Kryukov 443

1 Arun Vasu 381

2 OriginalGriff 345

3 Maciej Los 191

4 Aarti Meswania 190

Top Experts this month

0 Sergey Alexandrovich Kryukov 9,755

1 OriginalGriff 7,559

2 CPallini 4,018

3 Rohan Leuva 3,362

4 Maciej Los 2,951

Your status enables you to edit thisquestion. Alternatively, if the question isincomplete or simply isn't a question,then please report it.

Read Excel Sheet Data into DataTable - CodeProject http://www.codeproject.com/Questions/445400/Read-Excel-Sheet-Data...

1 of 4 25/05/2013 01:10

Page 2: Read Excel Sheet Data Into DataTable - CodeProject

Improve solution Permalink Posted 23 Aug '12 - 0:58Kamalkant(kk) 1.4K

Edited 23 Aug '12 - 1:04Uma Shankar Patel 25.6K

v2

Vote: Solution 2

Improve solution Permalink Posted 23 Aug '12 - 1:01Uma Shankar Patel 25.6K

Vote: Solution 3

Improve solution Permalink Posted 23 Aug '12 - 2:34chetan virkar 2.8K

Vote: Solution 4

conn.Close(); return dtexcel; }

Have a Question or Comment?

It works same as in case of database.like

Collapse | Copy Code

OleDbConnection cnn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath(@"~\data\cocustomerdetails.xlsx") + "; Extended Properties=Excel 12.0;"); OleDbCommand oconn = new OleDbCommand("select * from [Sheet1$]", cnn); cnn.Open(); OleDbDataAdapter adp = new OleDbDataAdapter(oconn); DataTable dt = new DataTable(); adp.Fill(dt);

Have a Question or Comment?

refer this linkRead Excel in ASP.NET[^]and just replace sheet1 to sheet3may be this will help u thank you@ChetanV@

Have a Question or Comment?

Hello,It is very easy to export excel data into datatable using [Commercial Spam Link Removed]. Take a lookat this C# example:

Collapse | Copy Code

ExcelWorkbook Wbook = ExcelWorkbook.ReadXLS(@"c:\test.xls");ExcelWorksheet Wsheet = Wbook.Worksheets["Sheet3"];DataTable dt = new DataTable();dt = Wsheet.WriteToDataTable();

Read Excel Sheet Data into DataTable - CodeProject http://www.codeproject.com/Questions/445400/Read-Excel-Sheet-Data...

2 of 4 25/05/2013 01:10

Page 3: Read Excel Sheet Data Into DataTable - CodeProject

Improve solution Permalink Posted 24 Feb '13 - 23:27Eric Goodman 261

Edited 25 Feb '13 - 10:05asreighawleiybhghwlerisg 109K

v2

Vote: Solution 5

Improve solution Permalink Posted 25 Feb '13 - 16:55Lacy00 1.4K

Have a Question or Comment?

You can use below code:

Collapse | Copy Code

namespace DataFromWorkbookToDB{ class Program { static void Main(string[] args) { Workbook workbook = new Workbook(); workbook.LoadFromFile(@"..\..\Sample.xlsx"); string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;data Source=sample.accdb;Persist Security Info=False;"; DataTable dt = workbook.Worksheets[0].ExportDataTable(); DataTable dt2 = workbook.Worksheets[1].ExportDataTable(); using (OleDbConnection conn = new OleDbConnection(connStr)) { conn.Open(); OleDbCommand command = new OleDbCommand(); command.Connection = conn; command.CommandText = "CREATE TABLE report(Name VARCHAR(225), Capital VARCHAR(225) ,Continent VARCHAR(225),Area VARCHAR(225),Population VARCHAR(225))"; command.ExecuteNonQuery(); command.CommandText = "CREATE TABLE lists(PartNo VARCHAR(225), VendorNo VARCHAR(225) ,Description VARCHAR(225),OnHand VARCHAR(225),OnOrder VARCHAR(225))"; command.ExecuteNonQuery(); for (int i = 0; i < dt.Rows.Count; i++) { DataRow row = dt.Rows[i]; string commd = "insert into [report](Name,Capital,Continent,Area,Population) values('" + row[0].ToString() + "','" + row[1].ToString() + " ','" + row[2].ToString() + "','" + row[3].ToString() +"','"+ row[4].ToString() + "')"; command.CommandText = commd; command.ExecuteNonQuery(); } for (int i = 0; i < dt2.Rows.Count; i++) { DataRow row = dt2.Rows[i]; string commd = "insert into [lists](PartNo,VendorNo,Description,OnHand,OnOrder) values('" + row[0].ToString() + "','" + row[1].ToString() + " ','" + row[2].ToString() + "','" + row[3].ToString() + "','" + row[4].ToString() + "')"; command.CommandText = commd; command.ExecuteNonQuery(); } } } }}

please note that above code needs this excel library Spire.XLS for .NET, you can give it a try,

Have a Question or Comment?

Add your solution here

B I U S small BIG var < > & link [^] encode untab indent outdent

Preview

code

Read Excel Sheet Data into DataTable - CodeProject http://www.codeproject.com/Questions/445400/Read-Excel-Sheet-Data...

3 of 4 25/05/2013 01:10

Page 4: Read Excel Sheet Data Into DataTable - CodeProject

Advertise | Privacy | MobileWeb01 | 2.6.130523.1 | Last Updated 25 Feb 2013

Copyright © CodeProject, 1999-2013All Rights Reserved. Terms of Use

Layout: fixed | fluid

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Submit your solution!

When answering a question please:

Read the question carefully.1.

Understand that English isn't everyone's first language so be lenient of bad spelling andgrammar.

2.

If a question is poorly phrased then either ask for clarification, ignore it, or edit thequestion and fix the problem. Insults are not welcome.

3.

Let's work to help developers, not make them feel stupid.

Read Excel Sheet Data into DataTable - CodeProject http://www.codeproject.com/Questions/445400/Read-Excel-Sheet-Data...

4 of 4 25/05/2013 01:10