excel interop saveas不工作

时间:2015-01-12 14:07:19

标签: c# asp.net-mvc excel office-interop

我有以下代码用于在c#中创建和保存excel文件,但是当它完成时,没有为我的桌面创建文件,我无法弄清楚我做错了什么:

Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();

xlApp.Visible = false;

Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
Worksheet ws = (Worksheet)wb.Worksheets[1];
Range rangeAToC = ws.get_Range("A1", "C1");

string[] headerRow = { "GIP Id", "First Name", "Last Name"};

int indexAtRow = 0;
foreach (Range cell in rangeAToC)
{
    cell.Value2 = headerRow[indexAtRow];
    indexAtRow++;
}

//Save report
wb.SaveAs("C:/Users/Abdul/Desktop/GipEmployeeReport.xls", Type.Missing,
 Type.Missing,Type.Missing, Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
 Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

//Close out COM objects
xlApp.Workbooks.Close();
xlApp.Quit();

1 个答案:

答案 0 :(得分:1)

我注意到的第一件事是你使用正斜杠(/)代替反斜杠\