356 lines
12 KiB
C#
356 lines
12 KiB
C#
using Newtonsoft.Json.Linq;
|
|
using NPOI.OpenXmlFormats.Wordprocessing;
|
|
using NPOI.XWPF.UserModel;
|
|
using YieldChain.Helpers;
|
|
using YLErp.Office.Converters;
|
|
|
|
namespace YLErp.OfficeModule
|
|
{
|
|
[TestClass]
|
|
public class TestGenericConverter
|
|
{
|
|
[TestMethod("测试FileHelper.ReplaceExtension")]
|
|
public void TestConverter()
|
|
{
|
|
var fileName = "d:\\mm\\111.xlsx";
|
|
var pdfFile = FileHelper.ReplaceExtension(fileName, ".pdf");
|
|
Assert.AreEqual(pdfFile, "d:\\mm\\111.pdf");
|
|
|
|
fileName = "d:\\mm\\111";
|
|
pdfFile = FileHelper.ReplaceExtension(fileName, ".pdf");
|
|
Assert.AreEqual(pdfFile, "d:\\mm\\111.pdf");
|
|
}
|
|
|
|
[TestMethod("测试坏的文档模板")]
|
|
public void TestBadTemplate()
|
|
{
|
|
string templatePath;
|
|
|
|
var targetPath = "111.docx";
|
|
|
|
var varDic = new JsonVarDic();
|
|
varDic.SetVar("a1", "111");
|
|
|
|
try
|
|
{
|
|
templatePath = CreateBadTemplate("bad1");
|
|
|
|
OfficeFileConverter.ConvertByUsingDocTemplate(templatePath, targetPath, varDic);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Assert.AreEqual(ex.Message, "区块未关闭:#loop1");
|
|
}
|
|
|
|
try
|
|
{
|
|
templatePath = CreateBadTemplate("bad2");
|
|
|
|
OfficeFileConverter.ConvertByUsingDocTemplate(templatePath, targetPath, varDic);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Assert.AreEqual(ex.Message, "区块未关闭:#loop2");
|
|
}
|
|
|
|
try
|
|
{
|
|
templatePath = CreateBadTemplate("bad3");
|
|
|
|
OfficeFileConverter.ConvertByUsingDocTemplate(templatePath, targetPath, varDic);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Assert.AreEqual(ex.Message, "没有找到要关闭的区块:/loop2");
|
|
}
|
|
|
|
try
|
|
{
|
|
templatePath = CreateBadTemplate("bad4");
|
|
|
|
OfficeFileConverter.ConvertByUsingDocTemplate(templatePath, targetPath, varDic);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Assert.AreEqual(ex.Message, "执行'/loop1'之前请先关闭'#loop2'的区块");
|
|
}
|
|
|
|
try
|
|
{
|
|
templatePath = CreateBadTemplate("bad-table-close1");
|
|
|
|
OfficeFileConverter.ConvertByUsingDocTemplate(templatePath, targetPath, varDic);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Assert.AreEqual(ex.Message, "不能在表格中关闭表格外的区块:#loop2");
|
|
}
|
|
}
|
|
|
|
private string CreateBadTemplate(string badKey)
|
|
{
|
|
var doc = new XWPFDocument();
|
|
var para = doc.CreateParagraph();
|
|
var r0 = para.CreateRun();
|
|
r0.SetText("{{#loop a1}}");
|
|
|
|
if (badKey == "bad2")
|
|
{
|
|
para = doc.CreateParagraph();
|
|
r0 = para.CreateRun();
|
|
r0.SetText("{{#loop a2}}");
|
|
}
|
|
|
|
if (badKey == "bad3")
|
|
{
|
|
para = doc.CreateParagraph();
|
|
r0 = para.CreateRun();
|
|
r0.SetText("{{/loop a1}}");
|
|
|
|
para = doc.CreateParagraph();
|
|
r0 = para.CreateRun();
|
|
r0.SetText("{{/loop a2}}");
|
|
}
|
|
|
|
if (badKey == "bad4")
|
|
{
|
|
para = doc.CreateParagraph();
|
|
r0 = para.CreateRun();
|
|
r0.SetText("{{#loop a2}}");
|
|
|
|
para = doc.CreateParagraph();
|
|
r0 = para.CreateRun();
|
|
r0.SetText("{{/loop a1}}");
|
|
}
|
|
|
|
if (badKey == "bad-table-close1")
|
|
{
|
|
para = doc.CreateParagraph();
|
|
r0 = para.CreateRun();
|
|
r0.SetText("{{#loop a2}}");
|
|
|
|
var table = doc.CreateTable(3, 3);
|
|
|
|
var c1 = table.GetRow(0).GetCell(0);
|
|
var p1 = c1.AddParagraph();
|
|
var r1 = p1.CreateRun();
|
|
r1.SetText("{{#loop a3}}{{A1}}{{/loop a3}}");
|
|
|
|
table.GetRow(1).GetCell(1).SetText("{{/loop a2}}");
|
|
}
|
|
|
|
var tempFilePath = Path.GetTempFileName();
|
|
|
|
using (var fs = new FileStream(tempFilePath, FileMode.Create))
|
|
{
|
|
doc.Write(fs);
|
|
}
|
|
|
|
return tempFilePath;
|
|
}
|
|
|
|
[TestMethod("测试文档模板")]
|
|
public void TestTemplate1()
|
|
{
|
|
var templatePath = Path.Combine(AppContext.BaseDirectory, "Resources\\OfficeModule\\test1.docx");
|
|
|
|
var targetPath = Path.Combine("d:\\", "test1.docx");
|
|
|
|
var varDic = new JsonVarDic();
|
|
|
|
varDic.SetVar("BeginIf1", true);
|
|
|
|
var loop2 = new JObject {
|
|
{"A1","myA1/2" },
|
|
{"A2","myA2/2" },
|
|
{"A3","myA3/2" },
|
|
{"A4","myA4/2" },
|
|
{"A5","myA5/2" },
|
|
};
|
|
|
|
var loop22 = new JArray() { loop2, loop2 };
|
|
|
|
var loop4 = new JObject {
|
|
{"A1","myA1" },
|
|
{"A2","myA2" },
|
|
{"A3","myA3" },
|
|
{"A4","myA4" },
|
|
{"A5","myA5" },
|
|
};
|
|
|
|
var loop44 = new JArray() { loop4, loop4 };
|
|
|
|
var loop5 = new JArray() { "L5-1", "L5-2", "L5-3", "L5-4" };
|
|
|
|
var testObj = new JObject
|
|
{
|
|
{ "A1", "测试A1" },
|
|
{ "A2", "测试A2" },
|
|
{ "B1", "测试BB1" },
|
|
{ "B2", "测试BB2" },
|
|
{ "T11", "测试T11" },
|
|
{ "loop4", loop4 },
|
|
};
|
|
|
|
varDic.SetVar("Test", testObj);
|
|
varDic.SetVar("loop2", loop22);
|
|
varDic.SetVar("loop4", loop44);
|
|
varDic.SetVar("loop5", loop5);
|
|
|
|
OfficeFileConverter.ConvertByUsingDocTemplate(templatePath, targetPath, varDic);
|
|
}
|
|
|
|
[TestMethod("测试文档模板")]
|
|
public void TestTemplate2()
|
|
{
|
|
var templatePath = Path.Combine(AppContext.BaseDirectory, "Resources\\PluginTests\\DongCai\\到期结算单\\结算书模板.docx");
|
|
using (var stream = File.OpenRead(templatePath))
|
|
{
|
|
var doc = new XWPFDocument(stream);
|
|
|
|
//解析成树状结构再进行处理
|
|
for (var i = 0; i < doc.BodyElements.Count; i++)
|
|
{
|
|
if (doc.BodyElements[i] is XWPFParagraph paragraph)
|
|
{
|
|
var str = paragraph.Text;
|
|
paragraph.ReplaceText("{{合同编号}}", "你好啊");
|
|
}
|
|
}
|
|
|
|
using var ss = File.OpenWrite("d:\\cc.docx");
|
|
doc.Write(ss);
|
|
}
|
|
Assert.IsTrue(File.Exists("d:\\cc.docx"));
|
|
}
|
|
|
|
[TestMethod("测试文档模板")]
|
|
public void TestTemplate3()
|
|
{
|
|
//var templatePath = Path.Combine(AppContext.BaseDirectory, "Resources\\PluginTests\\DongCai\\到期结算单\\结算书模板.docx");
|
|
var templatePath = Path.Combine("d:\\", "新建 DOCX 文档.docx");
|
|
|
|
//JsonVarDic varDic = new JsonVarDic();
|
|
//varDic.SetVar("合同编号", "你好你好");
|
|
//GenericConverter.DocTemplateConvert(templatePath, "d:\\bbbbb.docx",varDic);
|
|
using (var stream = File.OpenRead(templatePath))
|
|
{
|
|
var doc = new XWPFDocument(stream);
|
|
|
|
//解析成树状结构再进行处理
|
|
//for (var i = 0; i < doc.BodyElements.Count; i++)
|
|
//{
|
|
// if (doc.BodyElements[i] is XWPFParagraph paragraph)
|
|
// {
|
|
// var str = paragraph.Text;
|
|
// paragraph.ReplaceText("{{合同编号}}", "你好啊");
|
|
// }
|
|
//}
|
|
|
|
using var ss = File.OpenWrite("d:\\cc22.docx");
|
|
doc.Write(ss);
|
|
}
|
|
Assert.IsTrue(File.Exists("d:\\cc.docx"));
|
|
}
|
|
|
|
[TestMethod("测试文档模板4")]
|
|
public void TestTemplate4()
|
|
{
|
|
var srcpr = new CT_SectPr();
|
|
//设置A4纸纵向,如果要横向,两个值调换即可
|
|
srcpr.pgSz.w = 11906;
|
|
srcpr.pgSz.h = 16838;
|
|
|
|
var doc = new XWPFDocument();
|
|
doc.Document.body.sectPr = srcpr;
|
|
//输出标题
|
|
var ptitle = doc.CreateParagraph();
|
|
ptitle.Alignment = ParagraphAlignment.CENTER;
|
|
var titlerun = ptitle.CreateRun(); //向该段落中添加文字
|
|
titlerun.SetText("标题");
|
|
titlerun.IsBold = true;
|
|
titlerun.FontFamily = "华文行楷";
|
|
titlerun.FontSize = 30;
|
|
titlerun.SetColor("blue");
|
|
|
|
//输出一个段落
|
|
var p2 = doc.CreateParagraph();
|
|
p2.Alignment = ParagraphAlignment.CENTER;
|
|
p2.IndentationFirstLine = 100; //首行缩进
|
|
var r2 = p2.CreateRun();
|
|
r2.SetText(string.Format("生成时间:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm")));
|
|
r2.FontSize = 15;
|
|
titlerun.FontFamily = "宋体";
|
|
|
|
var sb = new StringBuilder();
|
|
using (var stream = File.OpenRead("d:\\simple.docx"))
|
|
{
|
|
var docx = new XWPFDocument();
|
|
|
|
//页眉
|
|
foreach (var xwpfHeader in docx.HeaderList)
|
|
{
|
|
sb.AppendLine(string.Format("{0}", new string[] { xwpfHeader.Text }));
|
|
}
|
|
|
|
//页脚
|
|
|
|
foreach (var xwpfFooter in docx.FooterList)
|
|
{
|
|
sb.AppendLine(string.Format("{0}", new string[] { xwpfFooter.Text }));
|
|
}
|
|
|
|
//读取段落
|
|
foreach (var para in docx.Paragraphs)
|
|
{
|
|
var text = para.ParagraphText; //获得文本
|
|
var runs = para.Runs;
|
|
// string styleid = para.Style;
|
|
for (var i = 0; i < runs.Count; i++)
|
|
{
|
|
var run = runs[i];
|
|
text = run.ToString(); //获得run的文本
|
|
sb.Append(text + ",");
|
|
}
|
|
}
|
|
//读取表格
|
|
foreach (var table in docx.Tables)
|
|
{
|
|
//循环表格行
|
|
foreach (var row in table.Rows)
|
|
{
|
|
foreach (var cell in row.GetTableCells())
|
|
{
|
|
sb.Append(cell.GetText());
|
|
}
|
|
}
|
|
}
|
|
|
|
//读取图片
|
|
foreach (var pictureData in docx.AllPictures)
|
|
{
|
|
var picExtName = pictureData.SuggestFileExtension();
|
|
var picFileName = pictureData.FileName;
|
|
var picFileContent = pictureData.Data;
|
|
|
|
var picTempName = string.Format(Guid.NewGuid().ToString() + "_" + picFileName + "." + picExtName);
|
|
|
|
using (var fs = new FileStream(picTempName, FileMode.Create, FileAccess.Write))
|
|
{
|
|
fs.Write(picFileContent, 0, picFileContent.Length);
|
|
fs.Close();
|
|
}
|
|
|
|
sb.AppendLine(picTempName);
|
|
}
|
|
|
|
Console.WriteLine(sb.ToString());
|
|
}
|
|
|
|
var out1 = new FileStream("d:\\simple.docx", FileMode.Create);
|
|
doc.Write(out1);
|
|
out1.Close();
|
|
}
|
|
}
|
|
}
|