C#将excel表格数据生成二维码并将二维码拼接在一张图片上

您所在的位置:网站首页 snap拼接图片 C#将excel表格数据生成二维码并将二维码拼接在一张图片上

C#将excel表格数据生成二维码并将二维码拼接在一张图片上

2023-06-08 01:41| 来源: 网络整理| 查看: 265

最近做了一个用C#读取excel数据生成二维码的需求,并且要固定数量生成在一张图片上方便打印 一、要使用的NuGet包,点击项目右键管理NuGet程序包搜索 1.Microsoft.Office.Interop.Excel 2.System.Data.OleDb 3.ThoughtWorks.QRCode.Standard 二、步骤 1.读取excel表格数据并生成二维码直接上代码

System.Data.DataTable GetDataFromExcelByConn(bool hasTitle = false) { OpenFileDialog openFile = new OpenFileDialog(); openFile.Filter = "Excel(*.xlsx)|*.xlsx|Excel(*.xls)|*.xls"; openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); openFile.Multiselect = false; if (openFile.ShowDialog() == DialogResult.Cancel) return null; var filePath = openFile.FileName; textBox1.Text = filePath; string fileType = System.IO.Path.GetExtension(filePath); if (string.IsNullOrEmpty(fileType)) return null; using (DataSet ds = new DataSet()) { //string strCon = string.Format("Provider=Microsoft.Jet.OLEDB.{0}.0;" + // "Extended Properties=\"Excel {1}.0;HDR={2};IMEX=1;\";" + // "data source={3};", // (fileType == ".xls" ? 4 : 12), (fileType == ".xls" ? 8 : 12), (hasTitle ? "Yes" : "NO"), filePath); string strCon = string.Empty; switch (fileType) { case ".xls": strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=Excel 8.0;"; break; case ".xlsx": strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=0;'"; break; default: strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=Yes;IMEX=0;'"; break; } string strCom = " SELECT * FROM [Sheet1$]"; using (OleDbConnection myConn = new OleDbConnection(strCon)) using (OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn)) { myConn.Open(); myCommand.Fill(ds); } if (ds == null || ds.Tables.Count 0) { for (int i = 0; i = 15) { int j = 0; j = i / 15; if (i % 15 == 0) { Image image = Image.FromStream(new System.IO.MemoryStream(File.ReadAllBytes(rs[i]))); g.DrawImage(image, new System.Drawing.Point(0, j * image.Height + j * 80)); //y1 = y1 + image.Height + 10;//y的告诉 5是为了让画布有个缝隙 x1 = 0; } else { Image image = Image.FromStream(new System.IO.MemoryStream(File.ReadAllBytes(rs[i]))); x1 = x1 + image.Width + 80;//y的告诉 5是为了让画布有个缝隙 g.DrawImage(image, new System.Drawing.Point(x1, j * image.Height + j * 80)); } } else { Image image = Image.FromStream(new System.IO.MemoryStream(File.ReadAllBytes(rs[i]))); g.DrawImage(image, new System.Drawing.Point(x, y)); x = x + image.Width + 80; //y = y + image.Height + 10;//y的告诉 5是为了让画布有个缝隙 } // Image image = Image.FromStream(new System.IO.MemoryStream(File.ReadAllBytes(rs[i]))); // g.DrawImage(image, new System.Drawing.Point(0, y)); // y = y + image.Height + 10;//y的告诉 5是为了让画布有个缝隙 } //把合并的图片进行保存为jpg格式 map.Save("D:\\q\\" + "\\total" + q + ".png", System.Drawing.Imaging.ImageFormat.Png); for (int i = 0; i < rs.Length; i++) { //删除原先的2个jpg图片 // File.Delete(rs[i]); } } }

合成图片有三个位置需要自己调整,一个是合成图片二维码左右上下间距(80),一个是图片像素即画布大小(2800, 3650),还有一个是生成二维码的像素(二维码像素宽度) 效果图因为是内部使用所以不是特别美观 在这里插入图片描述 打印的功能因为目前还不能实现多张图片连续打印,后续更新请关注 引用

using System.Data; using System.Text; using ThoughtWorks.QRCode.Codec; using System.Data.OleDb;

三、部分代码有引用别人的代码如有侵权请联系删除



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3