在将dataset中的数据导入到excel时出现错误:Index was outside the bounds of the array
好象服务器上的缓存没能及时释放,请各位高手指教如何解决。

解决方案 »

  1.   

    int[] myArr = new Arr[3]{1,2,3};
    int test = Arr[5];//数组越界应该是上面的原因吧,仔细检查一下数组。
      

  2.   

    导出数据的函数:
    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();
                }        }
      

  3.   

    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();
    }
      

  4.   

    Click the link to solve your problem.Good luck!