码迷,mamicode.com
首页 > 其他好文 > 详细

解析excel表格为DataSet

时间:2016-08-15 17:21:29      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using Log;
namespace Common
{
public class ExcelInputHelper
{
public static ExcelInputHelper Self = new ExcelInputHelper();

public static DataSet ExecleDataSet(string filePath)
{
string OleDbConnection = "Provider=Microsoft.Jet.OleDb.4.0;" + "data source=" + filePath + ";Extended Properties=‘Excel 8.0; HDR=YES; IMEX=1‘";
OleDbConnection conn = new OleDbConnection(OleDbConnection);
try
{
conn.Open();
}
catch (Exception ee)
{
if (ee.Message.Contains("外部表不是预期的格式"))
{
OleDbConnection = "Provider=Microsoft.Ace.OleDb.12.0;" + "data source=" + filePath + ";Extended Properties=‘Excel 12.0; HDR=YES; IMEX=1‘";
conn.ConnectionString = OleDbConnection;
conn.Open();
}
else
{
Log.Log4NetUtility.Error(Self, "无法打开文件");
return null;
}
}

DataSet ds = new DataSet();
OleDbDataAdapter odda = new OleDbDataAdapter("select * from [Sheet1$]", conn);
odda.Fill(ds);
conn.Close();
return ds;
}
}
}

解析excel表格为DataSet

标签:

原文地址:http://www.cnblogs.com/zhang-wenbin/p/5773595.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!