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
{
///
/// 导出Excel 并且控制格式
///
[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;
///
/// 初始化参数
///
/// 替换列表找标签
/// 列表配置文件路径
/// 模板路径
public ExcelXml(String lable, String excelConfigPath, String temPath)
: base()
{
this.Lable = lable;
this.ExcelConfigPath = excelConfigPath;
this.TemPath = temPath;
ReadTemplate();
ReadConfig();
}
///
/// 读取模板文件
///
private void ReadTemplate()
{
try
{
using (StreamReader reader = new StreamReader(TemPath))
{
this.TemContent = reader.ReadToEnd();
reader.Close();
}
}
catch (Exception ex)
{
throw new Exception("读取文件不存在:" + ex.Message);
}
}
///
/// 读取配置文件
///
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();
}
}
///
/// 写入Excel
///
///
///
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);
}
}
///
/// 写入Excel 并打开文件
///
///
///
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
{
}
}
///
/// 替换模板中标签
///
/// 替换后的内容
public string ReplaceSiteTags(MatchEvaluator m)
{
try
{
//匹配系统标签
Regex reg = new Regex(@"", RegexOptions.Compiled);
return reg.Replace(TemContent, m);
}
catch (Exception ex)
{
throw new Exception("模板内容是空的:" + ex.Message);
}
}
///
/// 获取配置文件标签
///
///
///
public MatchCollection ReplaceConfig()
{
try
{
Regex reg = new Regex(@"", RegexOptions.Compiled);
return reg.Matches(ExcelConfigContent);
}
catch (Exception ex)
{
throw new Exception("模板内容为空:" + ex.Message);
}
}
}
}