码迷,mamicode.com
首页 > Windows程序 > 详细

WPF中Grid绑定DataTable数据。

时间:2015-07-28 14:29:34      阅读:531      评论:0      收藏:0      [点我收藏+]

标签:

1.首先引用DocumentFormat.OpenXml.dll

2.然后新建一个OpenExcelHelper类,将Excel转化为Datatable。

        /// <summary>
        /// 根据指定EXCEL流组织成DataTable
        /// </summary>
        /// <param name="sheetName">需要读取的SHEET</param>
        /// <param name="stream">EXCEL文件流</param>
        /// <returns>DataTable</returns>
        public static  DataTable GetExcelData(string sheetName, string stream)
        {

            using (SpreadsheetDocument document = SpreadsheetDocument.Open(stream, false))
            {
                //打开Excel
                IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.Descendants<Sheet>().Where(s => s.Name == sheetName);
                if (sheets.Count() == 0)
                {
                    return null;
                }
                WorksheetPart worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(sheets.First().Id);
                //获取Excel中共享数据
                SharedStringTable sst = document.WorkbookPart.SharedStringTablePart.SharedStringTable;
                //得到Excel中得数据行
                IEnumerable<Row> rows = worksheetPart.Worksheet.Descendants<Row>();
                DataTable dt = new DataTable();
                //因为需要将数据导入到DataTable中,所以我们假定Excel的第一行是列名,从第二行开始是行数据
                foreach (Row row in rows)
                {
                    if (row.RowIndex == 1)
                    {
                        //Excel第一行为列名
                        GetDataColumn(row, sst, ref dt);
                    }
                    //Excel第二行同时为DataTable的第一行数据
                    GetDataRow(row, sst, ref dt);
                }
                return dt;
            }
        }
        private static void GetDataColumn(Row row, SharedStringTable sst, ref DataTable dt)
        {
            DataColumn dc = new DataColumn();
            foreach (Cell cell in row)
            {
                string cellValue = GetcellValue(cell, sst);
                dc = new DataColumn(cellValue);
                dt.Columns.Add(dc);
            }
        }
        /// <summary>
        /// 构建DataTable的列
        /// </summary>
        /// <param name="row">OpenXML定义的Row对象</param>
        /// <param name="sst"></param>
        /// <param name="dt">需要返回的DataTable对象</param>
        private static  void GetDataRow(Row row, SharedStringTable sst, ref DataTable dt)
        {
            DataRow dr = dt.NewRow();
            int i = 0;
            int nullRowCount = i;
            foreach (Cell cell in row)
            {
                string cellVal = GetcellValue(cell, sst);
                if (cellVal == string.Empty)
                {
                    nullRowCount++;
                }
                dr[i] = cellVal;
                i++;
            }
            if (nullRowCount != i)
            {
                dt.Rows.Add(dr);
            }

        }
        /// <summary>
        /// 获取单元格的值
        /// </summary>
        /// <param name="cell"></param>
        /// <param name="sst"></param>
        /// <returns></returns>
        private static  string GetcellValue(Cell cell, SharedStringTable sst)
        {
            //由于Excel的数据存储在SharedStringTable中,需要获取数据在SharedStringTable 中的索引
            string value = string.Empty;
            try
            {
                if (cell.ChildElements.Count == 0)
                {
                    return value;

                }
                value = double.Parse(cell.CellValue.InnerText).ToString();
                if ((cell.DataType != null) && (cell.DataType == CellValues.SharedString))
                {
                    value = sst.ChildElements[Int32.Parse(value)].InnerText;
                }
            }
            catch (Exception)
            {

                value = "N/A";
            }
            return value;

        }

3.页面对应的代码。

技术分享

4页面(AutoGenerateColumns一定要设置为true。AutoGenerateColumns 就是自动产生列的意思
gridview等控件,如果设置了 AutoGenerateColumns=true,就能够根据数据源的实际情况,自动生成gridview表格列,如果设置为 AutoGenerateColumns=flase,那么就必须要手动编写gridview控件的columns集合,否则即使正确帮定了数 据,gridvew也什么都不显示,因为没有定义列,同时也没有自动让他自己生成列)

技术分享

 



      

WPF中Grid绑定DataTable数据。

标签:

原文地址:http://www.cnblogs.com/qian3344/p/4682638.html

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