c#网站项目调试状态下成功发布提示【未将对象引用设置到对象的实例】
错误信息如下:
“/csharp”应用程序中的服务器错误。
未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
源错误:
执行当前 Web 请求期间生成了未经处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。
堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
_Default.ExportExcelByDT(DataTable dt, String strExcelFileName) +971
_Default.ExportExcelFileByDT(HttpResponse Response, String strExcelFileName, DataTable dt) +45
_Default.Button1_Click(Object sender, EventArgs e) +83
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +118
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +112
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563
版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.272
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
页面就一个源码如下:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Excel;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
System.Data.DataTable dt = new System.Data.DataTable();
dt = dtMenu();
ExportExcelFileByDT(HttpContext.Current.Response, "aa", dt);
}
private System.Data.DataTable dtMenu()
{
System.Data.DataTable dt = new System.Data.DataTable();
//菜单ID
DataColumn column0 = new DataColumn();
column0.DataType = System.Type.GetType("System.String");
column0.AllowDBNull = false;
column0.Caption = "菜单ID";
column0.ColumnName = "MenuID";
//菜单级别
DataColumn column1 = new DataColumn();
column1.DataType = System.Type.GetType("System.String");
column1.AllowDBNull = false;
column1.Caption = "菜单级别";
column1.ColumnName = "MenuLevel";
//菜单名称
DataColumn column2 = new DataColumn();
column2.DataType = System.Type.GetType("System.String");
column2.AllowDBNull = false;
column2.Caption = "菜单名称";
column2.ColumnName = "MenuName";
//菜单显示文本
DataColumn column3 = new DataColumn();
column3.DataType = System.Type.GetType("System.String");
column3.AllowDBNull = false;
column3.Caption = "菜单显示文本";
column3.ColumnName = "MenuText";
//上级菜单
DataColumn column4 = new DataColumn();
column4.DataType = System.Type.GetType("System.String");
column4.AllowDBNull = false;
column4.Caption = "上级菜单";
column4.ColumnName = "MenuPre";
//子菜单
DataColumn column5 = new DataColumn();
column5.DataType = System.Type.GetType("System.String");
column5.AllowDBNull = false;
column5.Caption = "子菜单";
column5.ColumnName = "SubMenu";
//菜单打开应用
DataColumn column6 = new DataColumn();
column6.DataType = System.Type.GetType("System.String");
column6.AllowDBNull = false;
column6.Caption = "菜单打开应用";
column6.ColumnName = "MenuForm";
dt.Columns.Add(column0);
dt.Columns.Add(column1);
dt.Columns.Add(column2);
dt.Columns.Add(column3);
dt.Columns.Add(column4);
dt.Columns.Add(column5);
dt.Columns.Add(column6);
DataRow row;
row = dt.NewRow();
row["MenuID"] = "1";
row["MenuLevel"] = "1";
row["MenuName"] = "menu1";
row["MenuText"] = "一级菜单1";
row["MenuPre"] = "0";
row["SubMenu"] = "1";
row["MenuForm"] = "1";
dt.Rows.Add(row);
row = dt.NewRow();
row["MenuID"] = "2";
row["MenuLevel"] = "2";
row["MenuName"] = "menu2";
row["MenuText"] = "一级菜单2";
row["MenuPre"] = "3";
row["SubMenu"] = "0";
row["MenuForm"] = "frm1";
dt.Rows.Add(row);
row = dt.NewRow();
row["MenuID"] = "3";
row["MenuLevel"] = "1";
row["MenuName"] = "menu3";
row["MenuText"] = "一级菜单3";
row["MenuPre"] = "0";
row["SubMenu"] = "1";
row["MenuForm"] = "1";
dt.Rows.Add(row);
row = dt.NewRow();
row["MenuID"] = "4";
row["MenuLevel"] = "2";
row["MenuName"] = "menu4";
row["MenuText"] = "一级菜单1二级菜单1";
row["MenuPre"] = "1";
row["SubMenu"] = "0";
row["MenuForm"] = "frm2";
dt.Rows.Add(row);
row = dt.NewRow();
row["MenuID"] = "5";
row["MenuLevel"] = "2";
row["MenuName"] = "menu5";
row["MenuText"] = "一级菜单1二级菜单2";
row["MenuPre"] = "1";
row["SubMenu"] = "1";
row["MenuForm"] = "1";
dt.Rows.Add(row);
row = dt.NewRow();
row["MenuID"] = "6";
row["MenuLevel"] = "2";
row["MenuName"] = "menu6";
row["MenuText"] = "一级菜单2二级菜单1";
row["MenuPre"] = "2";
row["SubMenu"] = "0";
row["MenuForm"] = "1";
dt.Rows.Add(row);
row = dt.NewRow();
row["MenuID"] = "7";
row["MenuLevel"] = "3";
row["MenuName"] = "menu7";
row["MenuText"] = "一级菜单1二级菜单2三级菜单1";
row["MenuPre"] = "5";
row["SubMenu"] = "0";
row["MenuForm"] = "1";
dt.Rows.Add(row);
row = dt.NewRow();
row["MenuID"] = "8";
row["MenuLevel"] = "3";
row["MenuName"] = "menu8";
row["MenuText"] = "一级菜单1二级菜单2三级菜单2";
row["MenuPre"] = "5";
row["SubMenu"] = "0";
row["MenuForm"] = "1";
dt.Rows.Add(row);
return dt;
}
public void ExportExcelFileByDT(HttpResponse Response, string strExcelFileName, System.Data.DataTable dt)
{
string fileFullName = string.Empty;
fileFullName = ExportExcelByDT(dt, strExcelFileName);
try
{
string fileName = HttpUtility.UrlEncode(strExcelFileName + ".xls", Encoding.UTF8);
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.ContentType = "application/ms-excel";
FileStream streamFile = File.OpenRead(fileFullName);
byte[] byteFile = new byte[streamFile.Length];
streamFile.Read(byteFile, 0, int.Parse(streamFile.Length.ToString()));
Response.BinaryWrite(byteFile);
streamFile.Close();
streamFile.Dispose();
Response.End();
}
finally
{
File.Delete(fileFullName);
}
}
private static string ExportExcelByDT(System.Data.DataTable dt, string strExcelFileName)
{
string strFilePath = "e:/";
Microsoft.Office.Interop.Excel.Application app = null;
Microsoft.Office.Interop.Excel.Workbook wb = null;
Microsoft.Office.Interop.Excel.Worksheet sheet = null;
try
{
app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = false;
wb = (Microsoft.Office.Interop.Excel.Workbook)app.Workbooks.Add(Missing.Value);
sheet = (Microsoft.Office.Interop.Excel.Worksheet)wb.ActiveSheet;
int rowIndex = 0;
int colIndex = 0;
System.Data.DataTable table = dt;
rowIndex++;
colIndex = 0;
foreach (DataColumn col in table.Columns)
{
colIndex++;
sheet.Cells[rowIndex, colIndex] = @"'" + col.ColumnName.ToString();
}
foreach (DataRow row in table.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in table.Columns)
{
colIndex++;
sheet.Cells[rowIndex, colIndex] = @"'" + row[col.ColumnName].ToString();
}
}
string fileFullName = String.Format("{0}{1}.xls", strFilePath, DateTime.Now.Ticks);
app.ActiveWorkbook.SaveAs(fileFullName, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel7, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
return fileFullName;
}
finally
{
wb.Close(false, Missing.Value, Missing.Value);
app.Workbooks.Close();
app.Quit();
Marshal.ReleaseComObject((object)app);
Marshal.ReleaseComObject((object)wb);
Marshal.ReleaseComObject((object)sheet);
GC.Collect();
}
}
}