using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Reflection; using System.Text; using System.Web; namespace NFine.Web { public class PrintJson { /// /// 打印的JSON数据 /// private JObject jPrintValue; /// /// 打印的参数,每个数组包括两个值:Key, Value /// private JArray jaryPrintParam; /// /// 打印预览时的按钮是否显示 /// private JObject jButtonHide; /// /// 设置页边距 /// private JObject jMargin; /// /// 设置数据集的名称 /// private JObject jDataSetName; /// /// 打印的单个图片的数组,包含3个元素:报表图片对象名、图片文件名、 图片文件内容 /// private JArray jaryPictureSingle; /// /// 打印的数据集中的图片的内容的数组,包含2个元素:图片文件名称、图片文件内容 /// private JArray jaryPictureTableValue; /// /// 打印的数据集中的图片的设置的数组,包含3个元素:报表图片对象名、数据集序号、字段名称 /// private JArray jaryPictureTableSet; /// /// 打印的数据内容的数组 /// private JArray jaryPrintData; /// /// 控件注册的姓名或公司名称 /// private string strRegName; /// /// 控件的注册码 /// private string strRegValue; /// /// 报表文件名称,包括绝对路径 /// private string strReportFileName; /// /// 报表临时文件所在的文件夹,服务器的报表JSON数据将临时存储在此文件夹,此文件夹要求能通过URL访问,控件通过文件名下载报表打印所需要的数据 /// private string strReportTempDirectory; /// /// 存储打印控件时初始化时,所传入的Cookie值、回传服务端的URL /// private JObject jCookieUrl; /// /// 打印初始化类 /// /// 报表临时文件所在的文件夹,服务器的报表JSON数据将临时存储在此文件夹,此文件夹要求能通过URL访问,控件通过文件名下载报表打印所需要的数据 /// 报表文件名称,包括绝对路径 public PrintJson(string _strReportTempDirectory, string _strReportFileName) { jPrintValue = new JObject(); jaryPrintParam = new JArray(); jButtonHide = new JObject(); jMargin = new JObject(); jDataSetName = new JObject(); jaryPictureSingle = new JArray(); jaryPictureTableValue = new JArray(); jaryPictureTableSet = new JArray(); jaryPrintData = new JArray(); jCookieUrl = new JObject(); this.strReportTempDirectory = _strReportTempDirectory; if (this.strReportTempDirectory != "") { if (strReportTempDirectory.Substring(strReportTempDirectory.Length - 1, 1) != "\\") { strReportTempDirectory = strReportTempDirectory + "\\"; } } if (!Directory.Exists(strReportTempDirectory)) { Directory.CreateDirectory(strReportTempDirectory); } this.strReportFileName = _strReportFileName; } /// /// 输入控件注册的信息,检查注册 /// /// 控件注册的姓名或公司名称 /// 控件的注册码 public void CheckRegister(string _strRegName, string _strRegValue) { this.strRegName = _strRegName; this.strRegValue = _strRegValue; } /// /// 设置控件初始化后,需要上传的Cookie,以及回传的服务器URL /// 服务端调用控件之前,判断是否有 Cookie(此Cookie为GuId值,保证唯一性,此Cookie的保存时间为10年以上),若有Cookie则在服务端的数据表中查找,判断是否有记录,若有记录,则表示此电脑已经安装过。 若没有记录,或者没有Cookie,则表示未安装过。若没有Cookie则建立一个Cookie。 /// 调用控件时,把此Cookie,还有Cookie回传的服务端的URL传给控件。控件在运行时,把此cookie值回传服务器,服务器把此Cookie写入数据表中,并且更新最后一次回调的时间 /// 浏览器在调用控件之后,可以再启动一个延时(比如延时3秒)函数,以Ajax去访问服务端(参数:Cookie值),服务端去读取数据表的此Cookie的记录,若不存在记录,或最后访问的时间超过了规定时间,则判断为客户机没有安装,提示下载安装。 /// /// 此Cookie值由服务器生成,最好是GUID,保证唯一性 /// 回传的服务器URL,是Get方式 public void SetCookieAndURL(string strCookie, string strServerGetUrl) { if (strCookie != "" && strServerGetUrl.ToLower().Contains("http")) { jCookieUrl.Add(new JProperty("Cookie", strCookie)); jCookieUrl.Add(new JProperty("ServerUrl", strServerGetUrl)); } } /// /// 增加打印的参数 /// /// 参数名 /// 参数值 public void AddPrintParam(string strKey, string strValue) { JObject jParam = new JObject(); jParam.Add(new JProperty("Key", strKey)); jParam.Add(new JProperty("Value", strValue)); jaryPrintParam.Add(jParam); } /// /// 在线编辑报表时,设置报表的文件名 /// /// public void SetReportFileName(string strReportFileName) { jPrintValue.Add(new JProperty("ReportFileName", strReportFileName)); } /// /// 在线编辑报表时,设置Web服务器接收保存报表数据的URL /// 公司版本才有此功能,个人版本无此功能 /// /// public void SetPostUrl(string strPostUrl) { jPrintValue.Add(new JProperty("PostUrl", strPostUrl)); } /// /// 在报表预览时,点击页面设置按钮,所设置的页面设置信息可通过此URL上传至Web服务器保存 /// /// public void SetPageSetUrl(string strPageSetUrl) { jPrintValue.Add(new JProperty("PageSetUrl", strPageSetUrl)); } /// /// 在报表打印时,在弹出的选择打印机的窗口后点击“确定”按钮时,通过Http的Post方式直接提交到所设置的URL页面,用户可以在URL处理用户已打印的份数 /// /// public void SetPrintUrl(string strPrintUrl) { jPrintValue.Add(new JProperty("PrintUrl", strPrintUrl)); } /// /// 指定打印时的打印机名称 /// /// public void SetPrinter(string strPrinter) { jPrintValue.Add(new JProperty("Printer", strPrinter)); } /// /// 报表打印预览窗口隐藏按钮:打印 /// 公司版本才有此功能,个人版本无此功能 /// public void IsHideButtonPrint() { jButtonHide.Add(new JProperty("pbPrint", false)); } /// /// 报表打印预览窗口隐藏按钮:打开文件 /// 公司版本才有此功能,个人版本无此功能 /// public void IsHideButtonLoad() { jButtonHide.Add(new JProperty("pbLoad", false)); } /// /// 报表打印预览窗口隐藏按钮:保存 /// 公司版本才有此功能,个人版本无此功能 /// public void IsHideButtonSave() { jButtonHide.Add(new JProperty("pbSave", false)); } /// /// 报表打印预览窗口隐藏按钮:导出 /// 公司版本才有此功能,个人版本无此功能 /// public void IsHideButtonExport() { jButtonHide.Add(new JProperty("pbExport", false)); } /// /// 报表打印预览窗口隐藏按钮:放大缩小 /// 公司版本才有此功能,个人版本无此功能 /// public void IsHideButtonZoom() { jButtonHide.Add(new JProperty("pbZoom", false)); } /// /// 报表打印预览窗口隐藏按钮:查找 /// 公司版本才有此功能,个人版本无此功能 /// public void IsHideButtonFind() { jButtonHide.Add(new JProperty("pbFind", false)); } /// /// 报表打印预览窗口隐藏按钮:大纲显示 /// 公司版本才有此功能,个人版本无此功能 /// public void IsHideButtonOutline() { jButtonHide.Add(new JProperty("pbOutline", false)); } /// /// 报表打印预览窗口隐藏按钮:页面设置 /// 公司版本才有此功能,个人版本无此功能 /// public void IsHideButtonPageSetup() { jButtonHide.Add(new JProperty("pbPageSetup", false)); } /// /// 报表打印预览窗口隐藏按钮:页面导航 /// 公司版本才有此功能,个人版本无此功能 /// public void IsHideButtonNavigator() { jButtonHide.Add(new JProperty("pbNavigator", false)); } /// /// 报表打印预览窗口隐藏按钮:快速导出 /// 公司版本才有此功能,个人版本无此功能 /// public void IsHideButtonExportQuick() { jButtonHide.Add(new JProperty("pbExportQuick", false)); } /// /// 设置页面设置的左边距,单位为毫米 /// 公司版本才有此功能,个人版本无此功能 /// /// public void SetLeftMargin(double dbLeftMargin) { jMargin.Add(new JProperty("LeftMargin", dbLeftMargin)); } /// /// 设置页面设置的右边距,单位为毫米 /// 公司版本才有此功能,个人版本无此功能 /// /// public void SetRightMargin(int dbRightMargin) { jMargin.Add(new JProperty("RightMargin", dbRightMargin)); } /// /// 设置页面设置的上边距,单位为毫米 /// 公司版本才有此功能,个人版本无此功能 /// /// public void SetTopMargin(int dbTopMargin) { jMargin.Add(new JProperty("TopMargin", dbTopMargin)); } /// /// 设置页面设置的下边距,单位为毫米 /// 公司版本才有此功能,个人版本无此功能 /// /// public void SetBottomMargin(int dbBottomMargin) { jMargin.Add(new JProperty("BottomMargin", dbBottomMargin)); } /// /// 设置数据集1的名称为指定名称(可以是中文) /// 公司版本才有此功能,个人版本无此功能 /// /// public void SetDataSetName1(string strDataSetName) { jDataSetName.Add(new JProperty("DataSetName1", strDataSetName)); } /// /// 设置数据集2的名称为指定名称(可以是中文) /// 公司版本才有此功能,个人版本无此功能 /// /// public void SetDataSetName2(string strDataSetName) { jDataSetName.Add(new JProperty("DataSetName2", strDataSetName)); } /// /// 设置数据集3的名称为指定名称(可以是中文) /// 公司版本才有此功能,个人版本无此功能 /// /// public void SetDataSetName3(string strDataSetName) { jDataSetName.Add(new JProperty("DataSetName3", strDataSetName)); } /// /// 设置数据集4的名称为指定名称(可以是中文) /// 公司版本才有此功能,个人版本无此功能 /// /// public void SetDataSetName4(string strDataSetName) { jDataSetName.Add(new JProperty("DataSetName4", strDataSetName)); } /// /// 设置数据集5的名称为指定名称(可以是中文) /// 公司版本才有此功能,个人版本无此功能 /// /// public void SetDataSetName5(string strDataSetName) { jDataSetName.Add(new JProperty("DataSetName5", strDataSetName)); } /// /// 设置数据集6的名称为指定名称(可以是中文) /// 公司版本才有此功能,个人版本无此功能 /// /// public void SetDataSetName6(string strDataSetName) { jDataSetName.Add(new JProperty("DataSetName6", strDataSetName)); } /// /// 建立数据集的主从关系 /// /// 主数据集序号,通常为1 /// 关联字段名为两个数据集共同的字段名 /// 关联的字段是否为数值型 public void MasterOptions(int iMasterDataSeq, string strFieldName, bool bIsNumber) { JObject jMasterOptions = new JObject(); jMasterOptions.Add(new JProperty("MasterDataSeq", iMasterDataSeq)); jMasterOptions.Add(new JProperty("FieldName", strFieldName)); jMasterOptions.Add(new JProperty("IsNumber", bIsNumber)); jPrintValue.Add("MasterOptions", jMasterOptions); } /// /// 增加单张图片打印 /// /// 在报表中的图片对象名称 /// 图片文件的绝对路径,包括最后的\ /// 图片文件名称 public void AddPictureSinglePrint(string strReportObjectName, string strPictureFileName, string strPictureFilePath) { if (!strPictureFilePath.EndsWith("\\")) { strPictureFilePath = strPictureFilePath + "\\"; } string strFileValue = PrintFunction.FileToString(strPictureFilePath + strPictureFileName); if (strFileValue != "") { JObject jPicture = new JObject(); jPicture.Add(new JProperty("ReportObjectName", strReportObjectName)); jPicture.Add(new JProperty("PictureFileName", strPictureFileName)); jPicture.Add(new JProperty("PictureFileValue", strFileValue)); jaryPictureSingle.Add(jPicture); } } /// /// 增加List数据中图片字段所对应的图片打印 /// /// List的类名 /// 在报表中的图片对象名称 /// list数据内容 /// 该list中所有数据集的序号,从1开始 /// 图片所在的字段名 /// 图片在服务器上的绝对路径 public void AddPictureIListToPrint(string strReportObjectName, IList listData, int iDataSetIndex, string strFieldName, string strFilePath) { //增加图片打印的设置信息 JObject jPictureSet = new JObject(); jPictureSet.Add(new JProperty("ReportObjectName", strReportObjectName)); jPictureSet.Add(new JProperty("DataSetIndex", iDataSetIndex)); jPictureSet.Add(new JProperty("FieldName", strFieldName)); jaryPictureTableSet.Add(jPictureSet); //利用反射找到图片字段的索引号 Type pDataType = listData[0].GetType(); int iPictureFieldCol = -1; PropertyInfo[] aryColumnes = pDataType.GetProperties(); for (int iColRecn = 0; iColRecn < aryColumnes.Length; iColRecn++) { if (strFieldName.Trim() == aryColumnes[iColRecn].Name.Trim()) { iPictureFieldCol = iColRecn; break; } } if (iPictureFieldCol < 0) { return; } //取各行的图片文件的值,写入JSON数组中 for (int iRowRecn = 0; iRowRecn < listData.Count; iRowRecn++) { string strPictureFileName = aryColumnes[iPictureFieldCol].GetValue(listData[iRowRecn], null).ToString(); if (strPictureFileName == "") { continue; } string strPictureValue = PrintFunction.FileToString(strFilePath + strPictureFileName); if (strPictureValue != "") { JObject jPicture = new JObject(); jPicture.Add(new JProperty("PictureFileName", strPictureFileName)); jPicture.Add(new JProperty("PictureFileValue", strPictureValue)); jaryPictureTableValue.Add(jPicture); } } } /// /// 增加DataTable中图片字段所对应的图片打印 /// /// 在报表中的图片对象名称 /// Table数据内容 /// 该table中所有数据集的序号,从1开始 /// 图片所在的字段名 /// 图片在服务器上的绝对路径 public void AddPictureTableToPrint(string strReportObjectName, DataTable dtTable, int iDataSetIndex, string strFieldName, string strFilePath) { //增加图片打印的设置信息 JObject jPictureSet = new JObject(); jPictureSet.Add(new JProperty("ReportObjectName", strReportObjectName)); jPictureSet.Add(new JProperty("DataSetIndex", iDataSetIndex)); jPictureSet.Add(new JProperty("FieldName", strFieldName)); jaryPictureTableSet.Add(jPictureSet); //取各行的图片文件的值,写入JSON数组中 if (!strFilePath.EndsWith("\\")) { strFilePath = strFilePath + "\\"; } foreach (DataRow pRow in dtTable.Rows) { string strPictureFileName = pRow[strFieldName].ToString().Trim(); if (strPictureFileName == "") { continue; } string strPictureValue = PrintFunction.FileToString(strFilePath + strPictureFileName); if (strPictureValue != "") { JObject jPicture = new JObject(); jPicture.Add(new JProperty("PictureFileName", strPictureFileName)); jPicture.Add(new JProperty("PictureFileValue", strPictureValue)); jaryPictureTableValue.Add(jPicture); } } } /// /// 增加打印的数据集,数据类型:List /// /// List的类名 /// list数据内容 public void AddPrintDataIList(IList listData) { string strPrintValue = PrintFunction.ListToXml(listData); if (strPrintValue != "") { jaryPrintData.Add(strPrintValue); } } /// /// 增加打印的数据集,数据类型:Table /// /// 数据内容,此Table需要取得数据结构,即在填充数据集之前,先调用:da.FillSchema(ds, SchemaType.Source); public void AddPrintDataTable(DataTable dtTable) { string strPrintValue = PrintFunction.TableToXml(dtTable); if (strPrintValue != "") { jaryPrintData.Add(strPrintValue); } } /// /// 预览报表,在打印之前先调用了 AddPrintDataIList 或 AddPrintDataTable 增加好了数据集 /// public string ShowReport() { if (jaryPrintData.Count == 0) { throw new Exception("没有数据集"); } jPrintValue.Add(new JProperty("Method", "ShowReport")); return CallPrint(); } /// /// 预览报表,1个List数据 /// /// /// public string ShowReport(IList listData) { AddPrintDataIList(listData); jPrintValue.Add(new JProperty("Method", "ShowReport")); return CallPrint(); } /// /// 预览报表:2个List数据 /// /// /// /// /// public string ShowReport(IList listData1, IList listData2) { AddPrintDataIList(listData1); AddPrintDataIList(listData2); jPrintValue.Add(new JProperty("Method", "ShowReport")); return CallPrint(); } /// /// 预览报表:多个Table数据 /// /// 此Table需要取得数据结构,即在填充数据集之前,先调用:da.FillSchema(ds, SchemaType.Source); /// Table数据,可不填 /// Table数据,可不填 /// Table数据,可不填 /// Table数据,可不填 /// Table数据,可不填 public string ShowReport(DataTable dtTable1, DataTable dtTable2 = null, DataTable dtTable3 = null, DataTable dtTable4 = null, DataTable dtTable5 = null, DataTable dtTable6 = null) { AddPrintDataTable(dtTable1); if (dtTable2 != null) { AddPrintDataTable(dtTable2); } if (dtTable3 != null) { AddPrintDataTable(dtTable3); } if (dtTable4 != null) { AddPrintDataTable(dtTable4); } if (dtTable5 != null) { AddPrintDataTable(dtTable5); } if (dtTable6 != null) { AddPrintDataTable(dtTable6); } jPrintValue.Add(new JProperty("Method", "ShowReport")); return CallPrint(); } /// /// 打印报表,在打印之前先调用了 AddPrintDataIList 或 AddPrintDataTable 增加好了数据集 /// public string PrintReport() { if (jaryPrintData.Count == 0) { throw new Exception("没有数据集"); } jPrintValue.Add(new JProperty("Method", "PrintReport")); return CallPrint(); } /// /// 打印报表,1个List数据 /// /// /// public string PrintReport(IList listData) { AddPrintDataIList(listData); jPrintValue.Add(new JProperty("Method", "PrintReport")); return CallPrint(); } /// /// 打印报表:2个List数据 /// /// /// /// /// public string PrintReport(IList listData1, IList listData2) { AddPrintDataIList(listData1); AddPrintDataIList(listData2); jPrintValue.Add(new JProperty("Method", "PrintReport")); return CallPrint(); } /// /// 打印报表:多个Table数据 /// /// 此Table需要取得数据结构,即在填充数据集之前,先调用:da.FillSchema(ds, SchemaType.Source); /// Table数据,可不填 /// Table数据,可不填 /// Table数据,可不填 /// Table数据,可不填 /// Table数据,可不填 public string PrintReport(DataTable dtTable1, DataTable dtTable2 = null, DataTable dtTable3 = null, DataTable dtTable4 = null, DataTable dtTable5 = null, DataTable dtTable6 = null) { AddPrintDataTable(dtTable1); if (dtTable2 != null) { AddPrintDataTable(dtTable2); } if (dtTable3 != null) { AddPrintDataTable(dtTable3); } if (dtTable4 != null) { AddPrintDataTable(dtTable4); } if (dtTable5 != null) { AddPrintDataTable(dtTable5); } if (dtTable6 != null) { AddPrintDataTable(dtTable6); } jPrintValue.Add(new JProperty("Method", "PrintReport")); return CallPrint(); } /// /// 在线设计报表,在打印之前先调用了 AddPrintDataIList 或 AddPrintDataTable 增加好了数据集 /// public string DesignReport() { if (jaryPrintData.Count == 0) { throw new Exception("没有数据集"); } jPrintValue.Add(new JProperty("Method", "DesignReport")); return CallPrint(); } /// /// 在线设计报表,1个List数据 /// /// /// public string DesignReport(IList listData) { AddPrintDataIList(listData); jPrintValue.Add(new JProperty("Method", "DesignReport")); return CallPrint(); } /// /// 在线设计报表:2个List数据 /// /// /// /// /// public string DesignReport(IList listData1, IList listData2) { AddPrintDataIList(listData1); AddPrintDataIList(listData2); jPrintValue.Add(new JProperty("Method", "DesignReport")); return CallPrint(); } /// /// 在线设计报表:多个Table数据 /// /// 此Table需要取得数据结构,即在填充数据集之前,先调用:da.FillSchema(ds, SchemaType.Source); /// Table数据,可不填 /// Table数据,可不填 /// Table数据,可不填 /// Table数据,可不填 /// Table数据,可不填 public string DesignReport(DataTable dtTable1, DataTable dtTable2 = null, DataTable dtTable3 = null, DataTable dtTable4 = null, DataTable dtTable5 = null, DataTable dtTable6 = null) { AddPrintDataTable(dtTable1); if (dtTable2 != null) { AddPrintDataTable(dtTable2); } if (dtTable3 != null) { AddPrintDataTable(dtTable3); } if (dtTable4 != null) { AddPrintDataTable(dtTable4); } if (dtTable5 != null) { AddPrintDataTable(dtTable5); } if (dtTable6 != null) { AddPrintDataTable(dtTable6); } jPrintValue.Add(new JProperty("Method", "DesignReport")); return CallPrint(); } /// /// 导出报表至Pdf文件,在打印之前先调用了 AddPrintDataIList 或 AddPrintDataTable 增加好了数据集 /// 公司版本才有此功能,个人版本无此功能 /// /// 所导出的Pdf文件的绝对路径及文件名;如果为空,则会弹出打开文件的对话框 public string ExportReportPdf(string strExportPdfFileName = "") { if (jaryPrintData.Count == 0) { throw new Exception("没有数据集"); } jPrintValue.Add(new JProperty("ExportPdfFileName", strExportPdfFileName)); jPrintValue.Add(new JProperty("Method", "ExportReportPdf")); return CallPrint(); } /// /// 导出报表至Pdf文件,1个List数据 /// 公司版本才有此功能,个人版本无此功能 /// /// /// /// 所导出的Pdf文件的绝对路径及文件名;如果为空,则会弹出打开文件的对话框 public string ExportReportPdf(IList listData, string strExportPdfFileName = "") { jPrintValue.Add(new JProperty("ExportPdfFileName", strExportPdfFileName)); AddPrintDataIList(listData); jPrintValue.Add(new JProperty("Method", "ExportReportPdf")); return CallPrint(); } /// /// 导出报表至Pdf文件:2个List数据 /// 公司版本才有此功能,个人版本无此功能 /// /// /// /// /// /// 所导出的Pdf文件的绝对路径及文件名;如果为空,则会弹出打开文件的对话框 public string ExportReportPdf(IList listData1, IList listData2, string strExportPdfFileName = "") { jPrintValue.Add(new JProperty("ExportPdfFileName", strExportPdfFileName)); AddPrintDataIList(listData1); AddPrintDataIList(listData2); jPrintValue.Add(new JProperty("Method", "ExportReportPdf")); return CallPrint(); } /// /// 导出报表至Pdf文件:多个Table数据 /// 公司版本才有此功能,个人版本无此功能 /// /// 此Table需要取得数据结构,即在填充数据集之前,先调用:da.FillSchema(ds, SchemaType.Source); /// Table数据,可不填 /// Table数据,可不填 /// Table数据,可不填 /// Table数据,可不填 /// Table数据,可不填 /// 所导出的Pdf文件的绝对路径及文件名;如果为空,则会弹出打开文件的对话框 public string ExportReportPdf(DataTable dtTable1, DataTable dtTable2 = null, DataTable dtTable3 = null, DataTable dtTable4 = null, DataTable dtTable5 = null, DataTable dtTable6 = null, string strExportPdfFileName = "") { jPrintValue.Add(new JProperty("ExportPdfFileName", strExportPdfFileName)); AddPrintDataTable(dtTable1); if (dtTable2 != null) { AddPrintDataTable(dtTable2); } if (dtTable3 != null) { AddPrintDataTable(dtTable3); } if (dtTable4 != null) { AddPrintDataTable(dtTable4); } if (dtTable5 != null) { AddPrintDataTable(dtTable5); } if (dtTable6 != null) { AddPrintDataTable(dtTable6); } jPrintValue.Add(new JProperty("Method", "ExportReportPdf")); return CallPrint(); } /// /// 组织好参数,调用控件进行操作 /// /// 报表文件名,包括绝对路径 private string CallPrint() { string strPrintFileName = ""; //取报表文件的内容 string strReportFileValue = PrintFunction.FileToString(this.strReportFileName); jPrintValue.Add(new JProperty("ReportFileValue", strReportFileValue)); //检查注册信息 JObject jRegInfo = new JObject(); jRegInfo.Add(new JProperty("RegName", this.strRegName)); jRegInfo.Add(new JProperty("RegValue", this.strRegValue)); jPrintValue.Add("RegInfo", jRegInfo); //报表打印的各参数 jPrintValue.Add("PrintParam", jaryPrintParam); jPrintValue.Add("ButtonHide", jButtonHide); jPrintValue.Add("Margin", jMargin); jPrintValue.Add("DataSetName", jDataSetName); jPrintValue.Add("PictureSingle", jaryPictureSingle); jPrintValue.Add("PictureTableValue", jaryPictureTableValue); jPrintValue.Add("PictureTableSet", jaryPictureTableSet); jPrintValue.Add("PrintData", jaryPrintData); jPrintValue.Add("CookieUrl", jCookieUrl); //把JSON内容写入文件,返回文件名 try { string strPrintValue = jPrintValue.ToString(Newtonsoft.Json.Formatting.None); Random r = new Random(); strPrintFileName = "PrintTemp" + DateTime.Now.ToString("yyyyMMddHHmmss") + r.Next(999999) + ".txt"; string strFileName = this.strReportTempDirectory + strPrintFileName; if (File.Exists(strFileName)) { File.Delete(strFileName); } FileStream pFileStream = new FileStream(strFileName, System.IO.FileMode.CreateNew); byte[] byteWrite = Encoding.UTF8.GetBytes(strPrintValue); pFileStream.Write(byteWrite, 0, byteWrite.Length); byteWrite = null; pFileStream.Flush(); pFileStream.Close(); pFileStream.Dispose(); } catch { strPrintFileName = ""; } return strPrintFileName; } } }