There are two solutions that I will provide:
Solution 1:
See belwo code, it can help you export excel to both ms access and sql datatable:
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"..\..\sample.xlsx");
Worksheet sheet = workbook.Worksheets[0];
DataTable data= sheet.ExportDataTable();
string conn = "Provider=Microsoft.ACE.OLEDB.12.0;data Source=dataBase.accdb;Persist Security Info=False;";
SqlConnection conn= new SqlConnection(connectionStr);
SqlCommand command = new SqlCommand();
conn.Open();
command.Connection = conn;
for (int i=1;i<data.rows.count;i++)>
{
DataRow row=data.Rows[i];
string commd = "insert into [tableSample](Name,Capital,Continent,Area) values('" + row[0].ToString() + "','" + row[1].ToString() +
" ','" + row[2].ToString() + "','" + row[3].ToString()+ "')";
command.CommandText = commd;
command.ExecuteNonQuery();
}
conn.Close();
In performing above code, you need a excel library:
http://spreadsheet.codeplex.com/[
^]Please note that this is a commercial library. anyhow, it can have a free trial.
Solution two:
static void ReadExcel(String strFileName)
{
Object Opt = Missing.Value;
Microsoft.Office.Interop.Excel.Application app = new Application(); Microsoft.Office.Interop.Excel.Workbook book;
Microsoft.Office.Interop.Excel.Worksheet xlsSheet;
Microsoft.Office.Interop.Excel.Range oRng;
book = app.Workbooks.Open(“”, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt);
xlsSheet = (Microsoft.Office.Interop.Excel.Worksheet)book.Sheets[1];
oRng = (Microsoft.Office.Interop.Excel.Range)xlsSheet.Cells[1, 1];
String StrCell = oRng.Text.ToString();
Console.WriteLine(StrCell);
book.Close(false, false, Missing.Value);
}
in using code in solution two, you need to install Microsoft.Office.Interop:
http://www.microsoft.com/en-us/download/details.aspx?id=3508[
^]