使用C#.net中的interop.excel生成数据集到Excel工作表 [英] Dataset to Excel sheets using interop.excel generation in C#.net

查看:77
本文介绍了使用C#.net中的interop.excel生成数据集到Excel工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dataset to Excel sheets using interop.excel generation in C#.net

i got error Object reference not set to an instance of an object. how to solve this error







protected void Page_Load(object sender,EventArgs e)

{

if(!IsPostBack)

{



getquery();



}



}

void getquery()

{

con = new SqlConnection(strcon);

con.Open();

string query =从getezee中选择前100 *;;

cmd = new SqlCommand(query,con);

SqlDataAdapter da = new SqlDataAdapter(cmd);

ds = new DataSet();

da.Fill(ds);

con.Close();

}





protected void excel_generation_Click(object sender, EventArgs e)

{

试试

{

Export_Excel(ds);



}

catch(例外情况)

{

throw ex;

}

}



public void Export_Excel(DataSet ds)

{



Microsoft.Office.Interop.Excel.Application objXL = null;

Microsoft.Office.Interop.Excel。工作簿objWB = null;

尝试

{



objXL = new Microsoft.Office.Interop.Excel。 Application();

objWB = objXL.Workbooks.Add(ds.Tables.Count);

int sheetcount = 1;



foreach(ds.Tables中的System.Data.DataTable dt)

{



Microsoft.Office.Interop。 Excel.Worksheet objSHT =(Microsoft.Office.Interop.Excel.Worksheet)objWB.S​​heets.Add();



objSHT.Name =SheetData+ sheetcount.ToString();

for(int j = 0; j< dt.Rows.Count; j ++)

{

for(int i = 0; i< dt.Columns.Count; i ++)

{



if(j == 0)

{

objSHT.Cells [1,i + 1] = dt.Columns [i] .ColumnName.ToString();

}



objSHT.Cells [j + 2,i + 1] = dt。行[j] [i] .ToString();

}

}



sheetcount ++;

}



objWB.S​​aved = true;

objWB.S​​aveCopyAs(C:\\+ Guid.NewGuid()。ToString()+。xlsx);



objWB.Close();



objXL.Quit();



}

catch(易感性)在前)

{

objWB.S​​aved = true;

objWB.Close();

objXL.Quit();

Response.Write(非法许可);

}

}

public override void VerifyRenderingInServerForm(Control control)

{

/ * Verifies控件被渲染* /

}




protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

getquery();

}

}
void getquery()
{
con = new SqlConnection(strcon);
con.Open();
string query = " Select top 100 * from getezee; ";
cmd = new SqlCommand(query,con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
con.Close();
}


protected void excel_generation_Click(object sender, EventArgs e)
{
try
{
Export_Excel(ds);

}
catch (Exception ex)
{
throw ex;
}
}

public void Export_Excel(DataSet ds)
{

Microsoft.Office.Interop.Excel.Application objXL = null;
Microsoft.Office.Interop.Excel.Workbook objWB = null;
try
{

objXL = new Microsoft.Office.Interop.Excel.Application();
objWB = objXL.Workbooks.Add(ds.Tables.Count);
int sheetcount = 1;

foreach (System.Data.DataTable dt in ds.Tables)
{

Microsoft.Office.Interop.Excel.Worksheet objSHT = (Microsoft.Office.Interop.Excel.Worksheet)objWB.Sheets.Add();

objSHT.Name = "SheetData" + sheetcount.ToString();
for (int j = 0; j < dt.Rows.Count; j++)
{
for (int i = 0; i < dt.Columns.Count; i++)
{

if (j == 0)
{
objSHT.Cells[1, i + 1] = dt.Columns[i].ColumnName.ToString();
}

objSHT.Cells[j + 2, i + 1] = dt.Rows[j][i].ToString();
}
}

sheetcount++;
}

objWB.Saved = true;
objWB.SaveCopyAs("C:\\" + Guid.NewGuid().ToString() + ".xlsx");

objWB.Close();

objXL.Quit();

}
catch (Exception ex)
{
objWB.Saved = true;
objWB.Close();
objXL.Quit();
Response.Write("Illegal permission");
}
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}

推荐答案

不要在ASP.net网站上使用互操作它并不适用于此,请尝试使用此组件: http://epplus.codeplex.com/ [ ^ ]
Don't use interop in ASP.net websites it is not meant for that, try this component instead : http://epplus.codeplex.com/[^]


我猜猜代码中的ds是什么都没有



I would guess that ds in the code bellow is nothing

protected void excel_generation_Click(object sender, EventArgs e)
{
try
{
Export_Excel(ds);

}
catch (Exception ex)
{
throw ex;
}
}





您正在使用ds而不在方法中实例化它。



这个问题很糟糕,你不是说哪一行会引发错误,而且这是最容易检测和解决的错误之一。在你的代码中放一个断点,然后逐行进行,直到你发现哪个对象没有实例化为止。



如果这有帮助请花时间接受解决方案。谢谢。



You're using ds without instantiating it in the method.

The question is badly posed, you're not saying which line throws the error and besides, this is one of the easiest bug to detect and resolve. Put a breakpoint into your code and go line by line until you find which object is not instantiated.

If this helps please take time to accept the solution. Thank you.


这篇关于使用C#.net中的interop.excel生成数据集到Excel工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆