
跪求! C# WINFORM应用程序对Excel 文件 编辑创建的 代码实例
跪求!C#WINFORM应用程序对Excel文件编辑创建的代码实例这个代码没有保存并关闭EXCEL文档哦。。保存并且关闭是怎样写的?...
跪求! C# WINFORM应用程序对Excel 文件 编辑创建的 代码实例
这个代码 没有 保存 并关闭 EXCEL 文档哦。。保存并且关闭 是怎样写的? 展开
这个代码 没有 保存 并关闭 EXCEL 文档哦。。保存并且关闭 是怎样写的? 展开
1个回答
展开全部
try
{
Excel.Application xls = new Excel.Application();
Excel.Workbook editBook = null;
Excel.Worksheet editSheet = null;
Excel.Workbooks excelBooks = (Excel.Workbooks)xls.Workbooks;
Workbook editBook = excelBooks.Open(excelPath, Type.Missing, true, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
editSheet = editBook.Sheets[1] as Excel.Worksheet;
Excel.Range targetCell = editSheet.get_Range("A1", "A1");
targetCell.Value2 = "要设定的值";
}
finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(targetCell);
System.Runtime.InteropServices.Marshal.ReleaseComObject(editSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(editBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelBooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xls);
}
{
Excel.Application xls = new Excel.Application();
Excel.Workbook editBook = null;
Excel.Worksheet editSheet = null;
Excel.Workbooks excelBooks = (Excel.Workbooks)xls.Workbooks;
Workbook editBook = excelBooks.Open(excelPath, Type.Missing, true, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
editSheet = editBook.Sheets[1] as Excel.Worksheet;
Excel.Range targetCell = editSheet.get_Range("A1", "A1");
targetCell.Value2 = "要设定的值";
}
finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(targetCell);
System.Runtime.InteropServices.Marshal.ReleaseComObject(editSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject(editBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excelBooks);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xls);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询