C# 创建以及修改Excel
C# 创建以及修改Excel
添加引用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
namespace ConsoleApp2
{<!-- -->
public class CreateExcel
{<!-- -->
Application excel = new Application();
public Workbook workbook;
public Worksheet worksheet;
public int index;
public CreateExcel()
{<!-- -->
excel.Visible = false;
workbook = excel.Workbooks.Add(System.Reflection.Missing.Value);
worksheet =(Worksheet) workbook.Sheets[1];
index = worksheet.Cells.SpecialCells(XlCellType.xlCellTypeLastCell).Row;
worksheet.Range[string.Format("A{0}", index)].Value = "fileVersion";
worksheet.Range[string.Format("B{0}", index)].Value = "productVersion";
worksheet.Range[string.Format("C{0}", index)].Value = "fileName";
}
public void NewSheet()
{<!-- -->
Worksheet newsheeet = excel.Worksheets.Add(After: this.worksheet);
}
public void Save(string path)
{<!-- -->
this.workbook.SaveAs(path,Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,XlSaveAsAccessMode.xlShared, Type.Missing, Type.Missing, Type.Missing);
}
public void fill(string fileVersion, string productVersion,string fileName)
{<!-- -->
worksheet.Range[string.Format("A{0}",index+1)].Value = fileVersion;
worksheet.Range[string.Format("B{0}", index+1)].Value = productVersion;
worksheet.Range[string.Format("C{0}", index+1)].Value = fileName;
index += 1;
}
}
}