I have a excel file.I want to read the file using c#.
Excel file contains following.
- Name subject Marks
- A maths 35
- B maths 50
- C maths 69
- ------------Empty row-----------------
- D maths 36
i want my o/p like below.
- Name subject Marks
- A maths 35
- B maths 50
- C maths 69
- D maths 36
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Excel = Microsoft.Office.Interop.Excel;
- namespace excel
- {
- class Program
- {
- static void Main(string[] args)
- {
- Excel.Application xlApp = new Excel.Application();
- Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\dinesh\Sample.xlsx");
- Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1]; // assume it is the first sheet
- Excel.Range xlRange = xlWorksheet.UsedRange;
- xlWorkbook.Worksheets.Add(xlWorksheet);
- }
- }
- }

Rafnas T PRank #280 · 8.8K reputationPosted Jan 25, 2017, 1:40 AM EST
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"E:\Book1.xlsx");
Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1]; // assume it is the first sheet
Excel.Range xlRange = xlWorksheet.UsedRange;
int rowCount = xlRange.Rows.Count;
//int colCount = 3;
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("subject", typeof(string));
dt.Columns.Add("Marks", typeof(string));
for (int i = 2; i <= rowCount; i++)
{
cell1 = ""; cell2 = ""; cell3 = "";
//for (int j = 1; j <= colCount; j++)
//{
if (xlRange.Cells[i, 1] != null && xlRange.Cells[i, 1].Value2 != null)
cell1 = xlRange.Cells[i, 1].Value2.ToString();
if (xlRange.Cells[i, 2] != null && xlRange.Cells[i, 2].Value2 != null)
cell2 = xlRange.Cells[i, 2].Value2.ToString();
if (xlRange.Cells[i, 3] != null && xlRange.Cells[i, 3].Value2 != null)
cell3 = xlRange.Cells[i, 3].Value2.ToString();
//here you will get cell value per iteration , from here you can add to datatable
//}
if(cell1 != "" && cell2 != "" && cell3 != "")
dt.Rows.Add(cell1, cell2, cell3);
}
dt.AcceptChanges();
xlWorksheet.Cells.Clear();
for (var i = 0; i < dt.Columns.Count; i++)
{
xlWorksheet.Cells[1, i + 1] = dt.Columns[i].ColumnName;
}
// rows
for (var i = 0; i < dt.Rows.Count; i++)
{
for (var j = 0; j < dt.Columns.Count; j++)
{
xlWorksheet.Cells[i + 2, j + 1] = dt.Rows[i][j];
}
}
xlWorksheet.SaveAs(@"E:\Book1.xlsx");
GC.Collect();
GC.WaitForPendingFinalizers();
//release com objects to fully kill excel process from running in the background
Marshal.ReleaseComObject(xlRange);
Marshal.ReleaseComObject(xlWorksheet);
//close and release
xlWorkbook.Close();
Marshal.ReleaseComObject(xlWorkbook);
//quit and release
xlApp.Quit();
Rafnas T PRank #280 · 8.8K reputationPosted Jan 23, 2017, 11:59 PM EST
string constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", "C:\dinesh\Sample.xlsx");
Econ = new OleDbConnection(constr);
string Query = string.Format("Select [Name ],[subject ],[Marks] FROM [{0}]", "Sheet1$");
OleDbCommand Ecom = new OleDbCommand(Query, Econ);
Econ.Open();
DataSet ds = new DataSet();
OleDbDataAdapter oda = new OleDbDataAdapter(Query, Econ);
Econ.Close();
oda.Fill(ds);
DataTable Exceldt = ds.Tables[0];
for (int i = Exceldt.Rows.Count - 1; i >= 0; i--)
{
if (Exceldt.Rows[i][" Name "] == DBNull.Value || Exceldt.Rows[i]["subject "] == DBNull.Value || Exceldt.Rows[i]["Marks"] == DBNull.Value)
{
Exceldt.Rows[i].Delete();
}
}
Exceldt.AcceptChanges();
Dinesh SanthalingamRank #2,085 · 690 reputationPosted Jan 24, 2017, 2:13 AM EST
Dinesh SanthalingamRank #2,085 · 690 reputationPosted Jan 24, 2017, 2:36 AM EST
Dinesh SanthalingamRank #2,085 · 690 reputationPosted Jan 24, 2017, 11:40 PM EST
Rafnas T P
Rafnas T PRank #280 · 8.8K reputationPosted Jan 24, 2017, 11:56 PM EST
Dinesh SanthalingamRank #2,085 · 690 reputationPosted Jan 24, 2017, 11:59 PM EST
Dinesh SanthalingamRank #2,085 · 690 reputationPosted Jan 25, 2017, 12:37 AM EST
Rafnas T PRank #280 · 8.8K reputationPosted Jan 25, 2017, 1:26 AM EST
Dinesh SanthalingamRank #2,085 · 690 reputationPosted Jan 25, 2017, 2:35 AM EST
Rafnas T P
Toti Bird23 reputationPosted Jan 25, 2017, 10:54 AM EST
Chandan Rajbhar152 reputationPosted Feb 10, 2017, 8:47 PM EST
Dinesh SanthalingamRank #2,085 · 690 reputationPosted Jan 24, 2017, 1:15 AM EST
Can you explain it without using OlEDb? @
Rafnas T PRank #280 · 8.8K reputationPosted Jan 24, 2017, 2:07 AM EST
Rafnas T PRank #280 · 8.8K reputationPosted Jan 24, 2017, 2:26 AM EST
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\dinesh\Sample.xlsx");
Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1]; // assume it is the first sheet
Excel.Range xlRange = xlWorksheet.UsedRange;
int rowCount = xlRange.Rows.Count;
int colCount = xlRange.Columns.Count;
for (int i = 0; i <= rowCount; i++)
{
for (int j = 0; j <= colCount; j++)
{
string cell= (string)(xlRange.Cells[i, j] as Excel.Range).Value2;
}
}
Rafnas T PRank #280 · 8.8K reputationPosted Jan 24, 2017, 11:36 PM EST
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"E:\Book1.xlsx");
Excel.Worksheet xlWorksheet = xlWorkbook.Sheets[1]; // assume it is the first sheet
Excel.Range xlRange = xlWorksheet.UsedRange;
int rowCount = xlRange.Rows.Count;
//int colCount = 3;
DataTable dt = new DataTable();
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("subject", typeof(string));
dt.Columns.Add("Marks", typeof(string));
for (int i = 2; i <= rowCount; i++)
{
cell1 = ""; cell2 = ""; cell3 = "";
//for (int j = 1; j <= colCount; j++)
//{
if (xlRange.Cells[i, 1] != null && xlRange.Cells[i, 1].Value2 != null)
cell1 = xlRange.Cells[i, 1].Value2.ToString();
if (xlRange.Cells[i, 2] != null && xlRange.Cells[i, 2].Value2 != null)
cell2 = xlRange.Cells[i, 2].Value2.ToString();
if (xlRange.Cells[i, 3] != null && xlRange.Cells[i, 3].Value2 != null)
cell3 = xlRange.Cells[i, 3].Value2.ToString();
//here you will get cell value per iteration , from here you can add to datatable
//}
if(cell1 != "" && cell2 != "" && cell3 != "")
dt.Rows.Add(cell1, cell2, cell3);
}
dt.AcceptChanges();
GC.Collect();
GC.WaitForPendingFinalizers();
//release com objects to fully kill excel process from running in the background
Marshal.ReleaseComObject(xlRange);
Marshal.ReleaseComObject(xlWorksheet);
//close and release
xlWorkbook.Close();
Marshal.ReleaseComObject(xlWorkbook);
//quit and release
xlApp.Quit();
Marshal.ReleaseComObject(xlApp);
Rafnas T PRank #280 · 8.8K reputationPosted Jan 24, 2017, 11:43 PM EST
Rafnas T PRank #280 · 8.8K reputationPosted Jan 24, 2017, 11:44 PM EST
Rafnas T PRank #280 · 8.8K reputationPosted Jan 25, 2017, 12:25 AM EST
Sandip G PatilRank #1,083 · 1.8K reputationPosted Feb 6, 2017, 9:35 PM EST