Microsoft.Office.Interop.Excel.Application MSexcel = new Microsoft.Office.Interop.Excel.Application(); Microsoft.Office.Interop.Excel.Workbook excelPrj = null; /*Microsoft.Office.Interop.Word.Application MSword = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.PowerPoint.Application MSpoint = new Microsoft.Office.Interop.PowerPoint.Application(); Microsoft.Office.Interop.Access.Application MSAccs = new Microsoft.Office.Interop.Access.Application(); Microsoft.Office.Interop.Word.Document wordPrj = null; Microsoft.Office.Interop.PowerPoint.Presentation pointPrj = null;*/ Missing miss = Missing.Value; private bool RepairFile(string brokenFile) { if (System.IO.Path.GetExtension(brokenFile).ToLower() == ".xlsx") { try { string OldFile = brokenFile; MSexcel.DisplayAlerts = false; MSexcel.Visible = false; MSexcel.UserControl = false; excelPrj = MSexcel.Workbooks.Open(OldFile, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlCorruptLoad.xlRepairFile); if (excelPrj != null) { excelPrj.Close(true); } return true; } catch { return false; } } else { /* if (System.IO.Path.GetExtension(brokenFile).ToLower() == ".docx") { }*/ return true; } }
if (File.Exists(brokenFile)) { File.Delete(brokenFile); }
if (excelPrj != null) { if (System.IO.File.Exists(brokenFile)) { Wtime = System.IO.File.GetLastWriteTime(brokenFile); System.IO.File.Delete(brokenFile); } excelPrj.SaveAs(brokenFile, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, miss, miss,false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,miss, miss, miss, miss, miss); excelPrj.Close(true); System.IO.File.SetLastWriteTime(brokenFile, Wtime); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)