.Net core_Excel 导出二维码(以导出箱单为例)
[accesslogattribute(note = "导出条形码箱单 — 条形码")]
public actionresult exportcontract(string inner_no)
{
try
{
string export = "条形码箱单.xls";
commonresult cr = new commonresult();
dataset ds = new dataset();
//获得表头数据
datatable dthead1 = new datatable();
//获得方法 具体参数可以根据需要传
dthead1 = tab1servcie.qrgetinvoicefirsthead(inner_no, currentcorporation.site);
//获得表体数据
datatable dtlist1 = new datatable();
dtlist1 = tab1servcie.qrgetinvoicefirstlist(inner_no, currentcorporation.site);
ds.tables.add(dthead1);
ds.tables.add(dtlist1);
//导出(需要配置xml)
ds.tables[0].tablename = "export_invoice_qr_head";
ds.tables[1].tablename = "export_invoice_qr_list";
dictionary<string, list<exportitem>> dicmapping = filehelper.getmappingdictionary(this, "wexport.xml", false, "export_invoice_qr");
dictionary<string, string> dicsheetname = new dictionary<string, string>();
dicsheetname.add("export_invoice_qr", "装箱单");
return step3(export.substring(0, export.length - 4), ds, dicmapping, dicsheetname, getfilephysicalpath("/content/exporttemplate/dec/" + export, false), inner_no);
}
catch (exception e)
{
commonresult cr = new commonresult();
cr.errormessage = e.message;
return json(cr);
}
}
/// <summary>
/// 打印二维码
/// </summary>
/// <param name="filename"></param>
/// <param name="ds"></param>
/// <param name="dicmapping"></param>
/// <param name="dicsheetname"></param>
/// <param name="tempaltepath"></param>
/// <param name="inner_no">内部编号(数据的标识编号)</param>
/// <returns></returns>
public actionresult step3(string filename, dataset ds, dictionary<string, list<exportitem>> dicmapping, dictionary<string, string> dicsheetname, string tempaltepath, string inner_no)
{
string path1 = "";
commonresult commonresult = new commonresult();
try
{
//地址
string a = path.getextension(tempaltepath);
commonresult.errormessage = npoihelper.saveexcel(filename, npoihelper.exporttomemorystream(ds, dicmapping, dicsheetname, tempaltepath, false), path.getextension(tempaltepath));
commonresult.success = true;
commonresult.data1 = appdomain.currentdomain.basedirectory + commonresult.errormessage;
path1 = commonresult.data1;
commonresult.errormessage = basecontroller.host + basecontroller.applicationpath + commonresult.errormessage;
filestream file = new filestream(@commonresult.data1, filemode.open, fileaccess.readwrite);
hssfworkbook excelbook = new hssfworkbook(file);
isheet sheet = excelbook.getsheet("装箱单");
list<pidmshnotifythird> lists = tab1servcie.getthirdlist(inner_no, currentcorporation.site);
//拆分 生产日期 生产批号 数量
image image;
int width =200, height = 60;
int lastrow = lists.count * 30;
for (int k = 0; k < lastrow; k++)
{
//创建行 (如果不创建会出现二维码积压覆盖)
irow row = sheet.createrow(28 + k); //创建行对象 --注意这点
row.heightinpoints = (float)(0.91 * 15.00);
for (int line = 0; line < 13; line++)
{
icell cell1 = row.createcell(line); //在第二行中创建单元格
icellstyle style = excelbook.createcellstyle();
}
}
//自适应界面
//for (int i = 0; i < lists.count; i++)
//{
// int _row = 16;
// int currline = _row + i * 4;
// int rowcount = sheet.lastrownum;
// if (i % 9 == 0 && i != 0)
// {
// currline = currline + 4;
// }
// excelbook.setprintarea(0, 0, 10, 0, 30);
// //是否自适应界面
// sheet.fittopage = false;
//}
excelbook.setprintarea(0, 0, 10, 0, lastrow);
//是否自适应界面
sheet.fittopage = false;
int result = 28;
for (int i = 0; i < lists.count; i++)
{
string[] strcount = null;
if (lists[i].net_wt_sep!=null)
{
strcount = lists[i].net_wt_sep.split(','); //数量分批
}
string[] strno = null;
if (lists[i].prod_batch_no_sep!=null)
{
strno = lists[i].prod_batch_no_sep.split(','); //生产批号分批
}
string[] strdate = null;
if (lists[i].produce_date!=null)
{
strdate = lists[i].produce_date.split(','); //生产日期分批
}
if (strcount.length== strno.length&& strno.length== strdate.length&& strno.length!=0 && strdate!=null && strno != null && strcount != null)
{
for (int j = 0; j < strcount.length; j++)
{
//[display(name = "客户货号")]
//public string note { get; set; }
if (lists[i].note != null)
{
byte[] note = getbarcode(height, width, barcodelib.type.code128, lists[i].note, out image);
sheet.getrow((j+1) * 4 + result).getcell(1).setcellvalue(lists[i].note);
int pictureidx = excelbook.addpicture(note, picturetype.png);
hssfpatriarch patriarch = (hssfpatriarch)sheet.createdrawingpatriarch();
hssfclientanchor anchor = new hssfclientanchor(100, 50, 950, 0, 0, result+1 + (j + 1) * 4, 2, result+3 + (j + 1) * 4);
hssfpicture pict = (hssfpicture)patriarch.createpicture(anchor, pictureidx);
}
// string[] strno = lists[i].prod_batch_no_sep.split(','); //生产批号分批
if (strno[j]!= null)
{
byte[] prod = getbarcode(height, width, barcodelib.type.code128, strno[j], out image);
sheet.getrow((j + 1) * 4 + result).getcell(4).setcellvalue(strno[j]);
int pictureidx1 = excelbook.addpicture(prod, picturetype.png);
hssfpatriarch patriarch1 = (hssfpatriarch)sheet.createdrawingpatriarch();
hssfclientanchor anchor1 = new hssfclientanchor(300, 50, 900, 0, 3, result + 1+ (j + 1) * 4, 5, result + 3 + (j + 1) * 4);
hssfpicture pict1 = (hssfpicture)patriarch1.createpicture(anchor1, pictureidx1);
}
// string[] strcount = lists[i].net_wt_sep.split(','); //数量分批
if (strcount[j] != null)
{
byte[] net_no = getbarcode(height, width, barcodelib.type.code128, strcount[j], out image);
sheet.getrow((j + 1) * 4 + result).getcell(6).setcellvalue(strcount[j]);
int pictureidx2 = excelbook.addpicture(net_no, picturetype.png);
hssfpatriarch patriarch2 = (hssfpatriarch)sheet.createdrawingpatriarch();
hssfclientanchor anchor2 = new hssfclientanchor(300, 50, 500, 0, 6, result + 1 + (j + 1) * 4, 7, result + 3 + (j + 1) * 4);
hssfpicture pict2 = (hssfpicture)patriarch2.createpicture(anchor2, pictureidx2);
}
// string[] strdate = lists[i].produce_date.split(','); //生产日期分批
if (strdate[j] != null)
{
byte[] prodce = getbarcode(height, width, barcodelib.type.code128, strdate[j], out image);
sheet.getrow((j + 1) * 4 + result).getcell(9).setcellvalue(strdate[j]);
int pictureidx3 = excelbook.addpicture(prodce, picturetype.png);
hssfpatriarch patriarch3 = (hssfpatriarch)sheet.createdrawingpatriarch();
hssfclientanchor anchor3 = new hssfclientanchor(300, 50, 500, 0, 8, result + 1 + (j + 1) * 4, 10, result + 3 + (j + 1) * 4);
hssfpicture pict3 = (hssfpicture)patriarch3.createpicture(anchor3, pictureidx3);
}
if (j+1== strcount.length)
{
result = result + (j + 1) * 4;
}
}
}
}
string filen = "/temp/export/" + filename + commonhelper.getguid() + ".xls";
string path = appdomain.currentdomain.basedirectory + filen;
commonresult.data1 = path;
commonresult.errormessage = basecontroller.host + basecontroller.applicationpath + filen;
filestream files = new filestream(@path, filemode.append);
excelbook.write(files);
//释放文件流
file.close();
file.dispose();
files.close();
files.dispose();
}
catch (exception ex)
{
commonresult.errormessage = ex.message;
}
finally
{
}
return base.json(commonresult, 0);
}
static byte[] getbarcode(int height, int width, type type, string code, out image image)
{
image = null;
barcode b = new barcode();
b.backcolor = color.white;
b.forecolor = color.black;
b.includelabel = false;
b.alignment = alignmentpositions.center;
b.labelposition = labelpositions.bottomcenter;
b.imageformat = imageformat.jpeg;
font font = new font("verdana", 10f);
b.labelfont = font;
b.height = height;
b.width = width;
b.encode(type, code);
byte[] buffer = b.getimagedata(savetypes.jpg);
return buffer;
}