如何在Windows Server 2012上使用C#和asp.net创建Excel文件并将数据填入其中
大家好,我正在尝试创建一个excel,填写并下载它。我尝试了很多不同的事情,但最近我读到,Windows Server 2012不支持Microsoft.Office.Interop.Excel,我应该如何做不同的事情 这是我尝试的代码。它在本地版本上运行良好,但在服务器上似乎不起作用。我希望有一些建议或者一些我可以使用的代码 非常感谢如何在Windows Server 2012上使用C#和asp.net创建Excel文件并将数据填入其中,c#,asp.net,C#,Asp.net,大家好,我正在尝试创建一个excel,填写并下载它。我尝试了很多不同的事情,但最近我读到,Windows Server 2012不支持Microsoft.Office.Interop.Excel,我应该如何做不同的事情 这是我尝试的代码。它在本地版本上运行良好,但在服务器上似乎不起作用。我希望有一些建议或者一些我可以使用的代码 非常感谢 using (ExcelPackage excel = new ExcelPackage()) { excel.Workbook.Wo
using (ExcelPackage excel = new ExcelPackage())
{
excel.Workbook.Worksheets.Add("Kalkulation");
//On the Server is a differen Path
FileInfo excelFile = new FileInfo(@"Path");
excel.SaveAs(excelFile);
Microsoft.Office.Interop.Excel.Application oXL = null;
Microsoft.Office.Interop.Excel._Workbook oWB = null;
Microsoft.Office.Interop.Excel._Worksheet oSheet = null;
try
{
oXL = new Microsoft.Office.Interop.Excel.Application();
//On the Server is a differen Path
oWB = oXL.Workbooks.Open(@"Path");
oSheet = String.IsNullOrEmpty("Kalkulation") ? (Microsoft.Office.Interop.Excel._Worksheet)oWB.ActiveSheet : (Microsoft.Office.Interop.Excel._Worksheet)oWB.Worksheets["Kalkulation"];
oSheet.Cells[1, 2] = "Kalkulation: " + lbl_title.Text;
oSheet.Cells[3, 2] = "Produktname";
oSheet.Cells[3, 3] = "Dimension";
oSheet.Cells[3, 4] = "Preis";
oSheet.Cells[3, 5] = "Zeit";
oSheet.Cells[3, 6] = "Anzahl";
oSheet.Cells[3, 7] = "Totalzeit";
oSheet.Cells[3, 8] = "Totalpreis";
oSheet.get_Range("C1", "H100").Cells.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
oSheet.get_Range("B1", "B1").Font.Bold = true;
oSheet.get_Range("B1", "B1").Font.Size = 20;
oSheet.get_Range("B1", "B1").RowHeight = 59;
oSheet.get_Range("D1", "D1").Font.Bold = true;
oSheet.get_Range("D1", "D1").Font.Size = 20;
oSheet.get_Range("D1", "D1").RowHeight = 59;
oSheet.get_Range("B3", "H3").Font.Bold = true;
oSheet.get_Range("B3", "H3").Font.Size = 12;
oSheet.get_Range("B3", "H3").RowHeight = 32;
oSheet.Rows.RowHeight = 25;
oSheet.Rows.ColumnWidth = 25;
int s = 4;
foreach (RepeaterItem rptItem in rpt.Items)
{
s++;
System.Web.UI.WebControls.Label lblName = (System.Web.UI.WebControls.Label)rptItem.FindControl("lblName");
System.Web.UI.WebControls.Label lbl_Dimension_O = (System.Web.UI.WebControls.Label)rptItem.FindControl("lbl_Dimension_O");
System.Web.UI.WebControls.Label lbl_Dimension_U = (System.Web.UI.WebControls.Label)rptItem.FindControl("lbl_Dimension_U");
System.Web.UI.WebControls.Label lbl_price = (System.Web.UI.WebControls.Label)rptItem.FindControl("lbl_price");
System.Web.UI.WebControls.Label lbl_time = (System.Web.UI.WebControls.Label)rptItem.FindControl("lbl_time");
System.Web.UI.WebControls.Label lbl_quantity = (System.Web.UI.WebControls.Label)rptItem.FindControl("lbl_quantity");
System.Web.UI.WebControls.Label lbl_fulltime = (System.Web.UI.WebControls.Label)rptItem.FindControl("lbl_fulltime");
System.Web.UI.WebControls.Label lbl_fullprice = (System.Web.UI.WebControls.Label)rptItem.FindControl("lbl_fullprice");
oSheet.Cells[s, 2] = lblName.Text;
if (!String.IsNullOrEmpty(lbl_Dimension_O.Text) || !String.IsNullOrEmpty(lbl_Dimension_U.Text))
{
oSheet.Cells[s, 3] = lbl_Dimension_O.Text + " / " + lbl_Dimension_U.Text;
}
else
{
oSheet.Cells[s, 3] = "";
}
oSheet.Cells[s, 4] = lbl_price.Text;
oSheet.Cells[s, 5] = lbl_time.Text;
oSheet.Cells[s, 6] = lbl_quantity.Text;
oSheet.Cells[s, 7] = lbl_fulltime.Text;
oSheet.Cells[s, 8] = lbl_fullprice.Text;
}
s = s + 2;
oSheet.Cells[s, 6] = "Provisorisch Total: ";
oSheet.get_Range("F" + s, "F" + s + 3).Font.Bold = true;
oSheet.Cells[s, 7] = lblTotalTime.Text;
oSheet.Cells[s, 8] = lblTotal.Text;
s++;
if (!String.IsNullOrEmpty(lblTotalwRabatt_2.Text) || !String.IsNullOrEmpty(lblTotalwRabatt.Text))
{
oSheet.Cells[s, 6] = "Korrekur:";
oSheet.get_Range("F" + s, "F" + s).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
if (!String.IsNullOrEmpty(lblTotalwRabatt_2.Text))
{
oSheet.Cells[s, 7] = Minus_2.Text + " " + lblDifferenz_2.Text;
oSheet.get_Range("G" + s, "G" + s).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
}
if (!String.IsNullOrEmpty(lblTotalwRabatt.Text))
{
oSheet.Cells[s, 8] = lblMinus.Text + " " + lblDifferenz.Text;
oSheet.get_Range("H" + s, "H" + s).Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
}
s++;
oSheet.Cells[s, 6] = "Total:";
if (!String.IsNullOrEmpty(lblTotalwRabatt_2.Text))
{
oSheet.Cells[s, 7] = lblTotalwRabatt_2.Text;
oSheet.get_Range("G" + s, "G" + s).Font.Bold = true;
oSheet.get_Range("G" + s, "G" + s).Font.Size = 12;
}
if (!String.IsNullOrEmpty(lblTotalwRabatt.Text))
{
oSheet.Cells[s, 8] = lblTotalwRabatt.Text;
oSheet.get_Range("H" + s, "H" + s).Font.Bold = true;
oSheet.get_Range("H" + s, "H" + s).Font.Size = 12;
}
}
oWB.Save();
Response.AppendHeader("Content-Disposition", "attachment; filename=Details.xlsx");
}
catch (Exception ex)
{
}
finally
{
if (oWB != null)
oWB.Close();
}
//On the Server is a differen Path
System.Diagnostics.Process.Start(@"Path");
有例外吗?没有,它下载并打开excel文件,当我用IIS启动网站并通过internet访问它时,它会下载.aspx文件。对于debug,它不会给出任何异常OK,以及.aspx文件中有什么?为什么要调用这个
System.Diagnostics.Process.Start(@“Path”)代码>?天哪,我想我明白了。ExcelPackage
是否来自EPPlus
nuget包?