2015-10-19 35 views
0

我有这样的代码,以我的GridView的导出到Excel,它工作在本地主机,但在部署后不起作用。单击我的导出按钮时收到的错误是运行时错误。导出到Excel函数在本地主机工作,但不是在发布的网站

protected void EXPORT_BUTTON_Click(object sender, EventArgs e) 
     { 
      Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); 

      // creating new WorkBook within Excel application 
      Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); 
      String DATA1 = "DATA1"; 
      String DATA2 = "DATA2"; 
      ExportToExcel(app, workbook, DATA_1, DATA1); 
      workbook.Worksheets["Sheet1"].Delete(); 
      workbook.Worksheets["Sheet2"].Delete(); 
      workbook.Worksheets["Sheet3"].Delete(); 
      ExportToExcel(app, workbook, DATA_2, DATA2); 
      string FolderPath = ServerName + DirectoryLocation + DirectoryFolder + ExportsFolder; 
      var filename = @"EXCEL_" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".xlsx"; 
      workbook.SaveAs(FolderPath + filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 
      workbook.Close(); 
      app.Quit(); 
      Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
      Response.AddHeader("content-disposition", "attachment; filename=" + filename + ";"); 
      Response.TransmitFile(FolderPath + filename); 
      Response.Flush(); 
      Response.End(); 
     } 

     public void ExportToExcel(Microsoft.Office.Interop.Excel._Application app, Microsoft.Office.Interop.Excel._Workbook workbook, GridView gridview, string SheetName) 
     { 
      // see the excel sheet behind the program 
      app.Visible = false; 

      Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets.Add(); 

      // changing the name of active sheet 
      worksheet.Name = SheetName; 

      // storing header part in Excel 
      for (int i = 1; i < gridview.Columns.Count + 1; i++) 
      { 
       worksheet.Cells[1, i] = gridview.Columns[i - 1].HeaderText; 
      } 


      // storing Each row and column value to excel sheet 
      for (int i = 0; i < gridview.Rows.Count - 1; i++) 
      { 
       for (int j = 0; j < gridview.Columns.Count; j++) 
       { 
        worksheet.Cells[i + 2, j + 1] = gridview.Rows[i].Cells[j].Text.ToString(); 
       } 
      } 

     } 
+0

愚蠢的问题,但在服务器上安装Office? – MyDaftQuestions

+0

@MyDaftQuestions no。但不能生成文件类型?× –

+0

如果服务器上没有安装Office,可以按照Greco的建议进行csv导出。如果你需要格式化,公式支持等,那么你将不得不使用第三方库。 .NET也有很多可用的,开源的。 – haraman

回答

1

我解决了使用EPPLUS我的问题,EPPlus是一个.NET库,读取和写入使用开放式的Office XML格式(XLSX)的Excel 2007/2010文件。它不需要在服务器上安装excel。谢谢。

下面的代码是一个参考:

protected void ExportToExcel_Click(object sender, EventArgs e) 
    { 
     var products = GetProducts(); 
     GridView1.DataSource = products; 
     GridView1.DataBind(); 
     ExcelPackage excel = new ExcelPackage(); 
     var workSheet = excel.Workbook.Worksheets.Add("Products"); 
     var totalCols = GridView1.Rows[0].Cells.Count; 
     var totalRows = GridView1.Rows.Count; 
     var headerRow = GridView1.HeaderRow; 
     for (var i = 1; i <= totalCols; i++) 
     { 
      workSheet.Cells[1, i].Value = headerRow.Cells[i - 1].Text; 
     } 
     for (var j = 1; j <= totalRows; j++) 
     { 
      for (var i = 1; i <= totalCols; i++) 
      { 
       var product = products.ElementAt(j-1); 
       workSheet.Cells[j + 1, i].Value = product.GetType().GetProperty(headerRow.Cells[i - 1].Text).GetValue(product, null); 
      } 
     } 
     using (var memoryStream = new MemoryStream()) 
     { 
      Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
      Response.AddHeader("content-disposition", "attachment; filename=products.xlsx"); 
      excel.SaveAs(memoryStream); 
      memoryStream.WriteTo(Response.OutputStream); 
      Response.Flush(); 
      Response.End(); 
     } 
    } 
0

'的Microsoft.Office.Interop.Excel' 意味着必须安装Microsoft Office运行时,使此代码的工作。

使用Microsoft.Office.Interop.Excel导出数据是可以避免的,因为它需要在服务器上安装应用程序,可扩展性差,且不符合MS Office许可条件。

我建议其他的方式,如文本,CSV出口将满足在服务器上更好的性能。

protected void EXPORT_BUTTON_Click(object sender, EventArgs e) 
{ 
... 
StringBuilder sb = new StringBuilder(); 

// storing header part in Excel 
for (int i = 1; i < gridview.Columns.Count + 1; i++) 
{ 
    sb.Append (gridview.Columns[i - 1].HeaderText); 
    stringBuilder.Append(","); 
} 

sb.Append(Environment.NewLine); 

// storing Each row and column value to excel sheet 
for (int i = 0; i < gridview.Rows.Count - 1; i++) 
{ 
    for (int j = 0; j < gridview.Columns.Count; j++) 
    { 
     sb.Append (gridview.Rows[i].Cells[j].Text.ToString()); 
     stringBuilder.Append(","); 
    } 
    sb.Append(Environment.NewLine); 
    } 

    WriteToCSV(sb.ToString(); 
} 

public static void WriteToCSV(string text, string fileName) 
    { 
     string attachment = "attachment; filename="+fileName+DateTime.Now.ToString("yy-MM-dd")+".csv"; 
     HttpContext.Current.Response.Clear(); 
     HttpContext.Current.Response.ClearHeaders(); 
     HttpContext.Current.Response.ClearContent(); 
     HttpContext.Current.Response.AddHeader("content-disposition", attachment); 
     HttpContext.Current.Response.ContentType = "text/csv;charset=utf8"; 
     HttpContext.Current.Response.AddHeader("Pragma", "public"); 

     HttpContext.Current.Response.Write('\uFEFF'); //this is BOM 
     HttpContext.Current.Response.Write(text); 
     HttpContext.Current.Response.End(); 
    } 
+0

嗨,我可以检查与你可以说如果即时通讯使用OpenXML SDK,Excel仍然需要安装在服务器端为它的工作目的我说得那么对吗? –

+0

不! OpenXML非常适合服务器应用程序,无需安装即可工作。 –

+0

请发布你的结果,如果你会使它工作 –

0

它失败的原因是您没有安装Excel。此处还有其他选项(例如转换为CSV文件),但如果要按原样使用代码,最简单的方法是在服务器上安装Excel!

相关问题