COM 操作Excel文档问题
通过Com读取Excel文档后文档的目录下出现了临时文件 ~$***.xlsx 但是关闭文档后这个文件还在,如果下次打开这个文档就会提示这个文档正在编辑。。。这个问题怎么解决呢?
如下是操作Excel的类
public class ExcelDocument
{
string filePath;
private ExcelAPI.Workbook workbook;
public ExcelDocument(string filePath)
{
this.filePath = filePath;
}
public ExcelAPI.Workbook OpenDocument()
{
return this.openDocument(filePath);
}
public ExcelAPI.Workbook openDocument(string fileName)
{
object MissingValue = Type.Missing;
ExcelAPI.Application app = new ExcelAPI.Application();
workbook = app.Workbooks.Open(fileName, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue, MissingValue, MissingValue,
MissingValue);
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null;
return workbook;
}
public void CloseDocument()
{
for (int i = 1; i < workbook.Sheets.Count; i++)
{
ExcelAPI.Worksheet workSheet = (ExcelAPI.Worksheet)workbook.Sheets[i];
System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
workSheet = null;
}
workbook.Close(true, null, null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
GC.Collect();
}
}