1.از اونجایی که چند سطر اول فایل اکسل من فرمت یکسانی ندارند (هم در نوع سلولها و هم در تعداد سلول ها متفاوتند ) ، مجبورم 6 خط اول رو که برای من ارزش داده ایی ندارند ، حذف کنم که کدش رو پیدا کردم و برای بقیه دوستان هم قرار میدم .
اول به references پروژه ، Microsoft.Office.Interop.Excel رو add کنید .
publicvoid DeleteExcelRows(string filePath,int RowNumber)
{
System.Threading.Thread.CurrentThread.CurrentCulture =
new System.Globalization.CultureInfo("en-US");
string xlsPath = filePath;
Microsoft.Office.Interop.Excel.Application excelApp =
new Microsoft.Office.Interop.Excel.ApplicationClass();
Microsoft.Office.Interop.Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(xlsPath, 0,
false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
Microsoft.Office.Interop.Excel.Worksheet sheet =
(Microsoft.Office.Interop.Excel.Worksheet)excelWorkbook.Worksheets.get_Item(Sheet1);//sheetName
Microsoft.Office.Interop.Excel.Range ran =
(Microsoft.Office.Interop.Excel.Range)sheet.Rows[RowNumber, Type.Missing];
ran.Select();
ran.Delete(Microsoft.Office.Interop.Excel.XlDirection.xlUp);
string tmpName = System.IO.Path.GetTempFileName();
System.IO.File.Delete(tmpName);
excelWorkbook.SaveAs(tmpName, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
excelWorkbook.Close(false, Type.Missing, Type.Missing);
excelApp.Quit();
System.IO.File.Delete(xlsPath);
System.IO.File.Move(tmpName, xlsPath);
}