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

NPOI导出导入数据

时间:2014-07-09 17:08:52      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   strong   文件   数据   

       注意:以下均为服务器控件

       //导入数据

        protected void btnImport_Click(object sender, EventArgs e)
        {
            if (this.fileImport.HasFile)//判断fileImport控件中是否有需要上传的文件
            {

                //判断文件类型是否符合要求
                if (Path.GetExtension(this.fileImport.FileName) != ".xls" && Path.GetExtension(this.fileImport.FileName) != ".xlsx")

               {
                    MessageTips("上传的文件类型不符合要求", MessageTipsType.Error);
                    return;
                }
                DataTable dt = GetImportInfo();//将Excel中的文件读取到dt中

                if (dt != null && dt.Rows.Count > 0)
                {
                    BOBasicInfoBusiness.InsertASIList(dt);//上传文件
                    MessageTips("上传成功", MessageTipsType.Success);
                }
            }
            else
            {
                MessageTips("请选择导入模板", MessageTipsType.Error);
            }
        }

        protected void btnModel_Click(object sender, EventArgs e)
        {
            HSSFWorkbook hssfworkbook = new HSSFWorkbook();
            HSSFSheet sheet1 = null;

            //蓝色
            HSSFCellStyle hssBlue = (HSSFCellStyle)hssfworkbook.CreateCellStyle();
            hssBlue.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.GOLD.index;
            hssBlue.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.GOLD.index;
            hssBlue.FillPattern = FillPatternType.SQUARES;

            //边框
            hssBlue.BorderBottom = NPOI.SS.UserModel.BorderStyle.THIN;
            hssBlue.BorderLeft = NPOI.SS.UserModel.BorderStyle.THIN;
            hssBlue.BorderRight = NPOI.SS.UserModel.BorderStyle.THIN;
            hssBlue.BorderTop = NPOI.SS.UserModel.BorderStyle.THIN;

            HSSFFont font = (HSSFFont)hssfworkbook.CreateFont();
            font.Boldweight = 600;
            font.FontHeight = 200;
            hssBlue.SetFont((HSSFFont)font);

            sheet1 = (HSSFSheet)hssfworkbook.CreateSheet("SI编码维护列表");
            HSSFRow row = (HSSFRow)sheet1.CreateRow(0);

            ICell cell0 = row.CreateCell(0);
            cell0.SetCellValue("SI编码维护列表");
            cell0.CellStyle = (ICellStyle)hssBlue;
            row.Height = 300;

            HSSFRow row1 = (HSSFRow)sheet1.CreateRow(1);

            ICell cell1 = row1.CreateCell(0);
            cell1.SetCellValue("SI编码");
            sheet1.SetColumnWidth(0, 5000);

            //填充信息(将数据库中的数据填入dt中)
            DataTable dt = BOBasicInfoBusiness.GetASIList();
            if (dt != null || dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    HSSFRow rowtemp = (HSSFRow)sheet1.CreateRow(i + 2);

                    ICell celltemp = rowtemp.CreateCell(0);
                    celltemp.SetCellValue(dt.Rows[i]["SICode"].ToString());
                    celltemp.SetCellType(CellType.STRING);
                }
            }
            //保存修改的模板
            string savePath = this.MapPath("~/BaseInfo/Temp/SICodeList.xls");
            using (FileStream file = new FileStream(savePath, FileMode.Create))
            {
                hssfworkbook.Write(file);//写入数据至BaseInfo/Temp/SICodeList.xls

            }
            string path = this.MapPath("~/BaseInfo/Temp/SICodeList.xls");

            ExcelOutE(this, path);//打开或保存此路径下的文件
        }

        //将Excel中的文件读取到dt中

        private DataTable GetImportInfo()
        {
            DataTable dt = null;

            HSSFWorkbook workbook = new HSSFWorkbook(this.fileImport.FileContent);
            HSSFSheet sheet = (HSSFSheet)workbook.GetSheetAt(0);
            HSSFRow contentRow = (HSSFRow)sheet.GetRow(2);

            dt = new DataTable();
            dt.Columns.Add("SICode");

            for (int i = (sheet.FirstRowNum + 2); i < sheet.LastRowNum + 1; i++)
            {
                HSSFRow row = (HSSFRow)sheet.GetRow(i);
                //取得每一行的每一列的值
                string SICode = row.GetCell(0).ToString();

                //插入DataTable
                dt.Rows.Add(new string[] { SICode});
            }
            workbook = null;
            sheet = null;

            return dt;
        }

        //打开或保存此路径下的文件

        private void ExcelOutE(Page Page, string fileName)
        {
            HttpResponse Response = Page.Response;
            HttpServerUtility Server = Page.Server;

            string fn = Server.UrlDecode(fileName);
            FileStream fileStream = new FileStream(fn, FileMode.Open);
            long fileSize = fileStream.Length;
            fileStream.Close();
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            Response.AppendHeader("Content-Disposition", "attachment;filename=Sheet1.xls");
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.ContentType = "application/ms-excel";
            Response.AddHeader("Content-Length", fileSize.ToString());
            Page.EnableViewState = false;
            Response.WriteFile(fn);
            Response.Flush();
            Response.End();
        }

NPOI导出导入数据,布布扣,bubuko.com

NPOI导出导入数据

标签:style   http   color   strong   文件   数据   

原文地址:http://www.cnblogs.com/lijie20111015_20140708/p/3831975.html

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