**工作中用到了二维码_生成二维码并导出到excel表.
这里完整做了一下,巩固知识。**
实现效果:在页面上输入字符串,后台处理生成对应的二维码,将二维码作为图片保存,并且在页面显示出来。
前期:用到三方插件ThoughtWorks.QRCode,下载引用。插件生成二维码的类已经很完善,在这里OOXX搬运下就行。
///
/// 根据提交的内容显示二维码
///
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 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
{
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