C # generate a QR code and put the QR code image in Excel,

Source: Internet
Author: User

C # generate a QR code and put the QR code image in Excel,

/// <Summary> /// Save the image to excel /// </summary> /// <param name = "excelFilePath"> Target Excel </param>/ // <param name = "imageFilePath"> saved image </param> /// <param name = "width"> width </param> /// <param name = "height"> image height when saved </param> /// <param name = "col"> columns of the Excel file </param> /// <param name = "row"> the number of rows in the Excel file </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 (); // source image size FileStream fs3 = new FileStream (excelFilePath, FileMode. openOrCreate); hssfworkbook. write (fs3); fs3.Close (); fs. close ();}

Generate QR code

/// <Summary> /// generate a QR code image /// </summary> /// <param name = "codeNumber"> string to generate the QR code </param>/ // <param name = "size"> size </param> /// <returns> QR code image </returns> public Bitmap Create_ImgCode (string codeNumber, int size) {// create a QR code generation class QRCodeEncoder qrCodeEncoder = new QRCodeEncoder (); // set the encoding mode qrCodeEncoder. QRCodeEncodeMode = QRCodeEncoder. ENCODE_MODE.BYTE; // sets the encoding measurement qrCodeEncoder. QRCodeScale = size; // set the encoding version qrCodeEncoder. QRCodeVersion = 0; // set the Encoding Error to correct qrCodeEncoder. QRCodeErrorCorrect = QRCodeEncoder. ERROR_CORRECTION.M; // generate a QR code image System. drawing. bitmap image = qrCodeEncoder. encode (codeNumber); return image ;}

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.