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条 ...
随机推荐
- hive sql执行的job在map时报java.lang.OutOfMemoryError的错误
较为详细且重要的一段报错信息是org.apache.hadoop.mapred.YarnChild: Error running child : java.lang.OutOfMemoryError: ...
- day04 一个简单的代码优化案例
import random punches = ['石头','剪刀','布'] computer_choice = random.choice(punches) user_choice = input ...
- 152. Maximum Product Subarray(动态规划)
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- C# 获取结构体的所有成员
读取结构体的所有成员(Engine为结构体) FieldInfo[] fieldInfos = typeof(Engine).GetFields();
- 爬虫----requests模块
一.介绍 #介绍:使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) #注意:requests库发送请求将网页内 ...
- daily start
2019/4/26 1. to summerize the merge experience, about makefile, about compile error analysis. 2. loo ...
- dubbo多网卡时,服务提供者的错误IP注册到注册中心导致消费端连接不上
使用了虚拟机之后,启动了dubbo服务提供者应用,又连了正式环境的注册中心: 一旦dubbo获取的ip错误后, 这种情况即使提供者服务停掉,目前dubbo没有能力清除这类错误的提供者: (需要修改源码 ...
- weblogic10补丁升级与卸载
1.首先将补丁包解压放在weblogic的utils/bsu/cache_dir文件夹下,如果没有该文件夹,则手动创建. 2.回到bsu目录,执行安装命令 C:\Oracle\Middleware\u ...
- 2、haproxy配置参数详解
代理相关配置参数 内容参考自马哥教育 HAProxy官方文档 https://cbonte.github.io/haproxy-dconv/2.0/configuration.html URI Syn ...
- Maven的pom配置文件
1.1 Maven的pom配置文件 1.1.1 pom文件内容和作用 Pom.xml文件用来设置项目的项目依赖.插件.项目版本等信息,其中必须的是xml的根元 ...