ab56a9bcf7
SVN-Revision: r240
376 lines
15 KiB
C#
376 lines
15 KiB
C#
using Lskj.Business.Impl;
|
|
using Lskj.Control;
|
|
using Lskj.Control.Model;
|
|
using Lskj.Core;
|
|
using Lskj.Model;
|
|
using Lskj.Util;
|
|
using Spire.Presentation;
|
|
using Spire.Presentation.Drawing;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Lskj.PubCreatPPT
|
|
{
|
|
public partial class FrmMain : BaseForm
|
|
{
|
|
public FrmMain()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
#region 公共变量
|
|
/// <summary>
|
|
/// ppt参数类型
|
|
/// </summary>
|
|
public DynamicCreatPPT moduleModel;
|
|
/// <summary>
|
|
/// 主sql参数表
|
|
/// </summary>
|
|
public DataTable Mainargs = new DataTable();
|
|
/// <summary>
|
|
/// 表格参数配置
|
|
/// </summary>
|
|
public DataTable Tableargs = new DataTable();
|
|
/// <summary>
|
|
/// 添加图片参数表
|
|
/// </summary>
|
|
public DataTable Imageargs = new DataTable();
|
|
/// <summary>
|
|
/// 创建ppt模块
|
|
/// </summary>
|
|
public Presentation presentation = null;
|
|
/// <summary>
|
|
/// 进度条设置
|
|
/// </summary>
|
|
public int fileCount = 0;
|
|
#endregion
|
|
#region 线程方法
|
|
/// <summary>
|
|
/// 主线程刷新数据
|
|
/// </summary>
|
|
/// <param name="msg"></param>
|
|
public void RefreshLog(string msg)
|
|
{
|
|
txtLog.Text += msg;
|
|
txtLog.Select(txtLog.TextLength, 0);
|
|
txtLog.ScrollToCaret();
|
|
}
|
|
/// <summary>
|
|
/// 后台线程刷新数据
|
|
/// </summary>
|
|
/// <param name="msg"></param>
|
|
public void RefreshLogUIThread(string msg)
|
|
{
|
|
Invoke((EventHandler)delegate
|
|
{
|
|
txtLog.Text += msg;
|
|
txtLog.Select(txtLog.TextLength, 0);
|
|
txtLog.ScrollToCaret();
|
|
});
|
|
}
|
|
#endregion
|
|
#region 事件
|
|
/// <summary>
|
|
/// <para>说明:点击通过配置生成ppt</para>
|
|
/// <para>创建人:王一帆</para>
|
|
/// <para>创建日期:2021-10-25 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
private void OnbtnStartClick(object sender, EventArgs e)
|
|
{
|
|
this.Initialization(); // 初始化同步平台
|
|
// 创建后台线程
|
|
try
|
|
{
|
|
Thread backThread = new Thread(GenerateProcess);
|
|
backThread.IsBackground = true;
|
|
backThread.Start();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw;
|
|
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:点击退出ppt</para>
|
|
/// <para>创建人:王一帆</para>
|
|
/// <para>创建日期:2021-10-25 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
private void OnbtnGetoutClick(object sender, EventArgs e)
|
|
{
|
|
this.Close();
|
|
}
|
|
|
|
#endregion
|
|
#region 方法
|
|
/// <summary>
|
|
/// <para>说明:初始化方法</para>
|
|
/// <para>创建人:王一帆</para>
|
|
/// <para>创建日期:2021-10-25 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
private void Initialization()
|
|
{
|
|
try
|
|
{
|
|
RefreshLog(DateTime.Now + "正在初始化平台.\r\n");
|
|
string path = PubUtil.CreatePptPath + moduleModel.pptFile + ".pptx";
|
|
presentation = new Presentation();
|
|
//Add new slide
|
|
presentation.Slides.Append();
|
|
//Load the PPT
|
|
presentation.LoadFromFile(path);
|
|
//生成配置ppt的参数
|
|
Mainargs = SqlHelper.ExecuteDataTable(moduleModel.tmpMainSql);
|
|
Tableargs = SqlHelper.ExecuteDataTable(moduleModel.tmpTableSql);
|
|
Imageargs = SqlHelper.ExecuteDataTable(moduleModel.tmpImageSql);
|
|
fileCount = 0;
|
|
fileCount += Mainargs.Rows.Count > 0 ? 1 : 0;
|
|
fileCount += Tableargs.Rows.Count > 0 ? 1 : 0;
|
|
fileCount += Imageargs.Rows.Count > 0 ? 1 : 0;
|
|
//设置进度条基础属性
|
|
this.lbl_progress.Value = 0;
|
|
this.lbl_progress.Style = ProgressBarStyle.Blocks;
|
|
this.lbl_progress.Maximum = fileCount;
|
|
this.lbl_progress.Minimum = 0;
|
|
this.lbl_progress.MarqueeAnimationSpeed = 100;
|
|
this.lbl_progress.Step = 1;
|
|
this.lbl_progress.Text = "0%";
|
|
this.lbl_progress.Refresh();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
RefreshLog(DateTime.Now + string.Format("初始化失败.原因:{0}.\r\n", ex.Message));
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:替换全局变量的方法</para>
|
|
/// <para>创建人:唐德馨</para>
|
|
/// <para>创建日期:2021-10-25 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
public void ReplaceTags(Spire.Presentation.ISlide pSlide, DataTable table)
|
|
{
|
|
foreach (IShape curShape in pSlide.Shapes)
|
|
{
|
|
if (curShape is IAutoShape)
|
|
{
|
|
foreach (TextParagraph paragraph in (curShape as IAutoShape).TextFrame.Paragraphs)
|
|
{
|
|
foreach (DataColumn column in table.Columns)
|
|
{
|
|
if (paragraph.Text.Contains(string.Format("{0}", column.ColumnName + "")))
|
|
{
|
|
paragraph.Text = paragraph.Text.Replace("{" + column.ColumnName + "" + "}", table.Rows[0][column.ColumnName] + "");
|
|
RefreshLogUIThread(string.Format("页面{0}配置列替换成功!\r\n", column.ColumnName));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:替换表格里的参数</para>
|
|
/// <para>创建人:唐德馨</para>
|
|
/// <para>创建日期:2021-10-25 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
public void AddRowsToTable(Spire.Presentation.ISlide pSlide, DataRow[] Ntable)
|
|
{
|
|
bool isSuccess = false;
|
|
ITable table = null;
|
|
foreach (IShape shape in pSlide.Shapes)
|
|
{
|
|
if (shape is ITable)
|
|
{
|
|
table = (ITable)shape;
|
|
for (int i = 0; i < Ntable.Length; i++)
|
|
{
|
|
if (i != Ntable.Length - 1)
|
|
{
|
|
table.TableRows.Append(table.TableRows[i + 1]);
|
|
}
|
|
for (int j = 0; j < table.ColumnsList.Count; j++)
|
|
{
|
|
string colName = (table[j, i + 1].TextFrame.Text + "").Replace("{", "").Replace("}", "");
|
|
table[j, i + 1].TextFrame.Text = Ntable[i].Table.Columns.Contains(colName) && !string.IsNullOrEmpty(Ntable[i][colName] + "") ? Ntable[i][colName] + "" : "";
|
|
}
|
|
RefreshLogUIThread(string.Format("第{0}页表格新增{1}行数据成功!\r\n", pSlide.SlideNumber, i + 1));
|
|
}
|
|
isSuccess = true;
|
|
break;
|
|
}
|
|
}
|
|
if (!isSuccess)
|
|
{
|
|
RefreshLogUIThread(string.Format("当前模板第{0}页没有配置表格\r\n", pSlide.SlideNumber));
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:替换表格里的参数</para>
|
|
/// <para>创建人:唐德馨</para>
|
|
/// <para>创建日期:2021-10-25 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
public void AddPicToPPT(Spire.Presentation.ISlide pSlide, DataRow[] Ntable)
|
|
{
|
|
try
|
|
{
|
|
|
|
for (int i = 0; i < Ntable.Length; i++)
|
|
{
|
|
string picUrlStr = Ntable[i].Table.Columns.Contains("ImageUrl") && !string.IsNullOrEmpty(Ntable[i]["ImageUrl"] + "") ? Ntable[i]["ImageUrl"] + "" : "";
|
|
int picWidth = Ntable[i].Table.Columns.Contains("Width") && !string.IsNullOrEmpty(Ntable[i]["Width"] + "") ? Convert.ToInt32(Ntable[i]["Width"] + "") : 0;
|
|
int picHeight = Ntable[i].Table.Columns.Contains("Height") && !string.IsNullOrEmpty(Ntable[i]["Height"] + "") ? Convert.ToInt32(Ntable[i]["Height"] + "") : 0;
|
|
int picTop = Ntable[i].Table.Columns.Contains("ImageTop") && !string.IsNullOrEmpty(Ntable[i]["ImageTop"] + "") ? Convert.ToInt32(Ntable[i]["ImageTop"] + "") : 0;
|
|
int picLeft = Ntable[i].Table.Columns.Contains("ImageLeft") && !string.IsNullOrEmpty(Ntable[i]["ImageLeft"] + "") ? Convert.ToInt32(Ntable[i]["ImageLeft"] + "") : 0;
|
|
Image image = Image.FromStream(WebRequest.Create(picUrlStr).GetResponse().GetResponseStream());
|
|
IImageData IImage = pSlide.Presentation.Images.Append(image);
|
|
if (image != null)
|
|
{
|
|
RectangleF rect = new RectangleF(picLeft, picTop, picWidth, picHeight);
|
|
pSlide.Shapes.AppendEmbedImage(ShapeType.Rectangle, IImage, rect);
|
|
RefreshLogUIThread(string.Format("第{0}页表格新增图片成功!\r\n", pSlide.SlideNumber));
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
RefreshLogUIThread(string.Format("第{0}页表格新增图片失败!url地址错误!\r\n", pSlide.SlideNumber));
|
|
}
|
|
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:线程设置ppt模块参数</para>
|
|
/// <para>创建人:王一帆</para>
|
|
/// <para>创建日期:2021-10-25 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
public void GenerateProcess()
|
|
{
|
|
RefreshLogUIThread(string.Format("==============开始生成ppt配置文件=================\r\n"));
|
|
//第一步设置选定ppt的全局变量
|
|
if (Mainargs.Rows.Count > 0)
|
|
{
|
|
|
|
RefreshLogUIThread(string.Format("第一步开始生成全局变量设置:\r\n"));
|
|
try
|
|
{
|
|
foreach (Spire.Presentation.ISlide slide in presentation.Slides)
|
|
{
|
|
ReplaceTags(slide, Mainargs);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
RefreshLogUIThread(string.Format("当前主界面配置存在错误!"));
|
|
}
|
|
updProgress();
|
|
}
|
|
//第二步设置选定ppt中指定表格参数
|
|
if (Tableargs.Rows.Count > 0)
|
|
{
|
|
RefreshLogUIThread(string.Format("\r\n第二步开始生成表格数据设置:.\r\n"));
|
|
foreach (Spire.Presentation.ISlide slide in presentation.Slides)
|
|
{
|
|
DataRow[] rows = Tableargs.Select(string.Format("page='{0}'", slide.SlideNumber));
|
|
if (rows.Length > 0)
|
|
{
|
|
AddRowsToTable(slide, rows);
|
|
}
|
|
}
|
|
updProgress();
|
|
}
|
|
//第三步设置ppt中添加图片
|
|
if (Imageargs.Rows.Count > 0)
|
|
{
|
|
RefreshLogUIThread(string.Format("\r\n第三步开始生成图片数据设置:.\r\n"));
|
|
foreach (Spire.Presentation.ISlide slide in presentation.Slides)
|
|
{
|
|
DataRow[] rows = Imageargs.Select(string.Format("page='{0}'", slide.SlideNumber));
|
|
if (rows.Length > 0)
|
|
{
|
|
AddPicToPPT(slide, rows);
|
|
}
|
|
}
|
|
updProgress();
|
|
}
|
|
RefreshLogUIThread(string.Format("{0}=========》生成成功.\r\n", moduleModel.pptFile));
|
|
string filePath = moduleModel.pptFile + "ppt文档" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pptx";
|
|
//Save to file.
|
|
presentation.SaveToFile(filePath, FileFormat.Pptx2013);
|
|
Viewer(filePath);
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:更新进度条</para>
|
|
/// <para>创建人:王一帆</para>
|
|
/// <para>创建日期:2021-10-26 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
private void updProgress()
|
|
{
|
|
this.Invoke(new EventHandler(delegate
|
|
{
|
|
//更新进度条进度状态,当进度条为跑马灯模式时(ProgressBarStyle.Marquee)不可能调用该方法
|
|
this.lbl_progress.PerformStep();
|
|
//设置进度百分比
|
|
double dCount = fileCount, dProg = this.lbl_progress.Value;
|
|
this.lbl_progress.Text = ((dProg / dCount) * 100).ToString() + "%";
|
|
this.lbl_progress.Refresh();
|
|
}));
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>说明:打开新生成ppt</para>
|
|
/// <para>创建人:王一帆</para>
|
|
/// <para>创建日期:2021-10-25 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
private void Viewer(string fileName)
|
|
{
|
|
try
|
|
{
|
|
System.Diagnostics.Process.Start(fileName);
|
|
}
|
|
catch { }
|
|
}
|
|
#endregion
|
|
}
|
|
}
|