178 lines
5.2 KiB
C#
178 lines
5.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.IO;
|
|
using System.Diagnostics;
|
|
using System.Text.RegularExpressions;
|
|
using System.Data;
|
|
|
|
namespace Zhaizj.Framework.Utils
|
|
{
|
|
/// <summary>
|
|
/// 导出Excel 并且控制格式
|
|
/// </summary>
|
|
[Serializable]
|
|
public class ExcelXml
|
|
{
|
|
|
|
public String Lable { get; set; }
|
|
public String ExcelConfigPath { get; set; }
|
|
public String TemPath { get; set; }
|
|
public String ExcelConfigContent{get;set;}
|
|
|
|
public String TemContent;
|
|
private StringBuilder SbConfig;
|
|
|
|
/// <summary>
|
|
/// 初始化参数
|
|
/// </summary>
|
|
/// <param name="lable">替换列表找标签</param>
|
|
/// <param name="excelConfigPath">列表配置文件路径</param>
|
|
/// <param name="temPath">模板路径</param>
|
|
public ExcelXml(String lable, String excelConfigPath, String temPath)
|
|
: base()
|
|
{
|
|
this.Lable = lable;
|
|
this.ExcelConfigPath = excelConfigPath;
|
|
this.TemPath = temPath;
|
|
ReadTemplate();
|
|
ReadConfig();
|
|
}
|
|
/// <summary>
|
|
/// 读取模板文件
|
|
/// </summary>
|
|
private void ReadTemplate()
|
|
{
|
|
try
|
|
{
|
|
|
|
using (StreamReader reader = new StreamReader(TemPath))
|
|
{
|
|
this.TemContent = reader.ReadToEnd();
|
|
reader.Close();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
throw new Exception("读取文件不存在:" + ex.Message);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 读取配置文件
|
|
/// </summary>
|
|
public void ReadConfig()
|
|
{
|
|
if (SbConfig == null)
|
|
{
|
|
SbConfig = new StringBuilder();
|
|
String line;
|
|
using (StreamReader reader = new StreamReader(ExcelConfigPath))
|
|
{
|
|
while ((line = reader.ReadLine()) != null)
|
|
{
|
|
if (line.Replace("\"", "").Trim().Equals("<"+Lable.Trim()+">"))
|
|
{
|
|
while ((line = reader.ReadLine()) != null)
|
|
{
|
|
if (line.Replace("\"", "").Trim().Equals("</"+Lable.Trim()+">"))
|
|
{
|
|
break;
|
|
}
|
|
SbConfig.Append(line.Trim());
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
this.ExcelConfigContent = SbConfig.ToString();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 写入Excel
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <param name="Content"></param>
|
|
public void WriteExcel(String path, String Content)
|
|
{
|
|
try
|
|
{
|
|
using (StreamWriter sw = new StreamWriter(path))
|
|
{
|
|
sw.WriteLine(Content);
|
|
sw.Flush();
|
|
sw.Close();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("写入文件路径:" + ex.Message);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 写入Excel 并打开文件
|
|
/// </summary>
|
|
/// <param name="path"></param>
|
|
/// <param name="Content"></param>
|
|
public void WriteExcelOpen(String path, String Content)
|
|
{
|
|
try
|
|
{
|
|
using (StreamWriter sw = new StreamWriter(path))
|
|
{
|
|
sw.WriteLine(Content);
|
|
sw.Flush();
|
|
sw.Close();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("写入文件路径:" + ex.Message);
|
|
}
|
|
try
|
|
{
|
|
Process.Start(path);
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 替换模板中标签
|
|
/// </summary>
|
|
/// <returns>替换后的内容</returns>
|
|
public string ReplaceSiteTags(MatchEvaluator m)
|
|
{
|
|
try
|
|
{
|
|
//匹配系统标签
|
|
Regex reg = new Regex(@"<!--#P_[^>]+-->", RegexOptions.Compiled);
|
|
return reg.Replace(TemContent, m);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("模板内容是空的:" + ex.Message);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 获取配置文件标签
|
|
/// </summary>
|
|
/// <param name="m"></param>
|
|
/// <returns></returns>
|
|
public MatchCollection ReplaceConfig()
|
|
{
|
|
try
|
|
{
|
|
|
|
Regex reg = new Regex(@"<!--#P_[^>]+-->", RegexOptions.Compiled);
|
|
return reg.Matches(ExcelConfigContent);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("模板内容为空:" + ex.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|