327 lines
16 KiB
C#
327 lines
16 KiB
C#
![]() |
using Kingdee.BOS.BusinessEntity.BusinessFlow;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Data;
|
|||
|
using System.IO;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Xceed.Document.NET;
|
|||
|
using Xceed.Drawing;
|
|||
|
using Xceed.Words.NET;
|
|||
|
|
|||
|
namespace GZ_LTHPilot_ORDER.Common
|
|||
|
{
|
|||
|
public class CommonHelper
|
|||
|
{
|
|||
|
public static void DolondWord(string templatePath, DataSet data, string outputPath, string FType)
|
|||
|
{
|
|||
|
List<string> List = new List<string>() { "CS", "ES" };
|
|||
|
|
|||
|
// 加载模板文档
|
|||
|
using (var document = DocX.Load(templatePath))
|
|||
|
{
|
|||
|
//int n = data.Tables.Count;
|
|||
|
var datatable = data.Tables[0];
|
|||
|
foreach (DataRow item in datatable.Rows)
|
|||
|
{
|
|||
|
document.ReplaceText("{" + Convert.ToString(item["Key"]) + "}", Convert.ToString(item["Value"]));
|
|||
|
}
|
|||
|
int x = document.Tables.Count;
|
|||
|
if (FType == "D")
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
else if (FType == "S")
|
|||
|
{
|
|||
|
// 假设模板中的表格是第一个表格
|
|||
|
var table = document.Tables[2];
|
|||
|
Row previousRow = (Row)table.Rows[1];
|
|||
|
//for (int i = table.Rows.Count - 4; i >= 1; i--) // 从最后一行开始删除,避免索引超出范围
|
|||
|
//{
|
|||
|
// table.Rows[i].Remove();
|
|||
|
//}
|
|||
|
table.Rows[1].Remove();
|
|||
|
var datatable2 = data.Tables[1];
|
|||
|
foreach (DataRow item in datatable2.Rows)
|
|||
|
{
|
|||
|
Row newRow = table.InsertRow(previousRow, table.Rows.Count - 1);
|
|||
|
foreach (var cell in newRow.Cells)
|
|||
|
{
|
|||
|
{
|
|||
|
// 注意:如果单元格中有多个段落或更复杂的结构,你可能需要遍历它们并分别复制样式
|
|||
|
Border Border = new Border() { Size = BorderSize.four, Color = Color.Black };
|
|||
|
cell.SetBorder(TableCellBorderType.Left, Border);
|
|||
|
cell.SetBorder(TableCellBorderType.Right, Border);
|
|||
|
cell.SetBorder(TableCellBorderType.Top, Border);
|
|||
|
cell.SetBorder(TableCellBorderType.Bottom, Border);
|
|||
|
}
|
|||
|
}
|
|||
|
for (int i = 0; i < datatable2.Columns.Count; i++)
|
|||
|
{
|
|||
|
newRow.Cells[i].ReplaceText("{" + i.ToString() + "}", Convert.ToString(item[i]));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
else if (FType == "W")
|
|||
|
{
|
|||
|
// 假设模板中的表格是第一个表格
|
|||
|
var table = document.Tables[1];
|
|||
|
Row previousRow = (Row)table.Rows[1];
|
|||
|
//for (int i = table.Rows.Count - 4; i >= 1; i--) // 从最后一行开始删除,避免索引超出范围
|
|||
|
//{
|
|||
|
// table.Rows[i].Remove();
|
|||
|
//}
|
|||
|
table.Rows[1].Remove();
|
|||
|
var datatable2 = data.Tables[1];
|
|||
|
foreach (DataRow item in datatable2.Rows)
|
|||
|
{
|
|||
|
Row newRow = table.InsertRow(previousRow, table.Rows.Count - 2);
|
|||
|
foreach (var cell in newRow.Cells)
|
|||
|
{
|
|||
|
{
|
|||
|
// 注意:如果单元格中有多个段落或更复杂的结构,你可能需要遍历它们并分别复制样式
|
|||
|
Border Border = new Border() { Size = BorderSize.four, Color = Color.Black };
|
|||
|
cell.SetBorder(TableCellBorderType.Left, Border);
|
|||
|
cell.SetBorder(TableCellBorderType.Right, Border);
|
|||
|
cell.SetBorder(TableCellBorderType.Top, Border);
|
|||
|
cell.SetBorder(TableCellBorderType.Bottom, Border);
|
|||
|
}
|
|||
|
}
|
|||
|
for (int i = 0; i < datatable2.Columns.Count; i++)
|
|||
|
{
|
|||
|
newRow.Cells[i].ReplaceText("{" + i.ToString() + "}", Convert.ToString(item[i]));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
else if (FType == "SX-1" || FType == "SX-2")
|
|||
|
{
|
|||
|
#region 这里开始复制
|
|||
|
// 假设模板中的表格是第一个表格
|
|||
|
var table = document.Tables[2];
|
|||
|
//// 清空表格中的所有行(除了表头,如果有的话)
|
|||
|
//// 注意:这里假设第一行是表头,不删除
|
|||
|
Row previousRowNew = (Row)table.Rows[1];
|
|||
|
Row previousRowthreeNew = (Row)table.Rows[2];
|
|||
|
Row previousRowfourNew = (Row)table.Rows[3];
|
|||
|
for (int i = 3; i >= 1; i--)
|
|||
|
{
|
|||
|
table.Rows[i].Remove();
|
|||
|
}
|
|||
|
var datatable2 = data.Tables[2];
|
|||
|
|
|||
|
// 使用LINQ来获取去重后的FType字段
|
|||
|
var DistinctFTypes = datatable2.AsEnumerable()
|
|||
|
.Select(row => row.Field<string>("FType"))
|
|||
|
.Distinct()
|
|||
|
.ToList();
|
|||
|
int Index = DistinctFTypes.Count;
|
|||
|
foreach (var item in DistinctFTypes)
|
|||
|
{
|
|||
|
Row previousRow = previousRowNew;
|
|||
|
var previousRowthree = previousRowthreeNew;
|
|||
|
var previousRowfour = previousRowfourNew;
|
|||
|
//插入组合中具体的数据行
|
|||
|
var Rows = datatable2.AsEnumerable().Where(t => Convert.ToString(t["FType"]) == item).ToList();
|
|||
|
//插入一个组合
|
|||
|
#region
|
|||
|
Row newRowfour = table.InsertRow(previousRowfour, 1);
|
|||
|
newRowfour.Cells[1].ReplaceText("{9}", Convert.ToString(Rows[0][11]));
|
|||
|
foreach (var Row in Rows)
|
|||
|
{
|
|||
|
Row newRowthree = table.InsertRow(previousRowthree, 1);
|
|||
|
for (int i = 0; i < 9; i++)
|
|||
|
{
|
|||
|
newRowthree.Cells[i].ReplaceText("{" + i.ToString() + "}", Convert.ToString(Row[i]));
|
|||
|
}
|
|||
|
document.ReplaceText("{10}", Convert.ToString(Row[10]));
|
|||
|
}
|
|||
|
Row newRow = table.InsertRow(previousRow, 1);
|
|||
|
newRow.Cells[0].ReplaceText("{0}", string.Format("{0}、{1}", ConvertToChineseUppercase(Index), Convert.ToString(item)));
|
|||
|
#endregion
|
|||
|
Index--;
|
|||
|
|
|||
|
}
|
|||
|
#endregion
|
|||
|
}
|
|||
|
else if (!List.Contains(FType) && FType != "HH" && FType != "TH")
|
|||
|
{
|
|||
|
// 假设模板中的表格是第一个表格
|
|||
|
var table = document.Tables[1];
|
|||
|
Row previousRow = (Row)table.Rows[1];
|
|||
|
//for (int i = table.Rows.Count - 4; i >= 1; i--) // 从最后一行开始删除,避免索引超出范围
|
|||
|
//{
|
|||
|
// table.Rows[i].Remove();
|
|||
|
//}
|
|||
|
table.Rows[1].Remove();
|
|||
|
var datatable2 = data.Tables[1];
|
|||
|
foreach (DataRow item in datatable2.Rows)
|
|||
|
{
|
|||
|
Row newRow = table.InsertRow(previousRow, table.Rows.Count - 3);
|
|||
|
foreach (var cell in newRow.Cells)
|
|||
|
{
|
|||
|
{
|
|||
|
// 注意:如果单元格中有多个段落或更复杂的结构,你可能需要遍历它们并分别复制样式
|
|||
|
Border Border = new Border() { Size = BorderSize.four, Color = Color.Black };
|
|||
|
cell.SetBorder(TableCellBorderType.Left, Border);
|
|||
|
cell.SetBorder(TableCellBorderType.Right, Border);
|
|||
|
cell.SetBorder(TableCellBorderType.Top, Border);
|
|||
|
cell.SetBorder(TableCellBorderType.Bottom, Border);
|
|||
|
}
|
|||
|
}
|
|||
|
for (int i = 0; i < datatable2.Columns.Count; i++)
|
|||
|
{
|
|||
|
newRow.Cells[i].ReplaceText("{" + i.ToString() + "}", Convert.ToString(item[i]));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
else if (List.Contains(FType))
|
|||
|
{
|
|||
|
#region 这里开始复制
|
|||
|
// 假设模板中的表格是第一个表格
|
|||
|
var table = document.Tables[3];
|
|||
|
//// 清空表格中的所有行(除了表头,如果有的话)
|
|||
|
//// 注意:这里假设第一行是表头,不删除
|
|||
|
Row previousRowNew = (Row)table.Rows[1];
|
|||
|
Row previousRowthreeNew = (Row)table.Rows[2];
|
|||
|
Row previousRowfourNew = (Row)table.Rows[3];
|
|||
|
for (int i = 3; i >= 1; i--)
|
|||
|
{
|
|||
|
table.Rows[i].Remove();
|
|||
|
}
|
|||
|
var datatable2 = data.Tables[2];
|
|||
|
|
|||
|
// 使用LINQ来获取去重后的FType字段
|
|||
|
var DistinctFTypes = datatable2.AsEnumerable()
|
|||
|
.Select(row => row.Field<string>("FType"))
|
|||
|
.Distinct()
|
|||
|
.ToList();
|
|||
|
int Index = DistinctFTypes.Count;
|
|||
|
foreach (var item in DistinctFTypes)
|
|||
|
{
|
|||
|
Row previousRow = previousRowNew;
|
|||
|
var previousRowthree = previousRowthreeNew;
|
|||
|
var previousRowfour = previousRowfourNew;
|
|||
|
//插入组合中具体的数据行
|
|||
|
var Rows = datatable2.AsEnumerable().Where(t => Convert.ToString(t["FType"]) == item).ToList();
|
|||
|
//插入一个组合
|
|||
|
#region
|
|||
|
Row newRowfour = table.InsertRow(previousRowfour, 1);
|
|||
|
newRowfour.Cells[1].ReplaceText("{9}", Convert.ToString(Rows[0][11]));
|
|||
|
foreach (var Row in Rows)
|
|||
|
{
|
|||
|
Row newRowthree = table.InsertRow(previousRowthree, 1);
|
|||
|
for (int i = 0; i < 9; i++)
|
|||
|
{
|
|||
|
newRowthree.Cells[i].ReplaceText("{" + i.ToString() + "}", Convert.ToString(Row[i]));
|
|||
|
}
|
|||
|
document.ReplaceText("{10}", Convert.ToString(Row[10]));
|
|||
|
}
|
|||
|
Row newRow = table.InsertRow(previousRow, 1);
|
|||
|
newRow.Cells[0].ReplaceText("{0}", string.Format("{0}、{1}", ConvertToChineseUppercase(Index), Convert.ToString(item)));
|
|||
|
#endregion
|
|||
|
Index--;
|
|||
|
|
|||
|
}
|
|||
|
#endregion
|
|||
|
}
|
|||
|
else if (FType == "HH")
|
|||
|
{
|
|||
|
// 获取模板文档中的表格
|
|||
|
var table = document.Tables[1];
|
|||
|
Row previousRow = (Row)table.Rows[1];
|
|||
|
var roww = table.Rows;
|
|||
|
table.Rows[1].Remove();
|
|||
|
//获取存储过程的查询
|
|||
|
var datatable2 = data.Tables[3];
|
|||
|
foreach (DataRow item in datatable2.Rows)
|
|||
|
{
|
|||
|
int textn = table.Rows.Count;
|
|||
|
Row newRow = table.InsertRow(previousRow, table.Rows.Count - 3);
|
|||
|
foreach (var cell in newRow.Cells)
|
|||
|
{
|
|||
|
{
|
|||
|
// 注意:如果单元格中有多个段落或更复杂的结构,你可能需要遍历它们并分别复制样式
|
|||
|
Border Border = new Border() { Size = BorderSize.four, Color = Color.Black };
|
|||
|
cell.SetBorder(TableCellBorderType.Left, Border);
|
|||
|
cell.SetBorder(TableCellBorderType.Right, Border);
|
|||
|
cell.SetBorder(TableCellBorderType.Top, Border);
|
|||
|
cell.SetBorder(TableCellBorderType.Bottom, Border);
|
|||
|
}
|
|||
|
}
|
|||
|
for (int i = 0; i < datatable2.Columns.Count; i++)
|
|||
|
{
|
|||
|
newRow.Cells[i].ReplaceText("{" + i.ToString() + "}", Convert.ToString(item[i]));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
// 获取模板文档中的表格
|
|||
|
var table = document.Tables[1];
|
|||
|
Row previousRow = (Row)table.Rows[1];
|
|||
|
var roww = table.Rows;
|
|||
|
table.Rows[1].Remove();
|
|||
|
//获取存储过程的查询
|
|||
|
var datatable2 = data.Tables[4];
|
|||
|
foreach (DataRow item in datatable2.Rows)
|
|||
|
{
|
|||
|
int textn = table.Rows.Count;
|
|||
|
Row newRow = table.InsertRow(previousRow, table.Rows.Count - 3);
|
|||
|
foreach (var cell in newRow.Cells)
|
|||
|
{
|
|||
|
{
|
|||
|
// 注意:如果单元格中有多个段落或更复杂的结构,你可能需要遍历它们并分别复制样式
|
|||
|
Border Border = new Border() { Size = BorderSize.four, Color = Color.Black };
|
|||
|
cell.SetBorder(TableCellBorderType.Left, Border);
|
|||
|
cell.SetBorder(TableCellBorderType.Right, Border);
|
|||
|
cell.SetBorder(TableCellBorderType.Top, Border);
|
|||
|
cell.SetBorder(TableCellBorderType.Bottom, Border);
|
|||
|
}
|
|||
|
}
|
|||
|
for (int i = 0; i < datatable2.Columns.Count; i++)
|
|||
|
{
|
|||
|
newRow.Cells[i].ReplaceText("{" + i.ToString() + "}", Convert.ToString(item[i]));
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
if (!Directory.Exists(Path.GetDirectoryName(outputPath)))
|
|||
|
{
|
|||
|
Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
|
|||
|
}
|
|||
|
// 保存新文档
|
|||
|
document.SaveAs(outputPath);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static string ConvertToChineseUppercase(int number)
|
|||
|
{
|
|||
|
// 定义一个字典来存储数字到大写汉字的映射
|
|||
|
Dictionary<int, string> numberToChineseMap = new Dictionary<int, string>
|
|||
|
{
|
|||
|
{ 0, "零" },
|
|||
|
{ 1, "一" },
|
|||
|
{ 2, "二" },
|
|||
|
{ 3, "三" },
|
|||
|
{ 4, "四" },
|
|||
|
{ 5, "五" },
|
|||
|
{ 6, "六" },
|
|||
|
{ 7, "七" },
|
|||
|
{ 8, "八" },
|
|||
|
{ 9, "九" }
|
|||
|
};
|
|||
|
|
|||
|
// 检查字典中是否包含该数字
|
|||
|
if (numberToChineseMap.ContainsKey(number))
|
|||
|
{
|
|||
|
return numberToChineseMap[number];
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
// 处理超出范围的情况,可以抛出异常或者返回默认值
|
|||
|
throw new ArgumentException("数字超出范围");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|