无法接近C#中的特定工作表

时间:2016-05-17 14:08:44

标签: c# excel interop

我想在Excel文件中搜索特定的工作表。我尝试使用以下方法来处理此表:

Microsoft.Office.Interop.Excel.Worksheet sheet = workBook.Worksheets[1];

但这不会以某种方式起作用,有谁能告诉我我做错了什么?提前致谢

public List<string> ExcelReader(string fileLocation, int sheetnummer)
{
    Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Workbook workBook = excel.Workbooks.Open(fileLocation);

    Microsoft.Office.Interop.Excel.Worksheet sheet = workBook.Worksheets[1]; // this doesn't work

    workBook.SaveAs(fileLocation + ".csv",
                    Microsoft.Office.Interop.Excel.XlFileFormat.xlCSVWindows);
    workBook.Close(true);

    excel.Quit();

    List<string> valueList = null;

    using (StreamReader sr = new StreamReader(fileLocation + ".csv"))
    {
        string content = sr.ReadToEnd();
        valueList = new List<string>(
                    content.Split(
                        new string[] { "\r\n" },
                        StringSplitOptions.RemoveEmptyEntries
                    )
                );
    }

    new FileInfo(fileLocation + ".csv").Delete();

    return valueList;
}

0 个答案:

没有答案