.Net core_Excel 導出二維碼(以導出箱單為例)

来源:https://www.cnblogs.com/ning-xiaowo/archive/2019/11/20/11898766.html
-Advertisement-
Play Games

[AccessLogAttribute(Note = "導出條形碼箱單 — 條形碼")]public ActionResult ExportContract(string INNER_NO){ try{string export = "條形碼箱單.xls";CommonResult cr = new ...


 


[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;
}

 

 

 

 

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 任何服務對資料庫的日常操作,都離不開增刪改查。如果一次查詢的紀錄很多,那我們必須採用分頁的方式。對於一個Springboot項目,訪問和查詢MySQL資料庫,持久化框架可以使用MyBatis,分頁工具可以使用github的 PageHelper。我們來看一下PageHelper的使用方法: 1 // ...
  • 作為程式員,在日常開發中,記憶猶新的莫過於寫代碼,升級程式。升級程式包含兩部分:一是,對服務程式更新;二是,對資料庫結構更新。本篇博文主要介紹資料庫結構更新,在對資料庫升級時,不知道園友們是否有如下經歷: 1)腳本文件中建表語句未作判斷是否存在,而導致執行失敗。 2)腳本文件中修改欄位在建表語句之前 ...
  • asp.net core 自定義 Policy 替換 AllowAnonymous 的行為 Intro 最近對我們的服務進行了改造,原本內部服務在內部可以匿名調用,現在增加了限制,通過 identity server 來管理 api 和 client,網關和需要訪問api的客戶端或api服務相互調用 ...
  • 交錯數組:數組元素本身也是一個數組 1 public static void Main(string[] args) 2 { 3 4 int[][] arr = new int[5][]; 5 arr[0] = new int[8]; 6 arr[1] = new int[8]; 7 arr[2] ...
  • 1. 概述反射 通過反射可以提供類型信息,從而使得我們開發人員在運行時能夠利用這些信息構造和使用對象。 反射機制允許程式在執行過程中動態地添加各種功能。 2. Type類的介紹 是BCL(基底類別庫)聲明的一個抽象類,所有它不能被實例化 對於程式中用到的每一個類型,CLR(公共語言運行時)都會創建一 ...
  • 最近新開發一個需要給App使用的API項目。開發API肯定會想到JASON Web Token(JWT)和OAuthor2(之前一篇隨筆記錄過OAuthor2)。 JWT和OAuthor2的比較 要像比較JWT和OAuthor2,首先要明白一點就是,這是兩個完全不同的東西,沒有可比性。 JWT是一種 ...
  • 通過前面的文章abp(net core)+easyui+efcore實現倉儲管理系統——EasyUI之貨物管理一 (十九) 至 abp(net core)+easyui+efcore實現倉儲管理系統——EasyUI之貨物管理八(二十六) 的學習,我們已經有實現了傳統的ASP.NET Core MVC... ...
  • 場景 使用DevExpress的TreeList顯示本磁碟下文件目錄併在樹節點上右鍵實現刪除與添加文件。 效果 自定義右鍵效果 實現 首先在包含Treelist的窗體的load方法中對treelist進行初始化 Common.DataTreeListHelper.RefreshTreeData(th ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...