Excel打印.二开案例.表单插件调用excel导出
【场景】插件调用excel打印
【案例】调用采购订单的excel打印,指定模板的预览
<1>数据库获取excel模板的内码
```sql
select * from t_bos_excelprint
--fbillformid,业务对象标识
```
<2>表单插件,在服务端导出文件以及在客户端导出文件
```csharp
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.Metadata.ElementMetadata;
using Kingdee.BOS.Core.NotePrint;
using Kingdee.BOS.ExcelPrint.Actions;
using Kingdee.BOS.ExcelPrint.Core;
using Kingdee.BOS.JSON;
using Kingdee.BOS.ServiceHelper.ExcelPrint;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace DynamicFormPlugIn.NotePrint
{
[Kingdee.BOS.Util.HotUpdate]
[System.ComponentModel.Description("excel打印 导出excel文件")]
public class ExcelSample_Export : AbstractBillPlugIn
{
public override void BarItemClick(BarItemClickEventArgs e)
{
base.BarItemClick(e);
object pkVal = this.View.Model.GetPKValue();
if (pkVal == null)
return;
List<string> billIds = new List<string>() { pkVal.ToString() };
if (e.BarItemKey.EqualsIgnoreCase("tb_ExportServer"))
{
ExportXlsOnServer(billIds);
return;
}
if (e.BarItemKey.EqualsIgnoreCase("tb_ExportClient"))
{
ExportXlsOnClient(billIds);
return;
}
}
private string ExcelPrint_TemplateId = "aea2be9f-f9ac-451a-81bf-968912a3d886";
/// <summary>
/// 在服务端导出excel文件
/// </summary>
private void ExportXlsOnServer(List<string> billIds)
{
//引用 Kingdee.BOS.ExcelPrint.Core
bool isMergePrint = false;
string strFilePath = PathUtils.GetPhysicalPath("TempFilePath", string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMdd HHmmss")));
//获取模板数据
var excelTemp = ExcelPrintServiceHelper.GetExcelTemplate(this.Context, new List<string>() { ExcelPrint_TemplateId });
Dictionary<string, string> tempBillDict = new Dictionary<string, string>();
tempBillDict[ExcelPrint_TemplateId] = string.Join(",", billIds);
//将单据数据填充到模板上
var excelBuffer = ExcelPrintUtils.FillBillDataToTemplateForExportAction(this.Context, this.View.BillBusinessInfo.GetForm().Id, excelTemp, null,
tempBillDict, isMergePrint, InvokeType.Server);
//保存文件
using (FileStream fs = new FileStream(strFilePath, FileMode.Create))
{
fs.Write(excelBuffer, 0, excelBuffer.Length);
}
}
/// <summary>
/// 在客户端导出excel文件
/// </summary>
private void ExportXlsOnClient(List<string> billIds)
{
JSONObject jo = new JSONObject();
jo.Add("exportType", (int)Kingdee.BOS.Core.NotePrint.ExportType.ByPage);
//Excel打印模板,单据内码集合映射
Dictionary<string, List<string>> tempBillDict = new Dictionary<string, List<string>>();
tempBillDict[ExcelPrint_TemplateId] = billIds;
DoBaseExcelPrintAction(ExcelPrintAction.Export, tempBillDict, jo);
}
/// <summary>
/// </summary>
/// <param name="action"></param>
/// <param name="dictPrint"></param>
/// <param name="objData"></param>
private void DoBaseExcelPrintAction(ExcelPrintAction action, Dictionary<string, List<string>> dictPrint, JSONObject objData = null)
{
JSONObject jsonBillID = new JSONObject();
jsonBillID.Add("Data", dictPrint);
if (action == ExcelPrintAction.Export || action == ExcelPrintAction.ExportMerge)
{
objData = objData ?? new JSONObject();
//判断是否按分录合并导出
object value;
objData.TryGetValue("exportType", out value, true);
ExportType type;
Enum.TryParse(value.GetString(), out type);
string dynamicFileName = string.Empty;
string hasDivFile = string.Empty; //自定义文件名
if (!hasDivFile.IsEmpty())
objData.Add("CustomExportFileName", hasDivFile);
if (type == ExportType.Merge)
{
action = ExcelPrintAction.ExportMerge;
}
else if (type == ExportType.BillTempId)
{
//按单模板分文件导出
action = ExcelPrintAction.ExportOneByOne;
if (!dynamicFileName.IsEmpty())
{
objData.Add("ExportDynamicFileName", dynamicFileName);
}
}
else
{
//默认方式导出
if (action == ExcelPrintAction.ExportMerge)
{
//只有分页账表导出所有页才是ExportMerge,需要特殊处理
action = ExcelPrintAction.Export;
}
}
}
//新增一个JSONObject对象,用于传递一些额外参数,如导出方式\自定义导出文件名. add by guangfan_chen
jsonBillID.Add("JSONObject", objData);
LoginExcelPrintParam loginParam = new LoginExcelPrintParam();
loginParam.ActionType = action;
loginParam.FormId = this.View.BillBusinessInfo.GetForm().Id;
loginParam.PageId = this.View.PageId;
loginParam.ActionNumber = this.GetType().Name;
loginParam.PrintParam = jsonBillID;
if (this.View.BusinessInfo.GetForm().ElementType == ElementType.ELEMENTTYPE_SYSREPORT)
{
//报表需要设置模板数,用于判断数据缓存是否需要清除
//每个模板对应的主键都一样,所以取第一个
string cacheKey = dictPrint.First().Value.First();
CacheUtil.SetCache(this.View.Context.GetAreaCacheKey(), "ExcelPrint", cacheKey + "Counter", dictPrint.Count);
}
ExcelPrintServiceHelper.SendExcelPrintAction(View, loginParam);
}
}
}
```
【效果】
<1>在服务端导出文件,可以指定文件生成目录和文件名
![server.webp](/download/010019ebbb1bf2334ce686c9608ed73b5385.webp)
<2>在客户端导出文件,直接弹出文件生成
![kehuduan.webp](/download/0100e97469ebc47b40879f3a1be20060dfc7.webp)
Excel打印.二开案例.表单插件调用excel导出
【场景】插件调用excel打印【案例】调用采购订单的excel打印,指定模板的预览<1>数据库获取excel模板的内码```sqlselect * from t_bos_e...
点击下载文档
本文2024-09-23 04:12:27发表“云星空知识”栏目。
本文链接:https://wenku.my7c.com/article/kingdee-k3cloud-163814.html
您需要登录后才可以发表评论, 登录登录 或者 注册
最新文档
热门文章