Esempio n. 1
0
        /// <summary>
        /// 导出图片
        /// </summary>
        /// <param name="filePath">excel文件</param>
        /// <param name="sheetName">sheet名称</param>
        /// <param name="img">文件路径</param>
        /// <param name="mGOffet">设置项</param>
        public void ExportIMG(string filePath, string sheetName, string img, IMGOffet mGOffet)
        {
            HSSFWorkbook wb;
            FileStream   file;

            file = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read);
            wb   = new HSSFWorkbook(file);
            ISheet sheet = wb.GetSheet(sheetName);

            if (sheet == null)
            {
                wb.CreateSheet(sheetName);
            }
            HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();

            SetPic(wb, patriarch, img, sheet, mGOffet);
        }
Esempio n. 2
0
 private void SetPic(HSSFWorkbook workbook, HSSFPatriarch patriarch, string path, ISheet sheet, IMGOffet mGOffet)
 {
     if (string.IsNullOrEmpty(path))
     {
         return;
     }
     byte[] bytes      = System.IO.File.ReadAllBytes(path);
     int    pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG);
     // 插图片的位置  HSSFClientAnchor(dx1,dy1,dx2,dy2,col1,row1,col2,row2) 后面再作解释
     HSSFClientAnchor anchor = new HSSFClientAnchor(mGOffet.StartOffsetX, mGOffet.StartOffsetY, mGOffet.EndOffsetX, mGOffet.EndOffsetY, mGOffet.StartCellCol, mGOffet.StartCellRow, mGOffet.EndCellCol, mGOffet.EndCellRow);
     //把图片插到相应的位置
     HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);
 }