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

2022-12-05,,,,

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

  用上面方法生成二维码有个问题,当数据量特别大的时候就生成不了。后面换了zxing来生成二维码,生成的数据多一点

 public static Bitmap Create(string str)
{
EncodingOptions options = null;
BarcodeWriter writer = null; options = new QrCodeEncodingOptions
{
DisableECI = true,
CharacterSet = "UTF-8",
Margin = ,
Width = ,
Height =
};
writer = new BarcodeWriter();
writer.Format = BarcodeFormat.QR_CODE;
writer.Options = options;
return writer.Write(str);
}

生成出来的二维码有可能周围的空白处有点多,初步测试可能是由于信息量过大,二维码如果按照原始块间距生成的话会导致超过固定的大小,因此自动减小块间距导致生成的没有固定大小大,留有空白

利用仪器扫描一张图片中某个部分含有二维码,如果图片过大,我遇到过3000*2400左右大小的,用二维码解析根本解析不出来,只有按照二维码的位置进行剪切裁剪后再解析二维码,能实现

  /// <summary>
/// 剪裁 -- 用GDI+
/// </summary>
/// <param name="b">原始Bitmap</param>
/// <returns>剪裁后的Bitmap</returns>
public static Bitmap Cut(Bitmap b)
{
if (b == null)
{
return null;
}
int startX = b.Width * / ;
int startY = ;
int width = b.Width / ;
int height = b.Height / ;
try
{
Bitmap bmpOut = new Bitmap(width, height, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmpOut);
g.DrawImage(b, new Rectangle(, , width, height), new Rectangle(startX, startY, width, height), GraphicsUnit.Pixel);
g.Dispose();
return bmpOut;
}
catch
{
return null;
}
} /// <summary>
/// 剪裁 -- 用GDI+
/// </summary>
/// <param name="b">原始Bitmap</param>
/// <param name="StartX">开始坐标X</param>
/// <param name="StartY">开始坐标Y</param>
/// <param name="iWidth">宽度</param>
/// <param name="iHeight">高度</param>
/// <returns>剪裁后的Bitmap</returns>
public static Bitmap Cut(Bitmap b, int StartX, int StartY, int iWidth, int iHeight)
{
if (b == null)
{
return null;
}
int w = b.Width;
int h = b.Height;
if (StartX >= w || StartY >= h)
{
return null;
}
if (StartX + iWidth > w)
{
iWidth = w - StartX;
}
if (StartY + iHeight > h)
{
iHeight = h - StartY;
}
try
{
Bitmap bmpOut = new Bitmap(iWidth, iHeight, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(bmpOut);
g.DrawImage(b, new Rectangle(, , iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
g.Dispose();
return bmpOut;
}
catch
{
return null;
}
}

C#生成二维码,把二维码图片放入Excel中的相关教程结束。

《C#生成二维码,把二维码图片放入Excel中.doc》

下载本文的Word格式文档,以方便收藏与打印。