Hi jonlink,
Try this out for importing the data to excel.
public DataSet GetDataFromExcel(string filePath)
{
try
{
string strConn;
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + "Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1;TypeGuessRows=0;ImportMixedTypes=Text\"";
DataTable dt = new DataTable();
dt = null;
using (OleDbConnection oleDB = new OleDbConnection(strConn))
{
oleDB.Open();
dt = oleDB.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (dt == null)
return null;
ListItemCollection items = new ListItemCollection();
int i = 0;
for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
{
string excelSheetName;
string lastCharacter = "";
excelSheetName = dt.Rows[rowIndex]["TABLE_NAME"].ToString();
excelSheetName = excelSheetName.Replace("'", "");
lastCharacter = excelSheetName.Substring(excelSheetName.Length - 1, 1);
if (lastCharacter == "$")
{
items.Add(dt.Rows[rowIndex]["TABLE_NAME"].ToString());
}
}
if (items.Count > 1)
return null;
string sName;
string query;
sName = items[0].ToString();
sName = sName.Replace("'", "");
sName = sName.Replace("$", "");
query = "";
query = String.Format("select * from [{0}$]", sName);
OleDbDataAdapter da = new OleDbDataAdapter(query, strConn);
DataSet ds = new DataSet();
da.Fill(ds);
return ds;
}
}
catch (Exception ex)
{
throw ex;
}
}
public static DataSet ImportExceltoDataset(string file)
{
IExcelDataReader iExcelDataReader = null;
FileStream oStream = File.Open(file, FileMode.Open, FileAccess.Read);
iExcelDataReader = ExcelReaderFactory.CreateBinaryReader(oStream);
iExcelDataReader.IsFirstRowAsColumnNames = true;
DataSet dsUnUpdated = new DataSet();
dsUnUpdated = iExcelDataReader.AsDataSet();
iExcelDataReader.Close();
return dsUnUpdated;
}
Some links:
link-1 import data
link-2 Export data
link-3 Read and Write Excel Data Using C#
If you are trying to import the data to an excel sheet from a grid then try this out.
import data from Excel to Datagrid
Import excel sheet data to datagrid without using oledb
Thanks