nopi 简洁笔记
导出excel
/// <summary>
/// 增加二维码
/// </summary>
/// <param name="dt"></param>
/// <param name="isexcel"></param>
protected void AddPicture(DataTable dt, bool isexcel = true)
{ if (!Directory.Exists(imageBasePath))
{
Directory.CreateDirectory(imageBasePath);
}
if (!Directory.Exists(excelBasePath))
{
Directory.CreateDirectory(excelBasePath);
}
//创建工作薄
HSSFWorkbook workbook = new HSSFWorkbook();
//create sheet
HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet("二维码打印");
string FileName ="二维码"+DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";
string strQrCodePath = "";
//填充列标题以及样式
int rowsNum = 0; //行号
// HSSFRow headerRow = (HSSFRow)sheet.CreateRow(rowsNum);
//HSSFCellStyle headStyle = (HSSFCellStyle)workbook.CreateCellStyle();
// headStyle.Alignment = (HorizontalAlignment)HorizontalAlignment.Center;
//设置列宽,excel列宽每个像素是1/256
//设置行高 ,excel行高度每个像素点是1/20 // headerRow.HeightInPoints = 0; // 表头高度
//HSSFFont font = (HSSFFont)workbook.CreateFont();
//font.FontHeightInPoints = 16;
//font.Boldweight = 100;
// headStyle.SetFont(font);
//只需要一列 2016-3-24 宽度像素是1/256
var widths = (int)(0.5 * 256);
sheet.SetColumnWidth(0, 14*256);//*256 1厘米=96/2.54≈37.8像素。
sheet.SetColumnWidth(1, widths);
sheet.SetColumnWidth(2, 25 * 256);//*256 1厘米=96/2.54≈37.8像素。
//填充数据行
HSSFRow row = null;
rowsNum = 0;
int rowindex = 0;
//设置下初始值
int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0, col1 = 0, row1 = rowindex, col2 = 0, row2 = rowindex;
string qrcodestr = "";
string msg = "";
int hight = 0;
HSSFCellStyle cellStyle = (HSSFCellStyle)workbook.CreateCellStyle();//设置样式
HSSFFont cellfont = (HSSFFont)workbook.CreateFont(); //设置字体
cellStyle.Alignment = HorizontalAlignment.Left;
cellStyle.VerticalAlignment = VerticalAlignment.Top;//对齐方式
cellStyle.WrapText = true;//设置换行这个要先设置
cellfont.FontHeightInPoints = 10;//字体大小
cellfont.Boldweight = 200;//字体厚度
foreach (DataRow dr in dt.Rows)
{
//循环列的次数 只要五列 先加第一行为空 高度是实在的像素
row = (HSSFRow)sheet.CreateRow(rowsNum);
//row.HeightInPoints = 47;
row.HeightInPoints = 102;//111(int)3.92 * 38; 148
row.CreateCell(0, CellType.String).SetCellValue("");
rowindex++;
rowsNum++;
for (int i = 0; i < 5; i++)
{
// cellfont = (HSSFFont)workbook.CreateFont();
row = (HSSFRow)sheet.CreateRow(rowsNum);
var cell = row.CreateCell(2, CellType.String);
//赋值都是第一列 0
if (dr[i]!=dr["二维码"])//只要不是二维码字符串就跳过
{
if (dr[i].Equals(dr["商品名称"]))
{
cellfont.FontHeightInPoints = 9;
cellfont.Boldweight =200;
row.HeightInPoints = 33.75f;
cellStyle.SetFont(cellfont);
cell.CellStyle = cellStyle;
cell.SetCellValue(dr[i].ToString());
}
if (dr[i].Equals(dr["产地"]))
{
cellfont.FontHeightInPoints = 12;
cellfont.Boldweight = 200;
row.HeightInPoints = 33.75f;
cellStyle.SetFont(cellfont);
cell.CellStyle = cellStyle;
cell.SetCellValue(dr[i].ToString());
}
if (dr[i].Equals(dr["规格"]))
{
cellfont.Boldweight = 200;
cellfont.FontHeightInPoints = 12;
row.HeightInPoints = 33.75f;
cellStyle.SetFont(cellfont);
cell.CellStyle = cellStyle;
cell.SetCellValue(dr[i].ToString());
}
if (dr[i].Equals(dr["清关类型"]))
{
cellfont.FontHeightInPoints = 13;
cellfont.Boldweight = 700;
row.HeightInPoints = 33.75f;
cellStyle.SetFont(cellfont);
cell.CellStyle = cellStyle;
cell.SetCellValue(dr[i].ToString());
} }
else
{
row.HeightInPoints = 105;//高度是1/20
if (isexcel)
{
WebClient myWebClient = new WebClient();
string imagepathfix = imageBasePath + Guid.NewGuid().ToString();
string pfx = ".png";
if (dr[i].ToString().LastIndexOf(".jpg") > 0)
{
pfx = ".jpg";
}
else
{
}
string oldimagpath = imagepathfix + "1" + pfx;
strQrCodePath = imagepathfix + "." + pfx;
try
{
myWebClient.DownloadFile(dr[i].ToString(), oldimagpath);
}
catch (Exception exx)
{
msg += dr["商品名称"].ToString() + " 的二维码有问题," + exx.Message;
continue;
}
//处理下二维码大小
if (!string.IsNullOrEmpty(TextBox1.Text))
{
int.TryParse(TextBox1.Text.Trim(), out hight);
}
if (hight == 0)
hight = 102;
ImageClass.MakeThumbnail(oldimagpath, strQrCodePath, hight, hight, "H");
}
else
{
qrcodestr = "http://www.unions3.com/vshop/ProductDetails.aspx?productid="
+ dr[i].ToString();
strQrCodePath = GetQrCode(qrcodestr, false);
}
byte[] bytes = File.ReadAllBytes(strQrCodePath);
int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG);
// Create the drawing patriarch. This is the top level container for all shapes.
HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
row1 = rowindex;
row2 = rowindex;
//add a picture
#region nopi创建画图
// 通常,利用NPOI画图主要有以下几个步骤:
//1.创建一个Patriarch;
//2.创建一个Anchor,以确定图形的位置;
//3.调用Patriarch创建图形;
//4.设置图形类型(直线,矩形,圆形等)及样式(颜色,粗细等)。
//关于HSSFClientAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2)的参数,有必要在这里说明一下:
//dx1:起始单元格的x偏移量,如例子中的255表示直线起始位置距A1单元格左侧的距离;
//dy1:起始单元格的y偏移量,如例子中的125表示直线起始位置距A1单元格上侧的距离;
//dx2:终止单元格的x偏移量,如例子中的1023表示直线起始位置距C3单元格左侧的距离;
//dy2:终止单元格的y偏移量,如例子中的150表示直线起始位置距C3单元格上侧的距离;
//col1:起始单元格列序号,从0开始计算;
//row1:起始单元格行序号,从0开始计算,如例子中col1 = 0,row1 = 0就表示起始单元格为A1;
//col2:终止单元格列序号,从0开始计算;
//row2:终止单元格行序号,从0开始计算,如例子中col2 = 2,row2 = 2就表示起始单元格为C3;
#endregion
//(int)3.07*38 (int)3.92*38
dx1 = 0; dy1 = 0; dx2 = (int)3.07 * 38; dy2 = (int)3.92 * 38; col1 = 1; row1 = rowindex; col2 = 1; row2 = rowindex;
HSSFClientAnchor anchor = new HSSFClientAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2);
HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx);
pict.Resize();
}
rowindex++;
rowsNum++;
} //删除图片文件
if (File.Exists(strQrCodePath))
{
File.Delete(strQrCodePath);
}
}
//供浏览器下载Excel
if (HttpContext.Current.Request.Browser.Browser == "IE")
FileName = HttpUtility.UrlEncode(FileName);
using (MemoryStream ms = new MemoryStream())
{
workbook.Write(ms);
ms.Flush();
ms.Position = 0;
HttpContext curContext = HttpContext.Current;
// 设置编码和附件格式
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.ContentEncoding = Encoding.UTF8;
curContext.Response.Charset = "";
curContext.Response.AppendHeader("Content-Disposition",
"attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8));
curContext.Response.BinaryWrite(ms.GetBuffer());
ms.Close();
ms.Dispose();
// curContext.Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
//}
//catch (Exception ex)
//{
if (!string.IsNullOrEmpty(msg))
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "key", "<script>alert('" + msg + "');</script>");
}
else { } //}
}
/// <summary> /// 增加二维码 /// </summary> /// <param name="dt"></param> /// <param name="isexcel"></param> protected void AddPicture(DataTable dt, bool isexcel = true) {
if (!Directory.Exists(imageBasePath)) { Directory.CreateDirectory(imageBasePath); } if (!Directory.Exists(excelBasePath)) { Directory.CreateDirectory(excelBasePath); } //创建工作薄 HSSFWorkbook workbook = new HSSFWorkbook(); //create sheet HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet("二维码打印"); string FileName ="二维码"+DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls"; string strQrCodePath = ""; //填充列标题以及样式 int rowsNum = 0; //行号 // HSSFRow headerRow = (HSSFRow)sheet.CreateRow(rowsNum); //HSSFCellStyle headStyle = (HSSFCellStyle)workbook.CreateCellStyle(); // headStyle.Alignment = (HorizontalAlignment)HorizontalAlignment.Center; //设置列宽,excel列宽每个像素是1/256 //设置行高 ,excel行高度每个像素点是1/20
// headerRow.HeightInPoints = 0; // 表头高度 //HSSFFont font = (HSSFFont)workbook.CreateFont(); //font.FontHeightInPoints = 16; //font.Boldweight = 100; // headStyle.SetFont(font); //只需要一列 2016-3-24 宽度像素是1/256 var widths = (int)(0.5 * 256); sheet.SetColumnWidth(0, 14*256);//*256 1厘米=96/2.54≈37.8像素。 sheet.SetColumnWidth(1, widths); sheet.SetColumnWidth(2, 25 * 256);//*256 1厘米=96/2.54≈37.8像素。 //填充数据行 HSSFRow row = null; rowsNum = 0; int rowindex = 0; //设置下初始值 int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0, col1 = 0, row1 = rowindex, col2 = 0, row2 = rowindex; string qrcodestr = ""; string msg = ""; int hight = 0; HSSFCellStyle cellStyle = (HSSFCellStyle)workbook.CreateCellStyle();//设置样式 HSSFFont cellfont = (HSSFFont)workbook.CreateFont(); //设置字体 cellStyle.Alignment = HorizontalAlignment.Left; cellStyle.VerticalAlignment = VerticalAlignment.Top;//对齐方式 cellStyle.WrapText = true;//设置换行这个要先设置 cellfont.FontHeightInPoints = 10;//字体大小 cellfont.Boldweight = 200;//字体厚度 foreach (DataRow dr in dt.Rows) { //循环列的次数 只要五列 先加第一行为空 高度是实在的像素 row = (HSSFRow)sheet.CreateRow(rowsNum); //row.HeightInPoints = 47; row.HeightInPoints = 102;//111(int)3.92 * 38; 148 row.CreateCell(0, CellType.String).SetCellValue(""); rowindex++; rowsNum++; for (int i = 0; i < 5; i++) { // cellfont = (HSSFFont)workbook.CreateFont(); row = (HSSFRow)sheet.CreateRow(rowsNum); var cell = row.CreateCell(2, CellType.String); //赋值都是第一列 0 if (dr[i]!=dr["二维码"])//只要不是二维码字符串就跳过 { if (dr[i].Equals(dr["商品名称"])) { cellfont.FontHeightInPoints = 9; cellfont.Boldweight =200; row.HeightInPoints = 33.75f; cellStyle.SetFont(cellfont); cell.CellStyle = cellStyle; cell.SetCellValue(dr[i].ToString()); } if (dr[i].Equals(dr["产地"])) { cellfont.FontHeightInPoints = 12; cellfont.Boldweight = 200; row.HeightInPoints = 33.75f; cellStyle.SetFont(cellfont); cell.CellStyle = cellStyle; cell.SetCellValue(dr[i].ToString()); } if (dr[i].Equals(dr["规格"])) { cellfont.Boldweight = 200; cellfont.FontHeightInPoints = 12; row.HeightInPoints = 33.75f; cellStyle.SetFont(cellfont); cell.CellStyle = cellStyle; cell.SetCellValue(dr[i].ToString()); } if (dr[i].Equals(dr["清关类型"])) { cellfont.FontHeightInPoints = 13; cellfont.Boldweight = 700; row.HeightInPoints = 33.75f; cellStyle.SetFont(cellfont); cell.CellStyle = cellStyle; cell.SetCellValue(dr[i].ToString()); } } else { row.HeightInPoints = 105;//高度是1/20 if (isexcel) { WebClient myWebClient = new WebClient(); string imagepathfix = imageBasePath + Guid.NewGuid().ToString(); string pfx = ".png"; if (dr[i].ToString().LastIndexOf(".jpg") > 0) { pfx = ".jpg"; } else { } string oldimagpath = imagepathfix + "1" + pfx; strQrCodePath = imagepathfix + "." + pfx; try { myWebClient.DownloadFile(dr[i].ToString(), oldimagpath); } catch (Exception exx) { msg += dr["商品名称"].ToString() + " 的二维码有问题," + exx.Message; continue; } //处理下二维码大小 if (!string.IsNullOrEmpty(TextBox1.Text)) { int.TryParse(TextBox1.Text.Trim(), out hight); } if (hight == 0) hight = 102; ImageClass.MakeThumbnail(oldimagpath, strQrCodePath, hight, hight, "H"); } else { qrcodestr = "http://www.unions3.com/vshop/ProductDetails.aspx?productid=" + dr[i].ToString(); strQrCodePath = GetQrCode(qrcodestr, false); } byte[] bytes = File.ReadAllBytes(strQrCodePath); int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG); // Create the drawing patriarch. This is the top level container for all shapes. HSSFPatriarch patriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch(); row1 = rowindex; row2 = rowindex; //add a picture #region nopi创建画图 // 通常,利用NPOI画图主要有以下几个步骤: //1.创建一个Patriarch; //2.创建一个Anchor,以确定图形的位置; //3.调用Patriarch创建图形; //4.设置图形类型(直线,矩形,圆形等)及样式(颜色,粗细等)。 //关于HSSFClientAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2)的参数,有必要在这里说明一下: //dx1:起始单元格的x偏移量,如例子中的255表示直线起始位置距A1单元格左侧的距离; //dy1:起始单元格的y偏移量,如例子中的125表示直线起始位置距A1单元格上侧的距离; //dx2:终止单元格的x偏移量,如例子中的1023表示直线起始位置距C3单元格左侧的距离; //dy2:终止单元格的y偏移量,如例子中的150表示直线起始位置距C3单元格上侧的距离; //col1:起始单元格列序号,从0开始计算; //row1:起始单元格行序号,从0开始计算,如例子中col1 = 0,row1 = 0就表示起始单元格为A1; //col2:终止单元格列序号,从0开始计算; //row2:终止单元格行序号,从0开始计算,如例子中col2 = 2,row2 = 2就表示起始单元格为C3; #endregion //(int)3.07*38 (int)3.92*38 dx1 = 0; dy1 = 0; dx2 = (int)3.07 * 38; dy2 = (int)3.92 * 38; col1 = 1; row1 = rowindex; col2 = 1; row2 = rowindex; HSSFClientAnchor anchor = new HSSFClientAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2); HSSFPicture pict = (HSSFPicture)patriarch.CreatePicture(anchor, pictureIdx); pict.Resize(); } rowindex++; rowsNum++; }
//删除图片文件 if (File.Exists(strQrCodePath)) { File.Delete(strQrCodePath); } } //供浏览器下载Excel if (HttpContext.Current.Request.Browser.Browser == "IE") FileName = HttpUtility.UrlEncode(FileName); using (MemoryStream ms = new MemoryStream()) { workbook.Write(ms); ms.Flush(); ms.Position = 0; HttpContext curContext = HttpContext.Current; // 设置编码和附件格式 curContext.Response.ContentType = "application/vnd.ms-excel"; curContext.Response.ContentEncoding = Encoding.UTF8; curContext.Response.Charset = ""; curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8)); curContext.Response.BinaryWrite(ms.GetBuffer()); ms.Close(); ms.Dispose(); // curContext.Response.End(); HttpContext.Current.ApplicationInstance.CompleteRequest(); } //} //catch (Exception ex) //{ if (!string.IsNullOrEmpty(msg)) { Page.ClientScript.RegisterStartupScript(this.GetType(), "key", "<script>alert('" + msg + "');</script>"); } else
{
}
//} }
nopi 简洁笔记的更多相关文章
- C#的多线程简洁笔记
New Thread(()=>{}).Start(); //匿名线程 Thread Ntd = new Thread(T1); Ntd.IsBackground = true; //后台线程 N ...
- Golang笔记(一)简洁的语言风格
Golang笔记(一)简洁的语言风格 概述 Golang继承了很多C语言的风格,寡人使用了十几年C语言,切换到Golang时上手很快,并且随着深入的使用,越来越喜欢这门语言.Golang最直观的感受是 ...
- sass笔记-3|Sass基础语法之样式复用和保持简洁
上一篇详述了Sass如何嵌套.导入和注释这3个基本方式来保持条理性和可读性,这一篇更进一步地阐述sass保持样式复用和简洁的方式--混合器和选择器继承--这两种方式都能复用样式,使用它们也不难,但一定 ...
- Java基础知识强化之集合框架笔记48:产生10个1~20之间的随机数(要求:随机数不能重复) 简洁版
1. 编写一个程序,获取10个1至20的随机数,要求随机数不能重复. 分析: A: 创建随机数对象 B: 创建一个HashSet集合 C: 判断集合的长度是不是小于10 是:就创建一个随机 ...
- Python笔记之不可不练
如果您已经有了一定的Python编程基础,那么本文就是为您的编程能力锦上添花,如果您刚刚开始对Python有一点点兴趣,不怕,Python的重点基础知识已经总结在博文<Python笔记之不可不知 ...
- Python笔记之不可不知
Python软件已经安装成功有很长一段时间了,也即或多或少的了解Python似乎也很长时间了,也是偏于各种借口,才在现在开始写点总结.起初接触Python是因为公司项目中需要利用Python来测试开发 ...
- Sass学习笔记之入门篇
Sass又名SCSS,是CSS预处理器之一,,它能用来清晰地.结构化地描述文件样式,有着比普通 CSS 更加强大的功能. Sass 能够提供更简洁.更优雅的语法,同时提供多种功能来创建可维护和管理的样 ...
- 在线课程笔记—.NET基础
关于学习北京理工大学金旭亮老师在线课程的笔记. 介绍: 在线课程网址:http://mooc.study.163.com/university/BIT#/c 老师个人网站:http://jinxuli ...
- Effective Java笔记一 创建和销毁对象
Effective Java笔记一 创建和销毁对象 第1条 考虑用静态工厂方法代替构造器 第2条 遇到多个构造器参数时要考虑用构建器 第3条 用私有构造器或者枚举类型强化Singleton属性 第4条 ...
随机推荐
- phpcms栏目点击选中
点击选中(没有二级栏目) {pc:content action="category" catid="0" num="4" siteid=&q ...
- CentOS 7 搭建CA认证中心实现https取证
CA认证中心简述 CA :CertificateAuthority的缩写,通常翻译成认证权威或者认证中心,主要用途是为用户发放数字证书 功能:证书发放.证书更新.证书撤销和证书验证. 作用:身份认证, ...
- spring boot + vue + element-ui全栈开发入门——集成element-ui
一.IDE开发工具 常用的开发工具有webstorm和sublime. 我个人喜好用Atom+插件的形式 打开Atom,在file --> settings --> packages中收 ...
- Oarcle之视图
视图 什么是视图? (在 SQL 中,视图是基于 SQL 语句的结果集的可视化的表. 视图包含行和列,就像一个真实的表.视图中的字段就是来自一个或多个数据库中的真实的表中的字段.我们可以向视图添加 S ...
- MySQL MHA 报错处理
安装环境:CentOS 6.5 MySQL 5.7.22 MHA 0.56 1.找不到mysql 命令 Sat Mar 23 07:17:50 2019 - [info] Connecting to ...
- laravel 分页使用
分页 //对数据库结果分页 $users = DB::table('users')->paginate(15); //可以对 Eloquent 模型分页: $allUsers = User::p ...
- Codeforces Round #466 (Div. 2) -A. Points on the line
2018-02-25 http://codeforces.com/contest/940/problem/A A. Points on the line time limit per test 1 s ...
- react-router 4.0(一)
import React from 'react'; import ReactDOM from 'react-dom' import {Link,Route,HashRouter} from 'rea ...
- Linux 修改SWAP分区后导致开机问题
Linux 系统出现提示原因 扩容后,修改了SWAP,或者安装了双 Linux 系统,在安装后一种 Linux 系统时把 SWAP分区 重新格式化,导致UUID 改变,所以启动时无法加载原来对应UUI ...
- Learning-Python【10】:函数初识
一.什么是函数 函数就是代码的一种组织形式,是指将一组语句的集合通过一个名字(函数名)封装起来,要想指向这个函数,只需要调用其函数名即可 函数分为两大类:内置函数 和 自定义函数 二.为何要用函数 减 ...