using Kingdee.BOS.App.Data; using Kingdee.BOS.Core; using Kingdee.BOS.Core.CommonFilter.ConditionVariableAnalysis; using Kingdee.BOS.Core.DynamicForm; using Kingdee.BOS.Core.DynamicForm.PlugIn.Args; using Kingdee.BOS.Core.Metadata.FormElement; using Kingdee.BOS.Core.Report; using Kingdee.BOS.Core.Report.PlugIn; using Kingdee.BOS.Orm.DataEntity; using Kingdee.BOS.Util; using NPOI.HSSF.Util; using NPOI.SS.Formula.Functions; using NPOI.SS.UserModel; using NPOI.SS.Util; using NPOI.XSSF.UserModel; using System; using System.ComponentModel; using System.Data; using System.IO; using System.Linq; using System.Security.Cryptography; namespace Pilot.Report.Exploitation.AccountsReceivable { /// /// 【表单插件】明细版-按钮触发 /// [Description("【表单插件】明细版-按钮触发-导出EXCEL"), HotUpdate] public class LiteVersionPlugIn2 : AbstractSysReportPlugIn { public override void BarItemClick(BarItemClickEventArgs e) { base.BarItemClick(e); // 判断 应收对账单【明细版】按钮 if (e.BarItemKey == "Test_Button_1") { string FPath = "应收账款对账单【明细版】.xlsx"; // 在临时文件目录,生成一个完整的文件名: C:\Program Files\Kingdee\K3Cloud\WebSite\...\JD.xls string filePath = PathUtils.GetPhysicalPath(KeyConst.TEMPFILEPATH, Path.GetFileName(FPath)); // 生成一个供用户下载文件的url地址: http:\\localhost\K3Cloud\...\JD.xls string fileUrl = PathUtils.GetServerPath(KeyConst.TEMPFILEPATH, Path.GetFileName(FPath)); //打开文件下载界面 DynamicFormShowParameter showParameter = new DynamicFormShowParameter(); showParameter.FormId = "BOS_FileDownload"; showParameter.OpenStyle.ShowType = ShowType.Modal; showParameter.CustomComplexParams.Add("url", fileUrl); //显示 this.View.ShowForm(showParameter); var reportModel = this.SysReportModel; //单据头 var rptTitles = reportModel.ReportTitles; string fClient = rptTitles.FirstOrDefault(a => a.TitleKey == "FClient").TitleValue.ToString(); string fDate = rptTitles.FirstOrDefault(a => a.TitleKey == "FDate").TitleValue.ToString(); string fProject = rptTitles.FirstOrDefault(a => a.TitleKey == "FProject").TitleValue.ToString(); string FState = rptTitles.FirstOrDefault(a => a.TitleKey == "FState").TitleValue.ToString(); //单据体 var list = reportModel.DataSource.Rows; string sqlTest = string.Format(@"/*dialect*/Select FAmount,FDate From MBBA_t_Cust100006 Where Month(FDATE) = Month(DATEADD(MONTH, -1, '{0}'))", fDate); var sqlTestList = DBUtils.ExecuteDynamicObject(Context, sqlTest); int Num = 0; decimal fAmount = 0; string fDate2 = ""; if (sqlTestList.Count() > 0) { Num = 1; fAmount = Math.Round(Convert.ToDecimal(sqlTestList[0]["FAmount"]),2); fDate2 = sqlTestList[0]["FDate"].ToString().Split(' ')[0]; } Main(filePath, fClient, fDate, fProject, list, Num, fAmount, fDate2, Context); } } /// /// 赋值EXCEL /// static void Main(string outputFilePath, string fClient, string fDate, string fProject, DataRowCollection list, int Num, decimal fAmount,string fDate2,Kingdee.BOS.Context Context) { #region Excel代码 string filePath = @"D:\KingdeeModel\对账单模板2.xlsx"; FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read); IWorkbook workbook = new XSSFWorkbook(file); file.Close(); //获取工作簿中的第一个工作表(索引为0) ISheet sheet = workbook.GetSheetAt(0); #endregion #region 单元格样式 // 创建一个单元格样式------------------------------- ICellStyle cellStyle = workbook.CreateCellStyle(); // 设置边框样式 cellStyle.BorderTop = BorderStyle.Thin; cellStyle.BorderBottom = BorderStyle.Thin; cellStyle.BorderLeft = BorderStyle.Thin; cellStyle.BorderRight = BorderStyle.Thin; // 设置水平居中对齐 cellStyle.Alignment = HorizontalAlignment.Center; // 设置垂直居中对齐 cellStyle.VerticalAlignment = VerticalAlignment.Center; // 创建一个单元格样式--------------------------------- ICellStyle cellStyle2 = workbook.CreateCellStyle(); // 设置边框样式 cellStyle2.BorderTop = BorderStyle.Thin; cellStyle2.BorderBottom = BorderStyle.Thin; cellStyle2.BorderLeft = BorderStyle.Thin; cellStyle2.BorderRight = BorderStyle.Thin; // 创建单元格样式--------------------------------------- ICellStyle style = workbook.CreateCellStyle(); // 设置边框样式 style.BorderTop = BorderStyle.Thin; style.BorderBottom = BorderStyle.Thin; style.BorderLeft = BorderStyle.Thin; style.BorderRight = BorderStyle.Thin; // 创建字体并设置为加粗 IFont font = workbook.CreateFont(); font.IsBold = true; // 设置字体加粗 // 将字体应用到单元格样式 style.SetFont(font); // 创建单元格样式--------------------------------------- ICellStyle style2 = workbook.CreateCellStyle(); // 创建字体并设置为加粗 IFont font2 = workbook.CreateFont(); font2.IsBold = true; // 设置字体加粗 // 将字体应用到单元格样式 style2.SetFont(font2); // 创建一个单元格样式------------------------------- ICellStyle style3 = workbook.CreateCellStyle(); // 设置边框样式 style3.BorderTop = BorderStyle.Thin; style3.BorderBottom = BorderStyle.Thin; style3.BorderLeft = BorderStyle.Thin; style3.BorderRight = BorderStyle.Thin; // 设置水平居中对齐 style3.Alignment = HorizontalAlignment.Center; // 设置垂直居中对齐 style3.VerticalAlignment = VerticalAlignment.Center; // 创建字体并设置为加粗 IFont font3 = workbook.CreateFont(); font3.IsBold = true; // 设置字体加粗 // 将字体应用到单元格样式 style3.SetFont(font3); #endregion #region 获取金额汇总数据 decimal YFHpriceSum = 0; decimal YFPpriceSum = 0; decimal priceSum = 0; for (var i = 0; i < list.Count; i++) { if (list[i]["Number"].ToString() == "合计") { YFHpriceSum = Math.Round(Convert.ToDecimal(list[i]["ShippedDebt"]), 2); YFPpriceSum = Math.Round(Convert.ToDecimal(list[i]["InvoicedDebt"]), 2); priceSum = Math.Round(Convert.ToDecimal(list[i]["ShippedDebt"]) + Convert.ToDecimal(list[i]["InvoicedDebt"]), 2); } } #endregion #region 四行一列修改/六行二列修改/十一行一列修改/十二行一列修改 IRow row = sheet.GetRow(3); IRow row2 = sheet.GetRow(5); IRow row22 = sheet.GetRow(6); IRow ro = sheet.GetRow(11); ICell cell = row.GetCell(0); ICell cell2 = row2.GetCell(1); ICell cell22 = row22.GetCell(8); ICell cel3 = ro.GetCell(0); IRow row3 = sheet.GetRow(10); ICell cell3 = row3.GetCell(0); //将新创建或获取的单元格的值修改 cell22.SetCellValue(""); var YFHpriceSumZW = ToChineseNumber(YFHpriceSum, Context); cell3.SetCellValue("贵公司截至 " + fDate + " 共欠我公司货款金额为:" + YFHpriceSum + "元【等于已发货欠款总额】(大写:"+ YFHpriceSumZW + ")"); //将新创建或获取的单元格的值修改 cell.SetCellValue("尊敬的 " + fClient + " 客户您好,我公司已根据合同/订单的要求向贵公司交付相关产品并请验收确认,鉴于我公司对贵公司已通知发货部分的交付义务已完成,"); cell2.SetCellValue(fClient); var fAmountZW = ToChineseNumber(fAmount, Context); if (Num > 0) { cel3.SetCellValue("其中上期(截止" + fDate2 + "共欠我公司货款金额为:" + fAmount + "元(大写:"+ fAmountZW + ")"); } else { cel3.SetCellValue(""); } #endregion #region 明细赋值 // 将字体应用到单元格样式 style2.SetFont(font2); int n = 14; for (var i = 0; i < list.Count; i++) { string date = string.IsNullOrWhiteSpace(list[i]["DATE"].ToString()) ? "" : list[i]["DATE"].ToString().Split(' ')[0]; string ShipmentsDate = string.IsNullOrWhiteSpace(list[i]["ShipmentsDate"].ToString()) ? "" : list[i]["ShipmentsDate"].ToString().Split(' ')[0]; string PaymentDate = string.IsNullOrWhiteSpace(list[i]["PaymentDate"].ToString()) ? "" : list[i]["PaymentDate"].ToString().Split(' ')[0]; string BillingDate = string.IsNullOrWhiteSpace(list[i]["BillingDate"].ToString()) ? "" : list[i]["BillingDate"].ToString().Split(' ')[0]; string ReturnDate = string.IsNullOrWhiteSpace(list[i]["ReturnDate"].ToString()) ? "" : list[i]["ReturnDate"].ToString().Split(' ')[0]; IRow rowi = sheet.CreateRow(13 + i); ICell c1 = rowi.CreateCell(0); c1.SetCellValue(list[i]["Number"].ToString()); c1.CellStyle = cellStyle; ICell c2 = rowi.CreateCell(1); c2.SetCellValue(""+date+""); c2.CellStyle = cellStyle2; ICell c3 = rowi.CreateCell(2); c3.SetCellValue(list[i]["CONTRACTNUMBER"].ToString()); c3.CellStyle = cellStyle2; ICell c4 = rowi.CreateCell(3); c4.SetCellValue(list[i]["ProjectName"].ToString()); c4.CellStyle = cellStyle2; ICell c5 = rowi.CreateCell(4); c5.SetCellValue(list[i]["SizeModel"].ToString()); c5.CellStyle = cellStyle2; ICell c6 = rowi.CreateCell(5); c6.SetCellValue(""); c6.CellStyle = cellStyle; //数量 if(!string.IsNullOrWhiteSpace(list[i]["FQty"].ToString())) { int FQty = Convert.ToInt32(list[i]["FQty"]); c6.SetCellValue("" + FQty + ""); c6.CellStyle = cellStyle; //数量 } ICell c7 = rowi.CreateCell(6); c7.SetCellValue(""); c7.CellStyle = cellStyle; if(!string.IsNullOrWhiteSpace(list[i]["FTaxPrice"].ToString())) { decimal FTaxPrice = Math.Round(Convert.ToDecimal(list[i]["FTaxPrice"]), 2); c7.SetCellValue("" + FTaxPrice + ""); c7.CellStyle = cellStyle; } ICell c8 = rowi.CreateCell(7); c8.SetCellValue(""); c8.CellStyle = cellStyle; if(!string.IsNullOrWhiteSpace(list[i]["FAllAmount"].ToString())) { decimal FAllAmount = Math.Round(Convert.ToDecimal(list[i]["FAllAmount"]), 2); c8.SetCellValue("" + FAllAmount + ""); c8.CellStyle = cellStyle; } ICell c9 = rowi.CreateCell(8); c9.SetCellValue(""); c9.CellStyle = cellStyle; if (!string.IsNullOrWhiteSpace(list[i]["OrderAmount"].ToString())) { decimal OrderAmount = Math.Round(Convert.ToDecimal(list[i]["OrderAmount"]), 2); c9.SetCellValue("" + OrderAmount + ""); c9.CellStyle = cellStyle; } ICell c11 = rowi.CreateCell(10); c11.SetCellValue(""); c11.CellStyle = cellStyle; if (!string.IsNullOrWhiteSpace(list[i]["ShipmentsNum"].ToString())) { int ShipmentsNum = Convert.ToInt32(list[i]["ShipmentsNum"]); c11.SetCellValue("" + ShipmentsNum + ""); c11.CellStyle = cellStyle; } ICell c12 = rowi.CreateCell(11); c12.SetCellValue(""); c12.CellStyle = cellStyle; if (!string.IsNullOrWhiteSpace(list[i]["ShipmentsAmount"].ToString())) { decimal ShipmentsAmount = Math.Round(Convert.ToDecimal(list[i]["ShipmentsAmount"]), 2); c12.SetCellValue("" + ShipmentsAmount + ""); c12.CellStyle = cellStyle; } ICell c14 = rowi.CreateCell(13); c14.SetCellValue(""); c14.CellStyle = cellStyle; if (!string.IsNullOrWhiteSpace(list[i]["ReturnNum"].ToString())) { int ReturnNum = Convert.ToInt32(list[i]["ReturnNum"]); c14.SetCellValue("" + ReturnNum + ""); c14.CellStyle = cellStyle; } ICell c15 = rowi.CreateCell(14); c15.SetCellValue(""); c15.CellStyle = cellStyle; if (!string.IsNullOrWhiteSpace(list[i]["ReturnAmount"].ToString())) { decimal ReturnAmount = Math.Round(Convert.ToDecimal(list[i]["ReturnAmount"]), 2); c15.SetCellValue("" + ReturnAmount + ""); c15.CellStyle = cellStyle; } ICell c19 = rowi.CreateCell(18); c19.SetCellValue(""); c19.CellStyle = cellStyle; if (!string.IsNullOrWhiteSpace(list[i]["PaymentAmount"].ToString())) { decimal PaymentAmount = Math.Round(Convert.ToDecimal(list[i]["PaymentAmount"]), 2); c19.SetCellValue("" + PaymentAmount + ""); c19.CellStyle = cellStyle; } ICell c20 = rowi.CreateCell(19); c20.SetCellValue(""); c20.CellStyle = cellStyle; if (!string.IsNullOrWhiteSpace(list[i]["ShippedDebt"].ToString())) { decimal ShippedDebt = Math.Round(Convert.ToDecimal(list[i]["ShippedDebt"]), 2); c20.SetCellValue("" + ShippedDebt + ""); c20.CellStyle = cellStyle; } ICell c21 = rowi.CreateCell(20); c21.SetCellValue(""); c21.CellStyle = cellStyle; if (!string.IsNullOrWhiteSpace(list[i]["InvoicedDebt"].ToString())) { decimal InvoicedDebt = Math.Round(Convert.ToDecimal(list[i]["InvoicedDebt"]), 2); c21.SetCellValue("" + InvoicedDebt + ""); c21.CellStyle = cellStyle; } ICell c17 = rowi.CreateCell(16); c17.SetCellValue(""); c17.CellStyle = cellStyle; if (!string.IsNullOrWhiteSpace(list[i]["BillingAmount"].ToString())) { decimal BillingAmount = Math.Round(Convert.ToDecimal(list[i]["BillingAmount"]), 2); c17.SetCellValue("" + BillingAmount + ""); c17.CellStyle = cellStyle; } ICell c10 = rowi.CreateCell(9); c10.SetCellValue(ShipmentsDate); c10.CellStyle = cellStyle2;//发货日期 ICell c13 = rowi.CreateCell(12); c13.SetCellValue(ReturnDate); c13.CellStyle = cellStyle2;//退货日期 ICell c16 = rowi.CreateCell(15); c16.SetCellValue(BillingDate); c16.CellStyle = cellStyle2; //开票日期 ICell c18 = rowi.CreateCell(17); c18.SetCellValue(PaymentDate); c18.CellStyle = cellStyle2;//回款日期 ICell c22 = rowi.CreateCell(21); c22.SetCellValue(list[i]["IsEnded"].ToString()); c22.CellStyle = cellStyle2; ICell c23 = rowi.CreateCell(22); c23.SetCellValue(list[i]["Remark"].ToString()); c23.CellStyle = cellStyle2; n++; if(list[i]["Number"].ToString() == "合计") { c1.CellStyle = style3; c2.CellStyle = style3; c3.CellStyle = style3; c4.CellStyle = style3; c5.CellStyle = style3; c6.CellStyle = style3; c7.CellStyle = style3; c8.CellStyle = style3; c9.CellStyle = style3; c10.CellStyle = style3; c11.CellStyle = style3; c12.CellStyle = style3; c13.CellStyle = style3; c14.CellStyle = style3; c15.CellStyle = style3; c16.CellStyle = style3; c17.CellStyle = style3; c18.CellStyle = style3; c19.CellStyle = style3; c20.CellStyle = style3; c21.CellStyle = style3; c22.CellStyle = style3; c23.CellStyle = style3; } } #endregion #region 赋值汇总数据行 //获取工作簿中的第一个工作表(索引为1) ISheet sheet2 = workbook.GetSheetAt(1); IRow rr1 = sheet2.GetRow(15); IRow rr2 = sheet2.GetRow(16); IRow rr3 = sheet2.GetRow(17); ICell cc1 = rr1.GetCell(6); ICell cc2 = rr2.GetCell(6); ICell cc3 = rr3.GetCell(6); cc1.SetCellValue("" + YFHpriceSum + ""); cc2.SetCellValue("" + YFPpriceSum + ""); cc3.SetCellValue("" + priceSum + ""); #endregion #region 明细行后模板赋值 IRow R1 = sheet2.GetRow(15); IRow R2 = sheet2.GetRow(16); IRow R3 = sheet2.GetRow(17); IRow R4 = sheet2.GetRow(18); IRow R5 = sheet2.GetRow(19); IRow R6 = sheet2.GetRow(20); IRow R7 = sheet2.GetRow(21); IRow r1 = sheet.CreateRow(n); IRow r2 = sheet.CreateRow(n + 1); IRow r3 = sheet.CreateRow(n + 2); IRow r4 = sheet.CreateRow(n + 3); IRow r5 = sheet.CreateRow(n + 4); IRow r6 = sheet.CreateRow(n + 5); IRow r7 = sheet.CreateRow(n + 6); //第一行 for (int i = 0; i < R1.LastCellNum; i++) { var test1 = ""; if (R1.GetCell(i) != null) { test1 = R1.GetCell(i).ToString(); } ICell CR1 = r1.CreateCell(i); CR1.SetCellValue(test1); if (i == 4 || i == 5 || i == 6 || i == 7 || i == 8 || i == 9) { CR1.CellStyle = style; } else { CR1.CellStyle = style2; } } //第二行 for (int i = 0; i < R2.LastCellNum; i++) { var test1 = ""; if (R2.GetCell(i) != null) { test1 = R2.GetCell(i).ToString(); } ICell CR1 = r2.CreateCell(i); CR1.SetCellValue(test1); if (i == 4 || i == 5 || i == 6 || i == 7 || i == 8 || i == 9) { CR1.CellStyle = style; } else { CR1.CellStyle = style2; } } //第三行 for (int i = 0; i < R3.LastCellNum; i++) { var test1 = ""; if (R3.GetCell(i) != null) { test1 = R3.GetCell(i).ToString(); } ICell CR1 = r3.CreateCell(i); CR1.SetCellValue(test1); if (i == 4 || i == 5 || i == 6 || i == 7 || i == 8 || i == 9) { CR1.CellStyle = style; } else { CR1.CellStyle = style2; } } //第四行 for (int i = 0; i < R4.LastCellNum; i++) { var test1 = ""; if (R4.GetCell(i) != null) { test1 = R4.GetCell(i).ToString(); } ICell CR1 = r4.CreateCell(i); CR1.SetCellValue(test1); CR1.CellStyle = style2; } //第五行 for (int i = 0; i < R5.LastCellNum; i++) { var test1 = ""; if (R5.GetCell(i) != null) { test1 = R5.GetCell(i).ToString(); } ICell CR1 = r5.CreateCell(i); CR1.SetCellValue(test1); if (i >= 4) { CR1.CellStyle = style2; } } //第六行 for (int i = 0; i < R6.LastCellNum; i++) { var test1 = ""; if (R6.GetCell(i) != null) { test1 = R6.GetCell(i).ToString(); } ICell CR1 = r6.CreateCell(i); CR1.SetCellValue(test1); if (i >= 4) { CR1.CellStyle = style2; } } //第七行 for (int i = 0; i < R7.LastCellNum; i++) { var test1 = ""; if (R7.GetCell(i) != null) { test1 = R7.GetCell(i).ToString(); } ICell CR1 = r7.CreateCell(i); CR1.SetCellValue(test1); if (i >= 4) { CR1.CellStyle = style2; } } //合并单元格 for (int i = 0; i < 7; i++) { // 合并单元格的起始行、结束行、起始列和结束列 int firstRow = n + i; // 比如第一行 int lastRow = n + i; // 合并到第三行(0-based index) int firstCol = 4; // 比如第一列 int firstCol2 = 7; // 比如第一列 int firstCol3 = 0; // 比如第一列 int lastCol = 5; // 合并到第三列(0-based index) int lastCol2 = 9; // 合并到第三列(0-based index) int lastCol3 = 3; // 合并到第三列(0-based index) // 创建CellRangeAddress对象 CellRangeAddress cellRangeAddress = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol); CellRangeAddress cellRangeAddress2 = new CellRangeAddress(firstRow, lastRow, firstCol2, lastCol2); CellRangeAddress cellRangeAddress3 = new CellRangeAddress(firstRow, lastRow, firstCol3, lastCol3); // 合并单元格 sheet.AddMergedRegion(cellRangeAddress); sheet.AddMergedRegion(cellRangeAddress2); sheet.AddMergedRegion(cellRangeAddress3); } #endregion #region Excel代码2 string folderPath = Path.GetDirectoryName(outputFilePath); if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } FileStream exportFile = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write); workbook.Write(exportFile); exportFile.Close(); #endregion } public static string ToChineseNumber(decimal number, Kingdee.BOS.Context Context) { //Select top 1 dbo.ConvertAmountToChineseWords(12345.14) as 'PriceText' From T_SAL_ORDER string sql = string.Format(@"/*dialect*/Select top 1 dbo.ConvertAmountToChineseWords({0}) as 'PriceText' From T_SAL_ORDER", number); var list = DBUtils.ExecuteDynamicObject(Context, sql); return list[0]["PriceText"].ToString(); } } }