using System.Data; using System.Collections; using Aspose.Cells; using Estsh.Core.Models; using Estsh.Core.Repository.IRepositories; using Estsh.Core.Services.IServices; using Estsh.Core.Model.Result; /*************************************************************************************************** * * 更新人:sitong.dong * 描述:采购周计划条码导出业务类 * 修改时间:2022.06.22 * 修改日志:系统迭代升级 * **************************************************************************************************/ namespace Estsh.Core.Services { /// /// 采购周计划条码导出业务处理类 /// public class PurchaseExportService : BaseService, IPurchaseExportService { private readonly IPurchaseExportRepository repository; public PurchaseExportService(IPurchaseExportRepository _repository) : base(_repository) { repository = _repository; } #region 类局部变量 private const int IROWINTERVAL = 8; //上、下模板差值 private const int ICOLINTERVAL = 6; //左、右模板差值 private const int IFIRSTLETTER = 65; //起始字母值(‘A’ ASSIC value) private const int ISTANDARDCOUNT = 8; //每页打印箱数量 //private Excel.Application app; //private Excel.Workbooks wbs; //private Excel.Workbook wb; //private Excel.Worksheets wss; //private Excel.Worksheet ws; #endregion /// /// 获取要货单数据 /// /// public List getOrderList(String dtpSeDate) { return repository.getOrderList(dtpSeDate); } /// /// 获取要货供应商列表 /// /// public List geVendorList(String orderNo) { return repository.geVendorList(orderNo); } /// /// 导出采购周计划条码信息 /// /// /// /// /// public Hashtable ExportDataToExcel(string folder, string orderNo, string vendorCode, string excelTemplate) { DataTable dtBarcode = new DataTable(); Hashtable result = new Hashtable(); //dtBarcode = this.repository.GetBarcodeData(orderNo, vendorCode); //if (dtBarcode == null || dtBarcode.Rows.Count < 1) //{ // result.Add("log", "[" + orderNo + "]:此要货单没有任何条码数据,请确认输入!"); // result.Add("message", "[" + orderNo + "]:此要货单没有任何条码数据,请确认输入!"); // result.Add("flag", "error"); // return result; //} //ArrayList alSuNo = new ArrayList(); //ArrayList alSuPwd = new ArrayList(); //for (int i = 0; i < dtBarcode.Rows.Count; i++) //{ // string strSuNoTemp = dtBarcode.Rows[i]["vendor_code"].ToString().Trim().ToUpper(); // bool isFind = false; // foreach (string item in alSuNo) // { // if (item.Trim().ToUpper() == strSuNoTemp) // { // isFind = true; // break; // } // } // if (isFind) // { // continue; // } // string strSuPwd = this.repository.QuerySupplierPwd(strSuNoTemp); // if (string.IsNullOrEmpty(strSuNoTemp)) // { // result.Add("log", "[" + strSuNoTemp + "]:不存在此供应商数据或供应商密码未维护,请确认配置!"); // result.Add("message", "[" + strSuNoTemp + "]:不存在此供应商数据或供应商密码未维护,请确认配置!"); // result.Add("flag", "error"); // return result; // } // alSuPwd.Add(strSuPwd); // alSuNo.Add(strSuNoTemp); //} //string strZipPath = folder + DateTime.Now.Ticks.ToString() + "\\"; //if (!Directory.Exists(strZipPath)) //{ // Directory.CreateDirectory(strZipPath); //} //try //{ // for (int i = 0; i < alSuNo.Count; i++) // { // string strSuPwd = alSuPwd[i].ToString().Trim(); //su_pwd // string strSheetName = alSuNo[i].ToString().Trim(); //su_no // string strXlsName = orderNo + "-" + strSheetName + ".xls"; // string strXlsFullName = strZipPath + strXlsName; // string strZipName = orderNo + "-" + strSheetName + ".zip"; // string strZipFullName = strZipPath + strZipName; // DataRow[] drSuLoop = dtBarcode.Select("vendor_code='" + strSheetName + "'"); // if (drSuLoop.Length < 1) // { // continue; // } // Excel.Application app = new Excel.Application(); // app.Visible = false; // //打开模板文件,获得WorkBook对象 // Excel.Workbook workBook = app.Workbooks.Open(excelTemplate, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, // Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); // string strExportResult = this.WriteDataToExcel(drSuLoop, orderNo, strSheetName, strXlsFullName, workBook); // workBook.Close(null, null, null); // app.Workbooks.Close(); // app.Application.Quit(); // app.Quit(); // System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook); // System.Runtime.InteropServices.Marshal.ReleaseComObject(app); // workBook = null; // app = null; // GC.Collect(); // //压缩数据 // ZipHelper.Zip(strXlsFullName, strZipFullName, strSuPwd); // if (File.Exists(strZipFullName)) // { // File.Delete(strXlsFullName); // } // } // String zipFile = folder + DateTime.Now.Ticks.ToString()+".zip"; // ZipHelper.Zip(strZipPath, zipFile, ""); // if (Directory.Exists(strZipPath)) // { // Directory.Delete(strZipPath,true); // } // result.Add("fileName",zipFile); // result.Add("log", "[" + orderNo + "]:此要货单数据文件已成功导出!"); // result.Add("message", "[" + orderNo + "]:此要货单箱条码数据文件已生成!"); // result.Add("flag", "success"); //} //catch (Exception e) //{ // if (Directory.Exists(strZipPath)) // { // Directory.Delete(strZipPath, true); // } // result.Add("log", "[" + orderNo + "]:此要货单导出异常!" + e.Message); // result.Add("message", "[" + orderNo + "]:此要货单导出异常!" + e.Message); // result.Add("flag", "error"); //} return result; } /// /// 导出到Excel /// /// /// /// /// /// /// private string WriteDataToExcel(DataRow[] drLoop, string orderNo, string sheetName, string fileFullPath, Workbook workBook) { int iRecordCount = drLoop.Length; int iPageCount = iRecordCount % ISTANDARDCOUNT; if (iPageCount == 0) { iPageCount = iRecordCount / ISTANDARDCOUNT; } else { iPageCount = (iRecordCount / ISTANDARDCOUNT) + 1; } //Excel.Worksheet workSheetSn = (Excel.Worksheet)workBook.Sheets.get_Item(1); //workSheetSn.Activate(); //for (int j = 0; j < iPageCount; j++) //{ // int iCurrentPage = j + 1; // int iStartRecord = (iCurrentPage - 1) * ISTANDARDCOUNT; // int iEndRecord = (iCurrentPage * ISTANDARDCOUNT) - 1; // if (iEndRecord >= iRecordCount) // { // iEndRecord = iRecordCount - 1; // } // ((Excel.Worksheet)workBook.Worksheets.get_Item(iCurrentPage)).Copy(Missing.Value, workBook.Worksheets[iCurrentPage]); // Excel.Sheets m_objSheets = null; // Excel._Worksheet m_objSheet = null; // Excel.Range m_objRange = null; // m_objSheets = (Excel.Sheets)workBook.Worksheets; // m_objSheet = (Excel._Worksheet)(m_objSheets.get_Item(iCurrentPage)); // m_objSheet.Name = sheetName + "-" + iCurrentPage.ToString("0000"); // int iIndexInPage = 0; // for (int k = iStartRecord; k <= iEndRecord; k++) // { // iIndexInPage++; // int iRowDiff = (iIndexInPage + 1) / 2; // int iColDiff = (iIndexInPage + 1) % 2; // ArrayList alData = new ArrayList(); // DataRow drItem = (DataRow)drLoop[k]; // int iIndex = k + 1; // alData.Add(drItem["carton_no"].ToString().Trim()); //el_sno // alData.Add(drItem["qty"].ToString().Trim()); //el_qty // alData.Add(drItem["part_no"].ToString().Trim()); //el_no // alData.Add(drItem["lot_no"].ToString().Trim()); //lot_no // alData.Add(drItem["part_spec"].ToString().Trim()); //el_name // alData.Add(iIndex.ToString() + @"/" + iRecordCount.ToString()); //iBoxIndex/iBoxCount // alData.Add(orderNo); // int iVendorLength = drItem["vendor_name"].ToString().Trim().Length; // if (iVendorLength > 8) // { // iVendorLength = 8; // } // alData.Add(drItem["vendor_name"].ToString().Trim().Substring(0, iVendorLength)); //su_name // alData.Add(drItem["buy_no"].ToString().Trim()); //ch_no-->pur_no // string strColName = ((char)IFIRSTLETTER).ToString(); // if (iColDiff > 0) // { // strColName = ((char)(IFIRSTLETTER + ICOLINTERVAL)).ToString(); // } // int iColValue = (iRowDiff - 1) * IROWINTERVAL; // int iRowIndexFact = 0; // string strCellName = string.Empty; // #region Fill Excel content // iRowIndexFact = iColValue + 1; // strCellName = strColName + iRowIndexFact.ToString(); // m_objRange = m_objSheet.get_Range(strCellName, Missing.Value); // m_objRange.Value2 = "*" + alData[0].ToString() + "*"; // iRowIndexFact++; // strCellName = strColName + iRowIndexFact.ToString(); // m_objRange = m_objSheet.get_Range(strCellName, Missing.Value); // m_objRange.Value2 = "标签号:" + alData[0].ToString(); // iRowIndexFact++; // strCellName = strColName + iRowIndexFact.ToString(); // m_objRange = m_objSheet.get_Range(strCellName, Missing.Value); // m_objRange.Value2 = "零件号:" + alData[2].ToString(); // iRowIndexFact++; // strCellName = strColName + iRowIndexFact.ToString(); // m_objRange = m_objSheet.get_Range(strCellName, Missing.Value); // m_objRange.Value2 = "描述:" + alData[4].ToString(); // iRowIndexFact++; // strCellName = strColName + iRowIndexFact.ToString(); // m_objRange = m_objSheet.get_Range(strCellName, Missing.Value); // m_objRange.Value2 = "要货单:" + alData[6].ToString(); // iRowIndexFact++; // strCellName = strColName + iRowIndexFact.ToString(); // m_objRange = m_objSheet.get_Range(strCellName, Missing.Value); // m_objRange.Value2 = "采购单:" + alData[8].ToString(); // iRowIndexFact++; // strCellName = strColName + iRowIndexFact.ToString(); // m_objRange = m_objSheet.get_Range(strCellName, Missing.Value); // m_objRange.Value2 = "";// "状态:"; // strColName = ((char)(IFIRSTLETTER + 3)).ToString(); // if (iColDiff > 0) // { // strColName = ((char)(IFIRSTLETTER + 3 + ICOLINTERVAL)).ToString(); // } // iRowIndexFact = 0; // strCellName = string.Empty; // iRowIndexFact = iColValue + 2; // strCellName = strColName + iRowIndexFact.ToString(); // m_objRange = m_objSheet.get_Range(strCellName, Missing.Value); // m_objRange.Value2 = "数量:" + alData[1].ToString(); // iRowIndexFact++; // strCellName = strColName + iRowIndexFact.ToString(); // m_objRange = m_objSheet.get_Range(strCellName, Missing.Value); // m_objRange.Value2 = "批号:" + alData[3].ToString(); // iRowIndexFact++; // strCellName = strColName + iRowIndexFact.ToString(); // m_objRange = m_objSheet.get_Range(strCellName, Missing.Value); // m_objRange.Value2 = "箱号:" + alData[5].ToString(); // iRowIndexFact++; // strCellName = strColName + iRowIndexFact.ToString(); // m_objRange = m_objSheet.get_Range(strCellName, Missing.Value); // m_objRange.Value2 = "供应商:" + alData[7].ToString(); // strColName = ((char)(IFIRSTLETTER + 2)).ToString(); // if (iColDiff > 0) // { // strColName = ((char)(IFIRSTLETTER + 2 + ICOLINTERVAL)).ToString(); // } // iRowIndexFact = iColValue + 6; // strCellName = strColName + iRowIndexFact.ToString(); // m_objRange = m_objSheet.get_Range(strCellName, Missing.Value); // m_objRange.Value2 = "";// "供应商批号:"; // #endregion // } //} //if (File.Exists(fileFullPath)) //{ // File.Delete(fileFullPath); //} ////workBook.WritePassword = "111"; ////workBook.Password = "222"; //workBook.SaveAs(fileFullPath, Missing.Value, Missing.Value, Missing.Value, // Missing.Value, Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, // Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); //System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheetSn); //workSheetSn = null; return ""; } } }