生成二维码并导出到excel表

该博客介绍了如何在工作中利用C#编程生成二维码,并将生成的二维码图片保存及展示。作者通过ThoughtWorks.QRCode插件实现二维码生成,然后将二维码图片整合到Excel表格中。虽然目前二维码在页面上的显示存在问题,但已能通过IIS访问保存的图片。此外,博客提供了相关资源链接以供进一步学习。


**工作中用到了二维码_生成二维码并导出到excel表.
这里完整做了一下,巩固知识。**

实现效果:在页面上输入字符串,后台处理生成对应的二维码,将二维码作为图片保存,并且在页面显示出来。

前期:用到三方插件ThoughtWorks.QRCode,下载引用。插件生成二维码的类已经很完善,在这里OOXX搬运下就行。

   /// <summary>
   /// 根据提交的内容显示二维码
   /// </summary>        
        protected void btn_Click(object sender, EventArgs e)
        {
            QRCodeEncoder enCoder = new QRCodeEncoder();
            enCoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
            enCoder.QRCodeBackgroundColor = Color.White;
            enCoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            enCoder.QRCodeScale = 4;//等级越大像素越高
            enCoder.QRCodeVersion = 7;
            //生成了二维码图片
            Bitmap map = enCoder.Encode(txtUrl.Text.Trim());
            //将图片保存在本地
            string filePath = @"F:\google下载\QRCode\";
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            string imgName=Guid.NewGuid().ToString().Replace("-","");
            string Path=filePath + imgName + ".png";
            map.Save(Path);            
            map.Dispose();
            img.ImageUrl = "http://****:55/" + imgName + ".png";

        }

效果:这里写图片描述
备注:生成二维码图片保存到本地后,我没有想到好的方法呈现在页面,只是在iis上 将保存图片的文件夹发出来在直接访问。对此,期待指点。

下面是将二维码导出到excel 表处理
//将数据整合生成excel

public static MemoryStream DealData(List<NewList> DataList)
        {
            HSSFWorkbook workbook = new HSSFWorkbook();
            ISheet sheet = workbook.CreateSheet("Sheet1");
            //10列
            sheet.SetColumnWidth(0, 8 * 256 + 200);
            sheet.SetColumnWidth(1, 8 * 256 + 200);
            sheet.SetColumnWidth(2, 8 * 256 + 200);
            sheet.SetColumnWidth(3, 8 * 256 + 200);
            sheet.SetColumnWidth(4, 8 * 256 + 200);
            sheet.SetColumnWidth(5, 8 * 256 + 200);
            sheet.SetColumnWidth(6, 8 * 256 + 200);
            sheet.SetColumnWidth(7, 8 * 256 + 200);
            sheet.SetColumnWidth(8, 8 * 256 + 200);
            sheet.SetColumnWidth(9, 20 * 256 + 200);//图片列

            //通用样式
            ICellStyle headStyle = workbook.CreateCellStyle();
            headStyle.Alignment = HorizontalAlignment.Left;
            headStyle.VerticalAlignment = VerticalAlignment.Center;
            IFont font = workbook.CreateFont();
            font.FontHeightInPoints = 10;
            font.Boldweight = 30;
            headStyle.SetFont(font);

            int rowIndex = 0;

            #region 表头及样式
            {
                IRow headerRow = sheet.CreateRow(rowIndex);
                headerRow.HeightInPoints = 20;                
                string[] Header = { "单位", "编码", "厂家", "型号", "功率", "启用日期", "操作人", "操作时间", "油机状态", "二维码" };
                for (int i = 0; i < Header.Length; i++)
                {
                    headerRow.CreateCell(i).SetCellValue(Header[i]);
                    headerRow.GetCell(i).CellStyle = headStyle;
                }
                rowIndex++;
            }
            #endregion
            #region 填充数据
            for (int j = 0; j <DataList.Count() ; j++)
            {
                IRow rows = sheet.CreateRow(rowIndex);
                rows.HeightInPoints = 110;//二维码原大小呈现,行高大
                int num = 0;//列指针
                rows.CreateCell(num).SetCellValue(DataList[j].Orgname);//单位
                rows.GetCell(num++).CellStyle = headStyle;
                rows.CreateCell(num).SetCellValue(DataList[j].Engine.Num);//编码
                rows.GetCell(num++).CellStyle = headStyle;
                rows.CreateCell(num).SetCellValue(DataList[j].Engine.Manufactor);//厂家
                rows.GetCell(num++).CellStyle = headStyle;
                rows.CreateCell(num).SetCellValue(DataList[j].Engine.Model);//型号
                rows.GetCell(num++).CellStyle = headStyle;
                rows.CreateCell(num).SetCellValue(DataList[j].Engine.Power.HasValue ? DataList[j].Engine.Power.ToString() : "");//功率
                rows.GetCell(num++).CellStyle = headStyle;
                rows.CreateCell(num).SetCellValue(DataList[j].Engine.EnableTime.HasValue ? DataList[j].Engine.EnableTime.ToString() : "");//启用日期
                rows.GetCell(num++).CellStyle = headStyle;
                rows.CreateCell(num).SetCellValue(DataList[j].realityName);//操作人
                rows.GetCell(num++).CellStyle = headStyle;
                rows.CreateCell(num).SetCellValue(DataList[j].Engine.DateInfo.ToString());//操作时间
                rows.GetCell(num++).CellStyle = headStyle;
                rows.CreateCell(num).SetCellValue(DataList[j].MachineStateText);//油机状态                
                rows.GetCell(num++).CellStyle = headStyle;
                //处理二维码图片
                AddPieChartMerage(sheet, workbook, DataList[j].Engine.QRCodePhoto, rowIndex, num, 0,true);
                rowIndex++;
            }
            #endregion

            using (MemoryStream ms = new MemoryStream())
            {
                workbook.Write(ms);
                ms.Flush();
                ms.Position = 0;
                sheet = null;
                workbook = null;
                return ms;
            }
        }

处理图片

///totalFileURL 图片完整路径  
 private static void AddPieChartMerage(ISheet sheet, HSSFWorkbook workbook, string totalFileURL, int row, int col, int MerageColCount,bool isResize=false)
        {
            try
            {
                HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
                //处理照片位置,【图片左上角为(col, row)第row+1行col+1列,右下角为( col +1, row +1)第 col +1+1行row +1+1列,宽为100,高为50
                HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 100, 50, col, row, col + 1, row + 1);
                int i = 0;
                foreach (var fileurl in totalFileURL.Split(';'))
                {
                    string path = fileurl;
                    byte[] bytes = System.IO.File.ReadAllBytes(path);
                    if (!string.IsNullOrEmpty(path))
                    {
                        int pictureIdx = workbook.AddPicture(bytes, NPOI.SS.UserModel.PictureType.JPEG);
                        anchor = new HSSFClientAnchor(i * 100, 0, i * 100 + 100, 0, col, row, col + 1 + MerageColCount, row + 1);

                        HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);
                        if (isResize)//是否显示图片原大小
                        {
                           pict.Resize();
                        }
                    }
                    i++;
                }
            }
            catch (Exception)
            {
                //throw ex;
            }
        }

效果:这里写图片描述

Mark:
生成待logo的二维码:http://www.jb51.net/article/104639.htm
二维码的编码/解码:https://www.cnblogs.com/xuhang/p/3832118.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值