程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#生成二維碼,把二維碼圖片放入Excel中,

C#生成二維碼,把二維碼圖片放入Excel中,

編輯:C#入門知識

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>
        /// 生成二維碼圖片
        /// </summary>
        /// <param name="codeNumber">要生成二維碼的字符串</param>     
        /// <param name="size">大小尺寸</param>
        /// <returns>二維碼圖片</returns>
        public Bitmap Create_ImgCode(string codeNumber, int size)
        {
            //創建二維碼生成類
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            //設置編碼模式
            qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            //設置編碼測量度
            qrCodeEncoder.QRCodeScale = size;
            //設置編碼版本
            qrCodeEncoder.QRCodeVersion = 0;
            //設置編碼錯誤糾正
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            //生成二維碼圖片
            System.Drawing.Bitmap image = qrCodeEncoder.Encode(codeNumber);
            return image;
        }

  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved