Index was outside the bounds of the array 在将dataset中的数据导入到excel时出现错误:Index was outside the bounds of the array好象服务器上的缓存没能及时释放,请各位高手指教如何解决。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 int[] myArr = new Arr[3]{1,2,3};int test = Arr[5];//数组越界应该是上面的原因吧,仔细检查一下数组。 导出数据的函数:public void ExportExcel(DataSet my_Ds, string filename) { //DataSet ds = this.SqlDataSource1; Excel.Application oExcel; oExcel = new Excel.Application(); try { Excel.Workbook oBook; Object oMissing = System.Reflection.Missing.Value; oBook = oExcel.Workbooks.Add(oMissing); //HttpResponse response = HttpContext.Current.Response; int lie = my_Ds.Tables[0].Columns.Count; int hang = my_Ds.Tables[0].Rows.Count; int i, j, t; string panduanstring = ""; i = 1; for (j = 0; j < lie; j++)//标题 { oExcel.Cells[1, i++] = my_Ds.Tables[0].Columns[j].ColumnName; } t = 1; for (i = 0; i < hang; i++)//内容 { for (j = 0; j < lie; j++) { panduanstring = my_Ds.Tables[0].Rows[i][j].ToString(); if (panduanstring.GetType().ToString() == "System.String") { oExcel.Cells[i + 2, t++] = "'" + my_Ds.Tables[0].Rows[i][j].ToString(); } else oExcel.Cells[i + 2, t++] = my_Ds.Tables[0].Rows[i][j].ToString(); } t = 1; } oExcel.Visible = true; oBook.Saved = true; oExcel.UserControl = false; string path = System.Windows.Forms.Application.StartupPath;// Server.MapPath("excel/"); string mm = filename ; oExcel.ActiveWorkbook.SaveCopyAs(mm); oExcel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject((object)oExcel); GC.Collect(); //response.Redirect("excel/" + filename + ".xls"); //oExcel.Quit(); //System.IO.File.Delete(path + filename + ".xls"); MessageBox.Show("导出成功!"); } catch { //oExcel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject((object)oExcel); GC.Collect(); } } public void ExportData(DataGrid ds,string attachName) { Response.Clear(); Response.Buffer= true; Response.Charset="utf-8"; Response.AppendHeader("Content-Disposition","attachment;" + attachName + "=.xls"); Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8"); //Response.ContentType指定文件类型 可以为application/ms-excel || application/ms-word || application/ms-txt || application/ms-html || 或其他浏览器可直接支持文档 Response.ContentType = "application/vnd.ms-excel"; Response.Charset = ""; //关闭 ViewState EnableViewState = false; System.IO.StringWriter tw = new System.IO.StringWriter();//将信息写入字符串 System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);//在WEB窗体页上写出一系列连续的HTML特定字符和文本。 //此类提供ASP.NET服务器控件在将HTML内容呈现给客户端时所使用的格式化功能 //获取control的HTML ds.RenderControl(hw);//将DATAGRID中的内容输出到HtmlTextWriter对象中 // 把HTML写回浏览器 Response.Write(tw.ToString()); Response.End(); } Click the link to solve your problem.Good luck! asp.net 读取Excel中的sql 函数问题 asp.net mvc2 怎样与非mvc网站合并在一起 VS2008连接不上SQL2008里的数据库 new出来的Grid控件,排序不灵?200分求助 在GridView中判断数据显示图片 无法从web服务器获取项目文件 一个后台管理系统..开始没有考虑登录 现在要加上 如何实现 不漏掉有些页面的访问 急关闭窗口 关于asp.net using c# 的分页问题~~顺便散分~~ 求助:asp.net 读取服务器端word文件到web页面 [请教].net1x与2.0有什么区别? 我也不知道是什么问题,就不知道是怎么回事,请教一下大家
int test = Arr[5];//数组越界应该是上面的原因吧,仔细检查一下数组。
public void ExportExcel(DataSet my_Ds, string filename)
{
//DataSet ds = this.SqlDataSource1; Excel.Application oExcel;
oExcel = new Excel.Application();
try
{ Excel.Workbook oBook;
Object oMissing = System.Reflection.Missing.Value; oBook = oExcel.Workbooks.Add(oMissing);
//HttpResponse response = HttpContext.Current.Response; int lie = my_Ds.Tables[0].Columns.Count;
int hang = my_Ds.Tables[0].Rows.Count;
int i, j, t;
string panduanstring = "";
i = 1;
for (j = 0; j < lie; j++)//标题
{ oExcel.Cells[1, i++] = my_Ds.Tables[0].Columns[j].ColumnName; } t = 1;
for (i = 0; i < hang; i++)//内容
{
for (j = 0; j < lie; j++)
{
panduanstring = my_Ds.Tables[0].Rows[i][j].ToString();
if (panduanstring.GetType().ToString() == "System.String")
{
oExcel.Cells[i + 2, t++] = "'" + my_Ds.Tables[0].Rows[i][j].ToString();
}
else
oExcel.Cells[i + 2, t++] = my_Ds.Tables[0].Rows[i][j].ToString(); }
t = 1;
} oExcel.Visible = true;
oBook.Saved = true;
oExcel.UserControl = false; string path = System.Windows.Forms.Application.StartupPath;// Server.MapPath("excel/");
string mm = filename ; oExcel.ActiveWorkbook.SaveCopyAs(mm);
oExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject((object)oExcel);
GC.Collect();
//response.Redirect("excel/" + filename + ".xls");
//oExcel.Quit(); //System.IO.File.Delete(path + filename + ".xls");
MessageBox.Show("导出成功!");
} catch
{
//oExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject((object)oExcel);
GC.Collect();
} }
{
Response.Clear();
Response.Buffer= true;
Response.Charset="utf-8"; Response.AppendHeader("Content-Disposition","attachment;" + attachName + "=.xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");
//Response.ContentType指定文件类型 可以为application/ms-excel || application/ms-word || application/ms-txt || application/ms-html || 或其他浏览器可直接支持文档
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = ""; //关闭 ViewState
EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();//将信息写入字符串
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);//在WEB窗体页上写出一系列连续的HTML特定字符和文本。
//此类提供ASP.NET服务器控件在将HTML内容呈现给客户端时所使用的格式化功能
//获取control的HTML
ds.RenderControl(hw);//将DATAGRID中的内容输出到HtmlTextWriter对象中
// 把HTML写回浏览器
Response.Write(tw.ToString());
Response.End();
}