C#如何制作Excel?【C#简易Excel表格制作方法】

第一次写技术博文,技术含量比较低,但是很实用,新手们可以做个参考,大神们有什么意见请多多指教!

源代码下载

using System.Diagnostics;
using System.IO;
using System.Threading;

#region 结束进程
/// 
/// 结束进程
///	FindAndKillProcessByName
/// 
/// 
private void FindAndKillProcessByName(string name)
{
    foreach (Process winProc in Process.GetProcessesByName(name))
    {
        if (winProc.ProcessName.Equals(name))
        {
            winProc.Kill();
        }
    }
}
#endregion

#region Excel模板
/// 
/// Excel模板
///	DemoXls
/// 
/// 
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 false;
    }
}
#endregion



如需转载请注明出处:http://blog.csdn.net/danding_ge/article/details/17000969

你可能感兴趣的:(C#,excel,c#)