stoneniqiu 阅读(311) 评论(0)

 常用NPOI导出数据到excel,但没有试过如何导出图片。NPOI最大的特点就是不依赖于Excel组件,服务端不需要安装Excel。在单元格中插入图片主要是用HSSFClientAnchor对象。他有8个参数。

 HSSFClientAnchor anchor = new HSSFClientAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2);

前面四个表示在单元格中两个点的位置,后面四个表示是哪个单元格。先看代码。

  public FileResult ExportAppsImg()
        {
            using (var db=new PortalDb())
            { 
            HSSFWorkbook workbook = new HSSFWorkbook();
            //创建一个sheet
            ISheet sheet1 = workbook.CreateSheet("sheet1");
            // 设置列宽,excel列宽每个像素是1/256
            sheet1.SetColumnWidth(0, 18 * 256);
            sheet1.SetColumnWidth(1, 18 * 256);
            IRow rowHeader = sheet1.CreateRow(0);//创建表头行
            rowHeader.CreateCell(0, CellType.STRING).SetCellValue("生产单号");
            rowHeader.CreateCell(1, CellType.STRING).SetCellValue("学/工号");
            rowHeader.CreateCell(2, CellType.STRING).SetCellValue("手机号");
            rowHeader.CreateCell(3, CellType.STRING).SetCellValue("单位");
            rowHeader.CreateCell(4, CellType.STRING).SetCellValue("预约类型");
            rowHeader.CreateCell(5, CellType.STRING).SetCellValue("车牌号");
            rowHeader.CreateCell(6, CellType.STRING).SetCellValue("颜色");
            rowHeader.CreateCell(7, CellType.STRING).SetCellValue("品牌");
            rowHeader.CreateCell(8, CellType.STRING).SetCellValue("工作证");
            rowHeader.CreateCell(9, CellType.STRING).SetCellValue("身份证正面");
            rowHeader.CreateCell(10, CellType.STRING).SetCellValue("身份证反面");
            rowHeader.CreateCell(11, CellType.STRING).SetCellValue("驾驶证正面");
            rowHeader.CreateCell(12, CellType.STRING).SetCellValue("驾驶证反面");
            rowHeader.CreateCell(13, CellType.STRING).SetCellValue("结婚证");
            rowHeader.CreateCell(14, CellType.STRING).SetCellValue("状态");
            rowHeader.CreateCell(15, CellType.STRING).SetCellValue("预约时间");
            rowHeader.CreateCell(16, CellType.STRING).SetCellValue("申请时间");
            var res = db.Appointments.ToList();
            if (res.Count > 0)
            {
                int rowline = 1;//从第二行开始(索引从0开始)
                for (int i = 0; i < res.Count; i++)
                {
                    IRow row = sheet1.CreateRow(rowline);
                    //设置行高 ,excel行高度每个像素点是1/20
                    row.Height = 80 * 20;
                    //填入生产单号
                    row.CreateCell(0, CellType.STRING).SetCellValue(res[i].Name);
                    row.CreateCell(1, CellType.STRING).SetCellValue(res[i].SchoolNumber);
                    row.CreateCell(2, CellType.STRING).SetCellValue(res[i].Mobile);
                    row.CreateCell(3, CellType.STRING).SetCellValue(res[i].School);
                    row.CreateCell(4, CellType.STRING).SetCellValue(res[i].AppointmentType.ToString());
                    row.CreateCell(5, CellType.STRING).SetCellValue(res[i].CardNumber);
                    row.CreateCell(6, CellType.STRING).SetCellValue(res[i].Color);
                    row.CreateCell(7, CellType.STRING).SetCellValue(res[i].Brand);
                    //将图片文件读入一个字符串
                    HSSFPatriarch patriarch = (HSSFPatriarch)sheet1.CreateDrawingPatriarch();
                    setPic(workbook,patriarch, res[i].WrokImg, sheet1, rowline, 8);
                    setPic(workbook, patriarch, res[i].IDCardImg, sheet1, rowline, 9);
                    setPic(workbook, patriarch, res[i].IDCardImgBack, sheet1, rowline, 10);
                    setPic(workbook, patriarch, res[i].DriveCardImg, sheet1, rowline, 11);
                    setPic(workbook, patriarch, res[i].DriveCardImgBack, sheet1, rowline, 12);
                    setPic(workbook, patriarch, res[i].MarryCardImg, sheet1, rowline, 13);
                    row.CreateCell(14, CellType.STRING).SetCellValue(res[i].State.ToString());
                    row.CreateCell(15, CellType.STRING).SetCellValue(res[i].VerifyTime.ToString());
                    row.CreateCell(16, CellType.STRING).SetCellValue(res[i].CreateTime.ToString());
                    rowline++;
                }
            }
            var path = Server.MapPath("/Content/Excel/预约申请表.xls");
            //把文件保存到网站目录下,记得先创建一个Excel文件夹
            using (Stream stream = System.IO.File.OpenWrite(path)) 
            {
                workbook.Write(stream);
            }
            return File(path, "application/vnd.ms-excel","预约申请表.xls");
            }
        }

        private void setPic(HSSFWorkbook workbook, HSSFPatriarch patriarch,string path, ISheet sheet, int rowline, int col)
        {
            if(string.IsNullOrEmpty(path))return;
            byte[] bytes = System.IO.File.ReadAllBytes(Server.MapPath(path));
            int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG);
            // 插图片的位置  HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2) 后面再作解释
            HSSFClientAnchor anchor = new HSSFClientAnchor(70, 10, 0, 0, col, rowline, col+1, rowline + 1);
            //把图片插到相应的位置
            HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);
        }

每一行只能有一个HSSFPatriarch对象,如果把它的创建放到了setPic方法中,那么一行只会出现一张图片,最后的图片会消掉之前的图片。再界面上方一个a标签即可下载:

<a class="btn btn-primary" href="@Url.Action("ExportAppsImg","Appointment")">导出</a>

效果图:

参考博客:

http://blog.csdn.net/pan_junbiao/article/details/39717443 -- NPOI使用手册

http://www.cnblogs.com/wei325/p/4748324.html