admin管理员组

文章数量:1583033

1. 包含库

(1)在项目中添加Microsoft.Office.Interop.Excel的程序包。

(2)包含库:using Microsoft.Office.Interop.Excel;

(3)为使用方便和避免混淆:using Excel = Microsoft.Office.Interop.Excel;

2. 创建保存文件和文件头

string filePath = "D:\\Project\\SaveData";
string data = DateTime.Now.ToString("yyyy-MM-dd");
if (!Directory.Exists(filePath + "\\" + data))
{
    Directory.CreateDirectory(filePath + "\\" + data);
}
string time = DateTime.Now.ToString("HH_mm_ss");
mFullPath = filePath + "\\" + data + "\\" + time + ".csv";
FileStream fs = File.Create(mFullPath);
fs.Close();
using (StreamWriter writer = File.CreateText(mFullPath))
{
    writer.WriteLine("WaferID,PreTHK,PostTHK");
    writer.Close();
}

3. 在需要的时候向文件写数据

//保存数据
using (StreamWriter writer = File.AppendText(mFullPath))
{
    writer.WriteLine("{0},{1},{2}", TxtBoxWaferID.Text, TxtBoxPre.Text, TxtBoxPost.Text);
    writer.Close();
}

4. 生成表格

本文标签: 数据officeMicrosoftExcelInterop