我搜索了S / O,但无法找到有助于解决问题的解决方案。我从我的asp.net应用程序导出数据到xls文件。当用户单击“导出数据”按钮时,我需要显示“打开/另存为/取消”对话框。
我如何做到这一点?
oSheet = Nothing
oRange = Nothing
oWB.SaveAs(sFileName.ToString())
oWB.Close()
oWB = Nothing
oXL.Quit()
答案 0 :(得分:4)
试试这个代码块。你可能需要微调它。代码的Response
块与对话框有关:
Private Sub GenerateXLSXFile(tbl As DataTable)
Dim excelPackage = New ExcelPackage
Dim excelWorksheet = excelPackage.Workbook.Worksheets.Add("DemoPage")
excelWorksheet.Cells("A1").LoadFromDataTable(tbl, True)
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Response.AddHeader("content-disposition", "attachment; filename=ExcelDemo.xlsx")
Dim stream As MemoryStream = New MemoryStream(excelPackage.GetAsByteArray())
Response.OutputStream.Write(stream.ToArray(), 0, stream.ToArray().Length)
Response.Flush()
Response.Close()
End Sub
答案 1 :(得分:3)
您可以通过多种方式完成此操作。您提供的代码示例与此无关,但显然您创建了文件并将其保存到磁盘。看到你有文件路径,你可以使用Response.WriteFile。您需要添加Content-Disposition以显示“另存为”对话框。
Response.AddHeader("Content-Disposition", "attachment; filename=EXCELFILE.xs")
Response.ContentType = "application/ms-excel" // not sure you need the contenttype, and it may be different
Response.WriteFile(FULLPATHTOFILE)
答案 2 :(得分:0)
嗯。你不。您的excel对象正在Web服务器上的ASP.NET进程中运行。无法在CLIENT(BROWSER)端显示对话框。
答案 3 :(得分:0)
public void export()
{
try
{
if (Session["ExcelGrid"] != null)
{
DataSet DS = null;
if (Session["ExcelGrid"] != null)
{
DS = (DataSet)Session["ExcelGrid"];
if (DS.Tables[0].Rows.Count > 0)
{
int j = 0;
int k = 0;
string FileName = string.Concat(DateTime.Now.ToString(), "Order");
DataTable dt = DS.Tables[0];
dt.Columns.Remove("UserRole");
dt.Columns.Remove("iControlDeviationID");
dt.Columns.Remove("sErrorCode");
dt.Columns.Remove("sItemCategory");
dt.Columns.Remove("sItemSubCategory");
dt.Columns.Remove("iDeviationID");
dt.Columns.Remove("iControlPointID");
dt.Columns.Remove("sFacility");
dt.Columns.Remove("sItemCategory1");
dt.Columns.Remove("sItemSubCategory1");
dt.Columns.Remove("sRegion");
dt.Columns.Remove("sCountry");
dt.Columns.Remove("sItem");
dt.Columns["dRegisterDate"].SetOrdinal(5);
Context.Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName + ".xls");
Context.Response.ContentType = "application/vnd.ms-excel";
Context.Response.Charset = "";
Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
Context.Response.Write("<table border=0 width='100%'><tr align = 'center'>");
for (j = 0; j < 30; j++)
{
if (j < 1)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
if (dt.Columns[i].Caption.ToString() == "sDeviation")
Context.Response.Write("<td style='border:#D0D7E5 1px solid; border-left:none;border-bottom:none'><b>" + Resources.Resource.DeviationTypes.ToString().ToUpper() + "</b></td>");
if (dt.Columns[i].Caption.ToString() == "sDeviationCriticalLevel")
Context.Response.Write("<td style='border:#D0D7E5 1px solid; border-left:none;border-bottom:none'><b>" + Resources.Resource.DeviationCriticalLevel.ToString().ToUpper() + "</b></td>");
if (dt.Columns[i].Caption.ToString() == "sObject")
Context.Response.Write("<td style='border:#D0D7E5 1px solid; border-left:none;border-bottom:none'><b>" + Resources.Resource.Object.ToString().ToUpper() + "</b></td>");
if (dt.Columns[i].Caption.ToString() == "sControlArea")
Context.Response.Write("<td style='border:#D0D7E5 1px solid; border-left:none;border-bottom:none'><b>" + Resources.Resource.ControlArea.ToString().ToUpper() + "</b></td>");
if (dt.Columns[i].Caption.ToString() == "sControlPoint")
Context.Response.Write("<td style='border:#D0D7E5 1px solid; border-left:none;border-bottom:none'><b>" + Resources.Resource.ControlPoint.ToString().ToUpper() + "</b></td>");
if (dt.Columns[i].Caption.ToString() == "dRegisterDate")
Context.Response.Write("<td style='border:#D0D7E5 1px solid; border-left:none;border-bottom:none'><b>" + Resources.Resource.RegisteredDate.ToString().ToUpper() + "</b></td>");
}
}
else
{
Context.Response.Write("<td style='border:#D0D7E5 1px solid; border-left:none;border-bottom:none'>" + " " + "</td>");
}
}
Context.Response.Write(" ");
Context.Response.Write("</tr>");
for (int l = 0; l < 100; l++)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
Context.Response.Write("<tr align = 'Left'>");
for (k = 0; k < j; k++)
{
if (k < 1)
{
for (int iCol = 0; iCol < dt.Columns.Count; iCol++)
{
if (l < 1)
{
Context.Response.Write("<td style='border:#D0D7E5 1px solid; border-left:none;border-bottom:none'>" + dr[iCol].ToString() + "</td>");
}
else
{
Context.Response.Write("<td style='border:#D0D7E5 1px solid; border-left:none;border-bottom:none'>" + " " + "</td>");
}
}
}
else
{
Context.Response.Write("<td style='border:#D0D7E5 1px solid; border-left:none;border-bottom:none'>" + " " + "</td>");
}
}
}
}
Context.Response.Write(" ");
Context.Response.Write("</tr>");
Context.Response.Write(" ");
Context.Response.Write("</table>");
Response.Buffer = true;
Context.Response.Flush();
Context.Response.Close();
Context.Response.End();
//ScriptManager.RegisterStartupScript(this, GetType(), "refresh", "window.setTimeout('window.location.reload(true);',5000);", true);
}
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
finally
{
ScriptManager.RegisterStartupScript(this, GetType(), "refresh", "a.aspx;", true);
}
}