c# excel文件追加

2021-01-25 09:12

阅读:743

标签:==   except   jpg   win   get   数值   microsoft   网上   data   

这2天我在写一个规约程序,想将数据保存到excel文件中,1分钟存一次,从网上找到的资源都是一次生成的,没有在原来的基础上追加的模式,几经实验如下:(做个标记)

实现功能,新建立一个data.xlsx文件作为模板,放到程序同级目录下,程序会根据日期新生成文件,同一天的数据按照追加的模式写入。

//* 保存遥测数据
public void saveycdata()
{
string[] data = new string[32];
for (int i = 0; i try
{
data[i] = (dataGridView1.Rows[i].Cells[4].Value.ToString());
}
catch
{
data[i] = "0";
}
savedatatoexcel(data);
}
//数据保存到excel
public void savedatatoexcel(string[] data)
{

Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show("无法创建excel对象,请确认excel环境是否正常");
return;

}
xlApp.Visible = false;
xlApp.UserControl = true;
xlApp.DisplayAlerts = false;
Excel._Workbook workbook;
DateTime dt = DateTime.Now;
int hour = dt.Hour;
int min = dt.Minute;
int hh = hour * 60 + min / 1;
string date = dt.Year.ToString("D4") + dt.Month.ToString("D2") + dt.Day.ToString("D2");
Excel.Workbooks workbooks = xlApp.Workbooks;
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"data" + date + @".xlsx"))
workbook = workbooks.Add(AppDomain.CurrentDomain.BaseDirectory + @"data" + date + @".xlsx"); //加载模板
else
{
File.Copy(AppDomain.CurrentDomain.BaseDirectory + @"data.xlsx", AppDomain.CurrentDomain.BaseDirectory + @"data" + date + @".xlsx", true);
workbook = workbooks.Add(AppDomain.CurrentDomain.BaseDirectory + @"data" + date + @".xlsx"); //加载模板
}
Excel.Sheets sheets = workbook.Sheets;
Excel._Worksheet worksheet = (Excel._Worksheet)sheets.get_Item(1); //第一个工作薄。
if (worksheet == null)
return; //工作薄中没有工作表.

//Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
//Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Open(AppDomain.CurrentDomain.BaseDirectory + @"data.xlsx", Missing.Value, Missing.Value, Missing.Value, Missing.Value,Missing.Value, Missing.Value, Missing.Value, Missing.Value,Missing.Value, Missing.Value, Missing.Value, Missing.Value);
//Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1
worksheet.Name = date;
string strcaption = "";
//写入标题
worksheet.Cells[1, 1] = "时间";
for (int i = 0; i {
worksheet.Cells[1, i + 2] = dataGridView1.Rows[i].Cells[1].Value;
}
//写入数值

worksheet.Cells[hh + 1, 1] = hour.ToString() + "时" + min.ToString() + "分";
for (int r = 0; r {
worksheet.Cells[hh + 1, r + 2] = dataGridView1.Rows[r].Cells[4].Value;

System.Windows.Forms.Application.DoEvents();
//System.Console.WriteLine("{0}\r\n", r.ToString());
//strcaption = " 导出... " + r.ToString() + " of " + dataGridView1.Rows.Count.ToString();
//this.Text = strcaption;
}
worksheet.Columns.EntireColumn.AutoFit();//列宽自适应

try
{
workbook.Saved = true;
//workbook.SaveCopyAs(AppDomain.CurrentDomain.BaseDirectory + @"data.xlsx");
workbook.SaveAs(AppDomain.CurrentDomain.BaseDirectory + @"data" + date + @".xlsx");

}
catch (Exception ex)
{
//MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
}

workbook.Close(Missing.Value, Missing.Value, Missing.Value);
xlApp.DisplayAlerts = true;
xlApp.Quit();
xlApp = null;
//int id = 0;

GC.Collect();//强行销毁

//System.Diagnostics.Process.Start(savefilename);

}

技术图片

 

 

c# excel文件追加

标签:==   except   jpg   win   get   数值   microsoft   网上   data   

原文地址:https://www.cnblogs.com/mainmaster/p/12031033.html


评论


亲,登录后才可以留言!