PY插件生成生成指定格式的excel文件并且打包下载

栏目:云星空知识作者:金蝶来源:金蝶云社区发布:2024-09-16浏览:2

PY插件生成生成指定格式的excel文件并且打包下载

前言:很多时候需要生成一个指定格式的excel文件,并且组织相关的数据,C#很好实现,但是py找了一圈,基本上只有sql查询后再下载,先组织代码分享 遗留问题: 1、生成csv格式文件,不是excel 2、excel文件怎么设置行宽 ####### # 引入clr运行库 import clr import sys # 添加对cloud插件开发的常用组件的引用 clr.AddReference('System') clr.AddReference('System.Data') clr.AddReference('Kingdee.BOS') clr.AddReference('Kingdee.BOS.Core') clr.AddReference('Kingdee.BOS.App') clr.AddReference('Kingdee.BOS.DataEntity') clr.AddReference('Kingdee.BOS.Contracts') clr.AddReference('Kingdee.BOS.ServiceHelper') clr.AddReference('ICSharpCode.SharpZipLib') clr.AddReference('Newtonsoft.Json') clr.AddReference("System.Web.Extensions") clr.AddReference('NPOI') clr.AddReference('NPOI.OOXML') # 导入cloud基础库中的常用实体对象(分命名空间导入,不会递归导入) from Kingdee.BOS import * from Kingdee.BOS.App.Data import * from Kingdee.BOS.Core import * from Kingdee.BOS.Core.Attachment import * from Kingdee.BOS.Core.Bill import * from Kingdee.BOS.Core.DynamicForm import * from Kingdee.BOS.Core.DynamicForm.PlugIn import * from Kingdee.BOS.Core.DynamicForm.PlugIn.ControlModel import * from Kingdee.BOS.JSON import * from Kingdee.BOS.Core.Log import * from Kingdee.BOS.Core.Metadata import * from Kingdee.BOS.Contracts import * from Kingdee.BOS.DataEntity import * from Kingdee.BOS.Orm.DataEntity import * from Kingdee.BOS.Util import PathUtils from Kingdee.BOS.ServiceHelper import * from Kingdee.BOS.ServiceHelper.Excel import ExcelOperation from ICSharpCode.SharpZipLib.Checksums import Crc32 from ICSharpCode.SharpZipLib.Zip import ZipOutputStream, ZipEntry from NPOI.XSSF.UserModel import XSSFWorkbook from System import * from System import Array, Byte from System import Uri from System.Collections.Generic import List as CList from System.Collections.Generic import Dictionary from System.Collections.Generic import * from System.Data import * from System.IO import * from System.IO import FileInfo from System.IO import FileStream, FileMode from System.IO import MemoryStream from System.Net import * from System.Net import FtpWebRequest, FtpWebResponse, NetworkCredential from System.Text import * from System.Threading import * from System.Security.Cryptography import * from System.Web.Script.Serialization import * from Newtonsoft.Json import JsonConvert from Newtonsoft.Json.Linq import * reload(sys) sys.setdefaultencoding('utf-8') def BarItemClick(e): key = e.BarItemKey.ToUpperInvariant() if key == "REFQ_tbButton".ToUpperInvariant(): test_creat_file() # 将列表转换为CSV字符串 def convert_list_to_csv(data_list): headers = ['序号','物料编码','物料名称','物料规格','数量',"PDF","DWG","STEP"] result = ",".join(headers) + "\n" for item in data_list: row = [item[header] for header in headers] result += ",".join(row) + "\n" return result def test_creat_file(): zipFileName = "采购订单图纸下载%s.zip" % (DateTime.Now.ToString("yyyyMMddHHmm")) zipFilePath = PathUtils.GetPhysicalPath(KeyConst.TEMPFILEPATH, zipFileName) fsOut = File.Create(zipFilePath) zipStream = ZipOutputStream(fsOut) order_data = [{"采购订单号": "12345", "图纸清单": "图纸1, 图纸2, 图纸3", "日期时间": "2023-10-01 12:00:00"}, {"采购订单号": "67890", "图纸清单": "图纸4, 图纸5,", "日期时间": "2023-10-02 13:00:00"}] # 生成EXCCEL表格记录文件信息 fname = "采购订单图纸清单%s.csv" % (DateTime.Now.ToString("yyyyMMddHHmm")) fileUrl = PathUtils.GetServerPath(KeyConst.TEMPFILEPATH, fname) excelHelper = ExcelOperation(this.View) result_entry1 = ZipEntry(fname) order_data_csv = convert_list_to_csv(order_data) result_content1 = Encoding.GetEncoding("GBK").GetBytes(order_data_csv) # result_content1 = Encoding.UTF8.GetBytes(fname) result_entry1.Size = len(result_content1) zipStream.PutNextEntry(result_entry1) zipStream.Write(result_content1, 0, len(result_content1)) zipStream.CloseEntry(); zipStream.Finish() zipStream.Close() showParameter = DynamicFormShowParameter() showParameter.FormId = "BOS_FileDownLoad" showParameter.OpenStyle.ShowType = ShowType.Modal downloadPath = PathUtils.GetServerPath(KeyConst.TEMPFILEPATH, zipFileName) showParameter.CustomParams.Add("url", downloadPath) this.View.ShowForm(showParameter) this.View.ShowMessage('图纸获取成功')

生成excel文件.zip



nb

PY插件生成生成指定格式的excel文件并且打包下载

前言:很多时候需要生成一个指定格式的excel文件,并且组织相关的数据,C#很好实现,但是py找了一圈,基本上只有sql查询后再下载,先组织代码分享...
点击下载文档
确认删除?
回到顶部
客服QQ
  • 客服QQ点击这里给我发消息