可以获取数据,我们也需要可以修改或者赋值给指定的单元格,需要指定行、列以及要给的赋值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
using System.Diagnostics;
using System.Reflection;
namespace ConsoleApplication3
{
class EditExcel
{
#region 给Excel指定单元赋值
/// <summary>
/// 给Excel指定单元赋值
/// </summary>
/// <param name="ExcelName"></param>
public void SetCellValue(string ExcelName)
{
Application App = new Application();
//获取缺少的object类型值
object missing = Missing.Value;
//打开指定的Excel文件
Workbook openwb = App.Workbooks.Open(ExcelName, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
//获取选选择的工作表
Worksheet ws = ((Worksheet)openwb.Worksheets["Sheet1"]);
Console.WriteLine("请输入你行号:");
int x = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("请输入你列号:");
int y = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("请输入要设置的值:");
string value = Console.ReadLine();
ws.Cells[x, y] = value;
Console.WriteLine("设置成功!");
Console.ReadLine();
App.DisplayAlerts = false;//不现实提示对话框
openwb.Save();//保存工作表
App.Visible = true;//显示Excel
openwb.Close(false, missing, missing);//关闭工作表
App.Quit();
}
#endregion
}
}
欢迎关注本人的公众号:编程手札,文章也会在公众号更新