1、无合并单元格的Excel文档读取
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + temp + ";Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
try
{
//1. 建立连接
//注意,"Extended Properties"是必需的
conn.Open();
//2. 读取数据
string query = "select * from [Sheet1$]";
OleDbCommand oleCommand = new OleDbCommand(query, conn);
OleDbDataAdapter da = new OleDbDataAdapter(oleCommand);
System.Data.DataTable table = new System.Data.DataTable();
da.Fill(table);
//table中的数据就是Excel中的内容
}
finally
{
conn.Close();
}
2、不规则Excel文件(包含合并单元格)
using Microsoft.Office.Interop.Excel;
using System.Reflection;