日期:2014-05-17 浏览次数:21024 次
using System.Diagnostics;
using System.IO;
using System.Threading;
#region 结束进程
/// <summary>
/// 结束进程
/// FindAndKillProcessByName
/// </summary>
/// <param name="name"></param>
private void FindAndKillProcessByName(string name)
{
foreach (Process winProc in Process.GetProcessesByName(name))
{
if (winProc.ProcessName.Equals(name))
{
winProc.Kill();
}
}
}
#endregion
#region Excel模板
/// <summary>
/// Excel模板
/// DemoXls
/// </summary>
/// <returns></returns>
private bool DemoXls()
{
string path = "C:\\我的文档";
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
path = path + "\\" + "Example.xls";
try
{
FindAndKillProcessByName("EXCEL"); //结束excel进程
FindAndKillProcessByName("et"); //结束wps进程
Thread.Sleep(1000); //写入excel延时
FileStream fs = new FileStream(path, FileMode.Create);
StreamWriter fsw = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));
string firstrow = "";
for (int i = 0; i < 10; i++)
{
firstrow += "列标题" + i + "\t"; //\t换一列
}
fsw.WriteLine(firstrow); //写入一行
fsw.WriteLine("内容1" + "\t" + "内容2" + "\t……");
fsw.WriteLine(); //空一行
fsw.Close();
fs.Close();
return true;
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
return&