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 ;}