經常使用NPOI導出數據到excel,但沒有試過如何導出圖片。NPOI最大的特色就是不依賴於Excel組件,服務端不須要安裝Excel。在單元格中插入圖片主要是用HSSFClientAnchor對象。他有8個參數。html
HSSFClientAnchor anchor = new HSSFClientAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2);
前面四個表示在單元格中兩個點的位置,後面四個表示是哪一個單元格。先看代碼。app
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(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("申請時間"); var res = db.Appointments.ToList(); if (res.Count > 0) { int rowline = 1;//從第二行開始(索引從0開始) HSSFPatriarch patriarch = (HSSFPatriarch)sheet1.CreateDrawingPatriarch(); 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(GetEnumTxt(res[i].AppointmentType)); 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); //將圖片文件讀入一個字符串 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, 9); setPic(workbook, patriarch, res[i].DriveCardImgBack, sheet1, rowline, 10); setPic(workbook, patriarch, res[i].MarryCardImg, sheet1, rowline, 11); row.CreateCell(12, CellType.STRING).SetCellValue((GetEnumTxt(res[i].State))); row.CreateCell(13, CellType.STRING).SetCellValue(res[i].VerifyTime.ToString()); row.CreateCell(14, CellType.STRING).SetCellValue(res[i].CreateTime.ToString()); rowline++; } } var path = Server.MapPath("/Content/Excel/預定申請表.xls"); //把文件保存到d:\aaa.xls,注意擴展名是.xls不要寫成.xlsx using (Stream stream = System.IO.File.OpenWrite(path)) { workbook.Write(stream); } return File(path, "application/vnd.ms-excel","預定申請表.xls"); } } //獲取枚舉類型的Display特性的name值 public string GetEnumTxt(Enum eEnum) { var enumType = eEnum.GetType(); var field = enumType.GetField(eEnum.ToString()); var display = field.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault() as DisplayAttribute; return display != null ? display.Name : eEnum.ToString(); } 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方法中,那麼一行只會出現一張圖片,最後的圖片會消掉以前的圖片。也不能放到for循環裏。因此放在最上面。再界面上方一個a標籤便可下載:spa
<a class="btn btn-primary" href="@Url.Action("ExportAppsImg","Appointment")">導出</a>
效果圖:.net
參考博客:excel
http://blog.csdn.net/pan_junbiao/article/details/39717443 -- NPOI使用手冊code