110,561
社区成员
发帖
与我相关
我的任务
分享
Excel.Application app = null;
Excel.Workbook wb = null;
Excel.Worksheet ws = null;
Excel.Range rng = null;
try
{
app = new Excel.Application(); // 创建application对象
app.Visible = false;
app.ScreenUpdating = false;
// 打开文件
wb = app.Workbooks.Open(@"D:\test.xls", Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing);
ws = (Excel.Worksheet)wb.Sheets[1];// 指定worksheet
rng = ws.get_Range("A1", Missing.Value);// 获取单元格
// 如果有批注则删除
if (rng.Comment != null)
{
object obj = rng.Comment;
rng.Comment.Delete();
}
rng.AddComment("comment changed" + DateTime.Now);
wb.Save(); // 保存
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
wb.Close(true, Type.Missing, Type.Missing);
app.Quit();
wb = null;
app = null;
GC.Collect();
}