文档章节

从excel读取数据到datatable

o
 osc_4nmshwhm
发布于 2018/08/07 11:58
字数 150
阅读 12
收藏 0

 /// <summary>
        /// 
        /// 从excel读取数据到datatable
        /// using System.Windows.Forms;
        /// using System.Data.OleDb;
        /// using System.Threading;
        /// </summary>
        /// <param name="hasTitle">excel量是否有头</param>
        /// <returns></returns> 

public static  DataTable GetDataFromExcelByConn(bool hasTitle = true)         {

             OpenFileDialog openFile = new OpenFileDialog();            

      openFile.Filter = "Excel(*.xlsx)|*.xlsx|Excel(*.xls)|*.xls";            

      openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

              openFile.Multiselect = false;            

    Thread th = new Thread(new ThreadStart(delegate ()             {

                openFile.ShowDialog();

            }));            

    th.TrySetApartmentState(ApartmentState.STA);  

              th.Start();

              th.Join();            

            var filePath = openFile.FileName;

            string fileType = System.IO.Path.GetExtension(filePath);

            if (string.IsNullOrEmpty(fileType)) return null;

            using (DataSet ds = new DataSet())             {

            string strCon = string.Format("Provider=Microsoft.Jet.OLEDB.{0}.0;" + "Extended Properties=\"Excel {1}.0;HDR={2};IMEX=1;\";" +  "data source={3};",  (fileType == ".xls" ? 4 : 12), (fileType == ".xls" ? 8 : 12), (hasTitle ? "Yes" : "NO"), filePath);

                string strCom = " SELECT * FROM [Sheet1$]";

                using (OleDbConnection myConn = new OleDbConnection(strCon))

                using (OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn))                 {

                    myConn.Open();

                    myCommand.Fill(ds);

                }

                if (ds == null || ds.Tables.Count <= 0) return null;

                return ds.Tables[0];

            }

        }

o
粉丝 0
博文 500
码字总数 0
作品 0
私信 提问
加载中
请先登录后再评论。

暂无文章

如何在Ruby中生成随机字符串 - How to generate a random string in Ruby

问题: I'm currently generating an 8-character pseudo-random uppercase string for "A" .. "Z": 我目前正在为“ A” ..“ Z”生成一个8个字符的伪随机大写字符串: value = ""; 8.times{......

法国红酒甜
58分钟前
20
0
Python中的mkdir -p功能[重复] - mkdir -p functionality in Python [duplicate]

问题: This question already has an answer here: 这个问题在这里已有答案: How can I safely create a nested directory? 如何安全地创建嵌套目录? 25 answers 25个答案 Is there a way...

技术盛宴
今天
15
0
原价500元的认证证书,限时免费考取!

本文作者:y****n 百度云智学院致力于为百度ABC战略(人工智能、大数据、云计算)提供人才生态体系建设,包括基于百度ABC、IoT的课程体系,整合百度优势技术能力的深度学习技术、Apollo无人车...

百度开发者中心
昨天
17
0
在virtualenv中使用Python 3 - Using Python 3 in virtualenv

问题: Using virtualenv , I run my projects with the default version of Python (2.7). 使用virtualenv ,我使用默认版本的Python(2.7)运行项目。 On one project, I need to use Pyth......

富含淀粉
今天
16
0
Python的__init__和self是做什么的? - What __init__ and self do on Python?

问题: I'm learning the Python programming language and I've came across something I don't fully understand. 我正在学习Python编程语言,遇到了一些我不太了解的东西。 In a method ......

javail
今天
15
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部