|
@@ -1,5 +1,8 @@
|
|
|
-using Aspose.Words;
|
|
|
+using Aspose.Cells;
|
|
|
+using Aspose.Words;
|
|
|
+using System.Collections.Generic;
|
|
|
using System.Data;
|
|
|
+using System.IO;
|
|
|
using System.Web;
|
|
|
using Ubiety.Dns.Core;
|
|
|
|
|
@@ -16,12 +19,13 @@ namespace OASystem.API.OAMethodLib.File
|
|
|
/// <summary>
|
|
|
/// 根据Word模板进行数据的导出Word操作
|
|
|
/// </summary>
|
|
|
- /// <param name="tempPath">模板路径</param>
|
|
|
- /// <param name="outputFileName">保存文件名称</param>
|
|
|
+ /// <param name="tempFileName">模板文件名称</param>
|
|
|
+ /// <param name="saveFolderName">保存文件夹名称</param>
|
|
|
/// <param name="dic">数据源 key 对应 value</param>
|
|
|
/// <param name="listDt">需要循环的数据DataTable 多个注意DataTableName </param>
|
|
|
/// <returns>返回的文档路径</returns>
|
|
|
- public static string ExpertWordToModel(string tempPath, string outputFileName, Dictionary<string,object> dic, List<DataTable>? listDt)
|
|
|
+ public static string ExpertWordToModel(string tempFileName, string saveFolderName, string saveFilName, Dictionary<string,object> dic,
|
|
|
+ List<DataTable>? listDt)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
@@ -36,6 +40,7 @@ namespace OASystem.API.OAMethodLib.File
|
|
|
fieldValues.Add(dic[key]);
|
|
|
}
|
|
|
|
|
|
+ string tempPath = string.Format("{0}/Word/Template/{1}", AppSettingsHelper.Get("OfficeTempBasePath"), tempFileName);
|
|
|
//载入模板
|
|
|
Document doc = new Document(tempPath);
|
|
|
doc.MailMerge.Execute(fieldNames.ToArray(), fieldValues.ToArray());
|
|
@@ -53,15 +58,24 @@ namespace OASystem.API.OAMethodLib.File
|
|
|
|
|
|
//合并模版,相当于页面的渲染
|
|
|
doc.MailMerge.Execute(new[] { "PageCount" }, new object[] { doc.PageCount });
|
|
|
- //保存合并后的文档
|
|
|
|
|
|
- string outputPath = string.Format("C:\\Server\\File\\OA2023\\Office\\Word\\TencentOCR\\Save\\{0}", outputFileName);
|
|
|
- doc.Save(outputPath);
|
|
|
- return outputPath; //返回下载地址
|
|
|
+ string path = string.Format("{0}/Word/{1}", AppSettingsHelper.Get("OfficeTempBasePath"), saveFolderName);
|
|
|
+
|
|
|
+ if (!System.IO.Directory.Exists(path))
|
|
|
+ {
|
|
|
+ System.IO.Directory.CreateDirectory(path);//不存在就创建文件夹
|
|
|
+ }
|
|
|
+
|
|
|
+ string saveFilePath = string.Format("{0}{1}", path, saveFilName);
|
|
|
+
|
|
|
+ //保存合并后的文档
|
|
|
+ doc.Save(saveFilePath);
|
|
|
+ saveFilePath = saveFilePath.Replace("C:", AppSettingsHelper.Get("OfficeBaseUrl")); //返回下载地址
|
|
|
+ return saveFilePath; //返回下载地址
|
|
|
}
|
|
|
- catch (Exception er)
|
|
|
+ catch (Exception ex)
|
|
|
{
|
|
|
- return er.Message;
|
|
|
+ return ex.Message;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -78,50 +92,64 @@ namespace OASystem.API.OAMethodLib.File
|
|
|
/// <summary>
|
|
|
/// 根据Word模板进行数据的导出Word操作
|
|
|
/// </summary>
|
|
|
- /// <param name="tempPath">模板路径</param>
|
|
|
- /// <param name="outputPath">保存路径</param>
|
|
|
+ /// <param name="tempFileName">Excel模板文件名称</param>
|
|
|
+ /// <param name="saveFolderName">保存文件夹名称</param>
|
|
|
+ /// <param name="saveFilName">保存文件名称</param>
|
|
|
/// <param name="dic">数据源 key 对应 value</param>
|
|
|
/// <param name="listDt">需要循环的数据DataTable 多个注意DataTableName </param>
|
|
|
/// <returns>返回的文档路径</returns>
|
|
|
- public static string ExpertExcelToModel(string tempPath, string outputFileName, Dictionary<string, object> dic, List<DataTable>? listDt)
|
|
|
+ public static string ExpertExcelToModel(string tempFileName, string saveFolderName, string saveFilName, Dictionary<string, object> dic,
|
|
|
+ List<DataTable>? listDt)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- removeWatermark();//加载监听,用于去除水印
|
|
|
-
|
|
|
- List<string> fieldNames = new List<string>(); //字段名字符串数组
|
|
|
- List<object> fieldValues = new List<object>(); //字段值数组
|
|
|
-
|
|
|
- foreach (string key in dic.Keys)
|
|
|
- {
|
|
|
- fieldNames.Add(key);
|
|
|
- fieldValues.Add(dic[key]);
|
|
|
- }
|
|
|
+ string tempPath = string.Format("{0}/Excel/Template/{1}",AppSettingsHelper.Get("OfficeTempBasePath"), tempFileName);
|
|
|
|
|
|
//载入模板
|
|
|
- Document doc = new Document(tempPath);
|
|
|
- doc.MailMerge.Execute(fieldNames.ToArray(), fieldValues.ToArray());
|
|
|
- //将我们获得的DataTable类型的数据:EduDataTable放入doc方法中做处理
|
|
|
+ Workbook wbook = new Workbook(tempPath);
|
|
|
+ //默认第一个工作表
|
|
|
+ Worksheet wSheet = wbook.Worksheets[0];
|
|
|
+ WorkbookDesigner designer = new WorkbookDesigner(wbook);
|
|
|
+
|
|
|
+ //数据源 多个
|
|
|
if (listDt != null && listDt.Count > 0)
|
|
|
{
|
|
|
- foreach (DataTable dt in listDt)
|
|
|
+ foreach (var item in listDt)
|
|
|
{
|
|
|
- if (dt != null && dt.Rows.Count > 0)
|
|
|
+ if (item.Rows.Count > 0)
|
|
|
{
|
|
|
- doc.MailMerge.ExecuteWithRegions(dt);
|
|
|
+ designer.SetDataSource(item.TableName, item);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //合并模版,相当于页面的渲染
|
|
|
- doc.MailMerge.Execute(new[] { "PageCount" }, new object[] { doc.PageCount });
|
|
|
+ //报表单位
|
|
|
+ if (dic.Count > 0)
|
|
|
+ {
|
|
|
+ foreach (var item in dic)
|
|
|
+ {
|
|
|
+ designer.SetDataSource(item.Key, item.Value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ designer.Process();
|
|
|
+
|
|
|
+ string path = string.Format("{0}/Excel/{1}", AppSettingsHelper.Get("OfficeTempBasePath"), saveFolderName);
|
|
|
+
|
|
|
+ if (!System.IO.Directory.Exists(path))
|
|
|
+ {
|
|
|
+ System.IO.Directory.CreateDirectory(path);//不存在就创建文件夹
|
|
|
+ }
|
|
|
+ string saveFilePath = string.Format("{0}{1}", path, saveFilName);
|
|
|
+
|
|
|
//保存合并后的文档
|
|
|
- doc.Save(outputFileName);
|
|
|
- return outputFileName; //返回下载地址
|
|
|
+ wbook.Save(saveFilePath);
|
|
|
+ saveFilePath = saveFilePath.Replace("C:", AppSettingsHelper.Get("OfficeBaseUrl")); //返回下载地址
|
|
|
+ return saveFilePath; //返回下载地址
|
|
|
}
|
|
|
- catch (Exception er)
|
|
|
+ catch (Exception ex)
|
|
|
{
|
|
|
- return er.Message;
|
|
|
+ return ex.Message;
|
|
|
}
|
|
|
}
|
|
|
|