0

我試圖創建一個使用Microsoft.Office.Interop.Excel一個Excel文件,而這似乎是我的本地機器上使用下面的代碼工作:無法保存到Azure雲服務的文件夾目錄

//Start Excel and get Application object. 
oXL = new Microsoft.Office.Interop.Excel.Application(); 
oXL.Visible = true; 

//Get a new workbook. 
oWB = (Microsoft.Office.Interop.Excel._Workbook)(oXL.Workbooks.Add("")); 
oSheet = (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet; 

//Add Contact Details for Manager 
oSheet.Cells[1, 1] = "Account Manager"; 
oSheet.Cells[2, 1] = "Manager Name"; 
oSheet.Cells[3, 1] = "+44(0)2871262626"; 
oSheet.Cells[4, 1] = "[email protected]"; 
oSheet.get_Range("A1", "A4").Font.Bold = true; 

oXL.DisplayAlerts = false; 
oWB.SaveAs(Server.MapPath("~/TransitFiles/") + bill.Landlord.Name + " Group Bill " + bill.LandlordBillID + ".xlsx", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 

當我嘗試將其部署到我的天藍雲服務我無法生成文件,儘管複製了dll並創建了相關的文件夾位置,這是由於我放置在那裏的holder.txt文檔,顯示Excel的最佳方式是什麼文件爲ActionResult in MVC

回答

0

當我嘗試這個我不能生成文件儘管有該dll

部署到我的Azure雲服務由於沒有沒有微軟Office目前在Azure上WebRole所以我們不能使用Office InterOP DLL。

我們可以使用OpenXml SDK。我做了一個演示,它在我身邊正常工作。以下是根據您的代碼的演示代碼。我們還可以從document獲得更多關於使用OPenXML運行excel的代碼。

 var fileName = Server.MapPath("~/")[email protected]"OpenXMltest.xlsx"; 
     using (SpreadsheetDocument document = SpreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook)) 
     { 
      WorkbookPart workbookPart = document.AddWorkbookPart(); 
      workbookPart.Workbook = new Workbook(); 

      WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>(); 
      var sheetData = new SheetData(); 
      worksheetPart.Worksheet = new Worksheet(sheetData); 

      Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets()); 

      Sheet sheet = new Sheet() { Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Test Sheet" }; 

      List<String> columns = new List<string>(); 

      Row newRow1 = new Row(); 
      Row newRow2 = new Row(); 
      Row newRow3 = new Row(); 
      Row newRow4 = new Row(); 

      Cell cell1 = new Cell 
      { 
       DataType = CellValues.String, 
       CellValue = new CellValue("Account Manager") 
      }; 
      Cell cell2 = new Cell 
      { 
       DataType = CellValues.String, 
       CellValue = new CellValue("Manager Name") 
      }; 
      Cell cell3 = new Cell 
      { 
       DataType = CellValues.String, 
       CellValue = new CellValue("+44(0)2871262626") 
      }; 
      Cell cell4 = new Cell 
      { 
       DataType = CellValues.String, 
       CellValue = new CellValue("[email protected]") 
      }; 

      newRow1.AppendChild(cell1); 
      newRow2.AppendChild(cell2); 
      newRow3.AppendChild(cell3); 
      newRow4.AppendChild(cell4); 
      sheetData.AppendChild(newRow1); 
      sheetData.AppendChild(newRow2); 
      sheetData.AppendChild(newRow3); 
      sheetData.AppendChild(newRow4); 
      sheets.Append(sheet); 

      workbookPart.Workbook.Save(); 
     } 

什麼是顯示在Excel文檔作爲MVC一個ActionResult的最佳方式?

如果我們想顯示文檔,根據我的經驗,我們可以使用dataview來做到這一點。

相關問題