使用c#excel interop选择范围

时间:2017-09-20 18:52:00

标签: c# excel com-interop excel-interop

我无法在C#中使用excel interop选择范围。请让我知道我在这里做错了什么。我是excel interop的新手

   static void Main(string[] args)
    {
        Excel.Application xlApp = new Excel.Application();
        Excel.Workbook xlWorkbook = xlApp.Workbooks.Open("D:\\s1.xlsx");

        Excel.Range xlTestRange;
        Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[2];

        xlTestRange = xlWorksheet.UsedRange;
        xlTestRange.Select();
        xlWorkbook.Save();
        xlWorkbook.Close();
        xlApp.UserControl = true;
        xlApp.Quit();


    }

1 个答案:

答案 0 :(得分:0)

以下代码在LINQPad中为我工作:

var xlApp = new Excel.Application();
var xlWorkbook = xlApp.Workbooks.Open(@"path-to-excel-file");
var xlWorksheet = (Excel.Worksheet)xlWorkbook.Sheets[1];

var xlTestRange = xlWorksheet.UsedRange;
xlTestRange.Select();
xlWorkbook.Save();

xlWorkbook.Close();
xlApp.Quit();