using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using NPOI.HSSF;
using System.IO;
using System.Text;
using NPOI.XWPF.UserModel;
using NPOI.OpenXmlFormats.Wordprocessing;
using Spire.Pdf;
using Spire.Pdf.General.Find;
using Spire.Pdf.Graphics;
using System.Drawing;
using Spire.License.V1_0;
public partial class admin_pwd : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
lblMsg.Text = "";
if (courseName.Text.Trim() == "")
{
lblMsg.Text = "<b style='color:red'>请输入课程名称</b>";
return;
}
if (openUnit.Text.Trim() == "")
{
lblMsg.Text = "<b style='color:red'>请输入开课单位</b>";
return;
}
if (Teachroom.Text.Trim() == "")
{
lblMsg.Text = "<b style='color:red'>请输入授课教研室</b>";
return;
}
if (teahcer.Text.Trim() == "")
{
lblMsg.Text = "<b style='color:red'>请输入授课教师</b>";
return;
}
if (Book.Text.Trim() == "")
{
lblMsg.Text = "<b style='color:red'>请输入教材</b>";
return;
}
if (techPerson.Text.Trim() == "")
{
lblMsg.Text = "<b style='color:red'>请输入授课对象</b>";
return;
}
if (tTimes.Text.Trim() == "")
{
lblMsg.Text = "<b style='color:red'>请输入授课时间</b>";
return;
}
if (Hours.Text.Trim() == "")
{
lblMsg.Text = "<b style='color:red'>请输入授课时数</b>";
return;
}
if (TeacTopic.Text.Trim() == "")
{
lblMsg.Text = "<b style='color:red'>请输入授课课题</b>";
return;
}
if (techType.Text.Trim() == "")
{
lblMsg.Text = "<b style='color:red'>请输入授课形式</b>";
return;
}
if (Aim.Text.Trim() == "")
{
lblMsg.Text = "<b style='color:red'>请输入教学目的与要求</b>";
return;
}
string TemplatePath1 = Server.MapPath("~/FileTemp/") + "教案示例2.docx";
string databasePath = "/FileTemp/" + "教案示例" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".docx";
string SavePath1 = Server.MapPath("~/FileTemp/") + "教案示例" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".docx"; // @"GoalFiles/Template1.docx";//根据模板1生成的文件路径
//关键字内容
var ParamTemplate1 = new
{
courseName = courseName.Text.Trim(),
openUnit = openUnit.Text.Trim(),
Teachroom = Teachroom.Text.Trim(),
teahcer = teahcer.Text.Trim(),
Book = Book.Text.Trim(),
techPerson = techPerson.Text.Trim(),
tTimes = tTimes.Text.Trim(),
Hours = Hours.Text.Trim(),
TeacTopic = TeacTopic.Text.Trim(),
techType = techType.Text.Trim(),
Aim = Aim.Text.Trim(),
Marjor = Marjor.Text.Trim(),
Difficults = Difficults.Text.Trim(),
TechMehod = TechMehod.Text.Trim(),
TechContent = TechContent.Text.Trim(),
Task = Task.Text.Trim(),
Measure = Measure.Text.Trim(),
Material = Material.Text.Trim()
};
NpoiUtil.Export(TemplatePath1, SavePath1, WordTemplateUtil.getProperties(ParamTemplate1));
//成功之后把路径写入数据库并调整到
string sql = "insert into [File](File_Title,File_Path,File_Type,File_Date)values('教案示例word--" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + "','" + SavePath1 + "','文件','" + DateTime.Now + "')";
DB.ExecuteSql(sql);
Response.Redirect("admin_file_view_list.aspx");
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using NPOI.HSSF;
using NPOI.XWPF.UserModel;
using NPOI.OpenXmlFormats.Wordprocessing;
using ICSharpCode.SharpZipLib;
using System.Reflection;
using System.IO;
/// <summary>
///NpoiUtil 的摘要说明
/// </summary>
public class NpoiUtil
{
public NpoiUtil()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 输出模板docx文档(使用字典)
/// </summary>
/// <param name="tempFilePath">docx文件路径</param>
/// <param name="outPath">输出文件路径</param>
/// <param name="data">字典数据源</param>
public static void Export(string tempFilePath, string outPath, Dictionary<string, string> data)
{
using (FileStream stream = File.OpenRead(tempFilePath))
{
XWPFDocument doc = new XWPFDocument(stream);
//遍历段落
foreach (var para in doc.Paragraphs)
{
ReplaceKey(para, data);
}
//遍历表格
foreach (var table in doc.Tables)
{
foreach (var row in table.Rows)
{
foreach (var cell in row.GetTableCells())
{
foreach (var para in cell.Paragraphs)
{
ReplaceKey(para, data);
}
}
}
}
//写文件
FileStream outFile = new FileStream(outPath, FileMode.Create);
doc.Write(outFile);
outFile.Close();
}
}
private static void ReplaceKey(XWPFParagraph para, Dictionary<string, string> data)
{
string text = "";
foreach (var run in para.Runs)
{
text = run.ToString();
foreach (var key in data.Keys)
{
//$$模板中数据占位符为$KEY$
if (text.Contains("{$" + key + "}"))
{
text = text.Replace("{$" + key + "}", data[key]);
}
}
run.SetText(text, 0);
}
}
/// <summary>
/// 输出模板docx文档(使用反射)
/// </summary>
/// <param name="tempFilePath">docx文件路径</param>
/// <param name="outPath">输出文件路径</param>
/// <param name="data">对象数据源</param>
public static void ExportObjet(string tempFilePath, string outPath, object data)
{
using (FileStream stream = File.OpenRead(tempFilePath))
{
XWPFDocument doc = new XWPFDocument(stream);
//遍历段落
foreach (var para in doc.Paragraphs)
{
ReplaceKeyObjet(para, data);
}
//遍历表格
foreach (var table in doc.Tables)
{
foreach (var row in table.Rows)
{
foreach (var cell in row.GetTableCells())
{
foreach (var para in cell.Paragraphs)
{
ReplaceKeyObjet(para, data);
}
}
}
}
//写文件
FileStream outFile = new FileStream(outPath, FileMode.Create);
doc.Write(outFile);
outFile.Close();
}
}
private static void ReplaceKeyObjet(XWPFParagraph para, object model)
{
string text = "";
Type t = model.GetType();
PropertyInfo[] pi = t.GetProperties();
foreach (var run in para.Runs)
{
text = run.ToString();
foreach (PropertyInfo p in pi)
{
//$$模板中数据占位符为$KEY$
string key = "{$" + p.Name + "}";
if (text.Contains(key))
{
try
{
text = text.Replace(key, p.GetValue(model, null).ToString());
}
catch (Exception ex)
{
//可能有空指针异常
text = text.Replace(key, "");
}
}
}
run.SetText(text, 0);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web;
using NPOI.HSSF;
using System.IO;
using System.Text;
using NPOI.XWPF.UserModel;
using NPOI.OpenXmlFormats.Wordprocessing;
using ICSharpCode.SharpZipLib;
using NPOI.OpenXml4Net;
using NPOI.OpenXmlFormats;
/// <summary>
///WordTemplateUtil 的摘要说明
/// </summary>
public class WordTemplateUtil
{
public WordTemplateUtil()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// NPOI操作word
/// </summary>
/// <param name="TemplatePath">模板路径</param>
/// <param name="SavePath">保存路径</param>
/// <param name="keywords">关键字集合</param>
public static void WriteToPublicationOfResult(string TemplatePath, string SavePath, Dictionary<string, string> keywords)
{
FileStream fs = new FileStream(TemplatePath, FileMode.Open, FileAccess.Read);
XWPFDocument document = new XWPFDocument(fs);
foreach (var table in document.Tables)
{
foreach (var row in table.Rows)
{
foreach (var cell in row.GetTableCells())
{
ReplaceKeyWords(cell.Paragraphs, keywords);//替换表格中的关键字
}
}
}
ReplaceKeyWords(document.Paragraphs, keywords);//替换模板中非表格的关键字
FileStream output = new FileStream(SavePath, FileMode.Create);
document.Write(output);
fs.Close();
fs.Dispose();
output.Close();
output.Dispose();
}
/// <summary>
/// 遍历段落,替换关键字
/// </summary>
/// <param name="Paragraphs">段落</param>
/// <param name="keywords">关键字集合</param>
public static void ReplaceKeyWords(IList<XWPFParagraph> Paragraphs, Dictionary<string, string> keywords)
{
foreach (var item in keywords)
{
foreach (var para in Paragraphs)
{
string oldtext = para.ParagraphText;
if (oldtext == "") continue;
string temptext = para.ParagraphText;
if (temptext.Contains("{$" + item.Key + "}"))
{
temptext = temptext.Replace("{$" + item.Key + "}", item.Value);
}
// para.ReplaceText(oldtext, temptext);
para.Text.Replace(oldtext, temptext);
}
}
}
/// <summary>
/// 格式化关键字集合
/// </summary>
/// <typeparam name="T">泛型对象</typeparam>
/// <param name="t">关键字集对象</param>
/// <returns></returns>
public static Dictionary<string, string> getProperties<T>(T t)
{
Dictionary<string, string> keywords = new Dictionary<string, string>();
if (t == null)
{
return keywords;
}
System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
if (properties.Length <= 0)
{
return keywords;
}
foreach (System.Reflection.PropertyInfo item in properties)
{
string name = item.Name;
object value = item.GetValue(t, null);
if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
{
keywords.Add(name, value.ToString());
}
else
{
getProperties(value);
}
}
return keywords;
}
}