最近做了一个用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) return null;
int j = ds.Tables[0].Rows.Count;
a = j;
int k = j / 300;
if (k > 0)
{
for (int i = 0; i <= k; i++)
{
Directory.CreateDirectory("D:\\q\\" + i);
}
}
else
{
Directory.CreateDirectory("D:\\q\\" + 0);
}
List<String> list = new List<string>();
for (int i = 0; i < j; i++)
{
int l = i / 300;
//pictureBox1.Image= bmp;
//实例化一个生成二维码的对象
QRCodeEncoder qrEncoder = new QRCodeEncoder();
//设置二维码的编码模式
qrEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
//二维码像素宽度
qrEncoder.QRCodeScale = 5;
//设置版本
qrEncoder.QRCodeVersion = 0;
//根据内容生成二维码图像
string a = ds.Tables[0].Rows[i][0].ToString();
Bitmap image = null;
image = qrEncoder.Encode(a, Encoding.UTF8);
//pictureBox1.Image = image;
//pictureBox1.Height = image.Height;
//pictureBox1.Width = image.Width;
image.Save("D:\\q\\" + l + "\\" + a + ".png");
list.Add(a);
dataGridView1.RowTemplate.Height = image.Height + 10;
this.dataGridView1.Rows.Add(i + 1, a, image);
}
return ds.Tables[0];
}
}
2.把生成的二维码放在一张图片中
private void button4_Click(object sender, EventArgs e)
{
//把图片合并到一个图片中
//文件路径
for (int q = 0; q <= a / 300; q++)
{
string fs = "D:\\q\\" + q;
//不存在文件进行创建
if (!System.IO.Directory.Exists(fs)) System.IO.Directory.CreateDirectory(fs);
//存储到pic文件夹中文件
//获取到pic下面的jpg图片
string[] rs = System.IO.Directory.GetFiles(fs, "*.png");
//最大宽度和高度
int maL = 0, totalH = 0;
//循环遍历获取文件的最大宽度与总高度
for (int i = 0; i < rs.Length; i++)
{
Image image = Image.FromStream(new System.IO.MemoryStream(File.ReadAllBytes(rs[i])));
if (image.Width > maL) maL = image.Width;
totalH = totalH + image.Height + 10;
}
if (totalH == 0 || maL == 0) return;
Bitmap map = new Bitmap(2800, 3650);//定义画布自己调节适合自己打印机分辨率和纸张大小的尺寸
Graphics g = Graphics.FromImage(map);//定义画笔
g.Clear(Color.White);//把画布更改为白色
int y = 0;//y轴坐标
int x = 0;//x轴坐标
int y1 = 0;
int x1 = 0;
for (int i = 0; i < rs.Length; i++)
{
if (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;
三、部分代码有引用别人的代码如有侵权请联系删除