NPOI动态获取数据源绑定Gridview显示

第一次用这个东西,还是比较顺利,用起来也比较好 记录一下



1.引用

using NPOI;
using NPOI.HSSF;
using NPOI.DDF;
using NPOI.POIFS;
using NPOI.HSSF.UserModel;
using NPOI.HPSF;
using NPOI.POIFS.FileSystem;

2.主要代码

  ///
        /// 绑定数据
        ///

        protected void BindExcelData()
        {

            //先上传 再打开

            HSSFWorkbook workbook = new HSSFWorkbook(this.fupFile.FileContent);

            HSSFSheet sheet = workbook.GetSheetAt(0);

            DataTable table = new DataTable();

            HSSFRow headerRow = sheet.GetRow(0);

            int cellCount = headerRow.LastCellNum;

            //一定要对单元格类型作判断 否则会报错 cannot get a string value from a numeric cell
            for (int i = headerRow.FirstCellNum; i < cellCount; i++)
            {
                if (headerRow.GetCell(i) == null || headerRow.GetCell(i).StringCellValue.Trim() == "")
                {
                    // 如果遇到第一个空列,则不再继续向后读取            
                    cellCount = i + 1;
                    break;
                }
                DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue);

                table.Columns.Add(column);
            }


            int rowCount = sheet.LastRowNum;

            for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++)
            {
                HSSFRow row = sheet.GetRow(i);
                if (row == null || row.GetCell(0) == null || row.GetCell(0).ToString().Trim() == "")
                {
                    // 如果遇到第一个空行,则不再继续向后读取                
                    break;
                }
                DataRow dataRow = table.NewRow();
                for (int j = row.FirstCellNum; j < cellCount - 1; j++)
                {
                    if (row.GetCell(j) != null)
                    {
                        dataRow[j] = row.GetCell(j).ToString();
                    }
                }
                table.Rows.Add(dataRow);
            }


            //workbook = null;
            //sheet = null;

            this.gdvList.DataSource = table;

            this.gdvList.AutoGenerateColumns = true;

            this.gdvList.DataBind();

            gdvList.DataKeyNames = new string[] { "IMEI" };

            this.gdvList.PageSize = 20000;

            lblCount.Text = gdvList.Rows.Count.ToString();
        }



3.要注意的地方

NPOI对日期格式的转换,显示出来的日期不是我们常用的yyyy-MM-dd格式,需要做特殊判断


       public static string ConvertDate(string date)
        {
        
            string[] orginDate = date.Trim().Split('-');

            string year = orginDate[2].ToString().Trim();

            string day = orginDate[1].ToString().Trim();

            string month = orginDate[0].ToString().Trim();

            if (day.Length == 1)
            {
                day = "0" + day.ToString();
            }

            if (month.Length == 1)
            {
                month = "0" + month.ToString().Trim();
            }

            if (year.Length == 2)
            {
                year = "20" + year.ToString().Trim();
            }


            return year + "-" + month + "-" + day;

        }





[本日志由 admin 于 2010-08-11 07:59 PM 编辑]
上一篇: Oracle EBS:SQL 失效一揽子PO协议
下一篇: 《22条商规》读后感
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags:
相关日志:
评论: 0 | 引用: 0 | 查看次数: 7735
发表评论
昵 称:
密 码: 游客发言不需要密码.
邮 箱: 邮件地址支持Gravatar头像,邮箱地址不会公开.
网 址: 输入网址便于回访.
内 容:
验证码:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 30 字 | UBB代码 关闭 | [img]标签 关闭