using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Addmysql
{
public class ExcelToTable
{
/// <summary>
/// 读取excel文件内容并存放在DataSet中.
/// </summary>
/// <returns>返回DataSet对象</returns>
public DataSet ExcelToDS(string path, out string err)
{
DataSet ds = null;
err = "";
try
{
//string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;"
// + "Data Source=" + @path + ";" + "Extended Properties=Excel 8.0;";
string strConn = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + @path + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
strExcel = "select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
DataTable table1 = new DataTable();
ds = new DataSet();
myCommand.Fill(table1);
myCommand.Fill(ds);
}
catch (Exception ex)
{
err = ex.Message;
}
return ds;
}
}
}
这个上ExcelToTable类,继续下一篇:sqlHelp