最近做了一个用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