我正在使用 OpenFile 对话框打开一个文件,并且我想确认该文件是 excel 格式。
我打开的文件是“C:\Desktop\Distribution.xls”,但我的 if 语句的两个条件都评估为真。我应该使用另一种方法吗?
DialogResult result = openFileDialog1.ShowDialog();
if (result==DialogResult.OK)
{
file = openFileDialog1.FileName;
file = file.Trim();
if (!file.EndsWith(".xlsx")||!file.EndsWith(".xls"))
{
MessageBox.Show("Incorrect file format. Please save file in an .xls format");
}
else
{
book = application.Workbooks.Open(file);
sheet = (Worksheet)book.Worksheets[1];
range = sheet.get_Range("A1", "A1".ToString());
range.EntireRow.Delete(XlDirection.xlUp);
sheet.Cells[1, 2].EntireColumn.NumberFormat = "@";
book.SaveAs(csvConverstion, XlFileFormat.xlCSV);
book.Close(false, Type.Missing, Type.Missing);
application.Quit();
}