阅读背景:

C#生成二维码,把二维码图片放入Excel中

来源:互联网 
  /// <summary>
        /// 把图片保存到excel中
        /// </summary>
        /// <param name="excelFilePath">目标Excel</param>
        /// <param name="imageFilePath">保存的图片</param>
        /// <param name="width">保存时图片宽度</param>
        /// <param name="height">保存时图片高度</param>
        /// <param name="col">Excel第几列开始放</param>
        /// <param name="row">Excel第几行开始放</param>
        public static void InsertImgToExcel(string excelFilePath, string imageFilePath,int width,int height,int col,int row)
        {
            try
            {
                FileStream fs = new FileStream(excelFilePath, FileMode.Open, FileAccess.ReadWrite);
                HSSFWorkbook hssfworkbook = new HSSFWorkbook(fs);
                ISheet sheet1 = hssfworkbook.GetSheetAt(0);

                //map the path to the img folder
                string imagesPath = imageFilePath;
                //create an image from the path
                System.Drawing.Image image = System.Drawing.Image.FromFile(imagesPath);
                MemoryStream ms = new MemoryStream();
                //pull the memory stream from the image (I need this for the byte array later)
                image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                //the drawing patriarch will hold the anchor and the master information
                IDrawing patriarch = sheet1.CreateDrawingPatriarch();
                //store the coordinates of which cell and where in the cell the image goes
                HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 100, 100, col, row, col+3, row+3);
                //types are 0, 2, and 3. 0 resizes within the cell, 2 doesn't
                anchor.AnchorType = 2;
                //add the byte array and encode it for the excel file
                int index = hssfworkbook.AddPicture(ms.ToArray(), PictureType.JPEG);
                IPicture pict = patriarch.CreatePicture(anchor, LoadImage(imagesPath, hssfworkbook));
                pict.Resize();//原图大小

                FileStream fs3 = new FileStream(excelFilePath, FileMode.OpenOrCreate);
                hssfworkbook.Write(fs3);
                fs3.Close();
                fs.Close();
}
  /// <summary>
        /// 把图片保存到excel中
  



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: