首页 新闻 会员 周边 捐助

导出excel 递归worksheet 导出的时候没有数据

0
悬赏园豆:5 [已关闭问题] 关闭于 2015-12-17 15:33

导出excel  递归worksheet 导出的时候没有数据,跪求指点

问题补充:
        public static void ExportExcel(DataTable dt)
        {
            var excel1 = new Microsoft.Office.Interop.Excel.Application();
            excel1.DisplayAlerts = false; //不显示报警
            var workbook1 = excel1.Workbooks.Add(Type.Missing); //创建book
            excel1.Visible = false;//不可见
            var worksheet1 = (Microsoft.Office.Interop.Excel.Worksheet)workbook1.Worksheets["sheet1"];  //表头   
            //Excel里从第1行,第1列计算            
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                worksheet1.Cells[1, i + 1] = dt.Columns[i].ColumnName;
            }

            int shendu = 0;

            //worksheet1 = ChildrenBale(0, dt, 2, shendu, worksheet1);
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";  //文件名称
            string filePath = System.Web.HttpContext.Current.Server.MapPath("~/Upload/Excel/" + fileName);  //文件路径
            workbook1.SaveAs(filePath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            excel1.Workbooks.Close(); //关闭book
            excel1.Quit();
        }
    public static Microsoft.Office.Interop.Excel.Worksheet ChildrenBale(int pid, DataTable dt, int rows, int shendu, Microsoft.Office.Interop.Excel.Worksheet sheet)
        {
            DataRow[] dr = null;
            if (pid == 0)
            {
                dr = dt.Select("父编号=" + pid + " or 父编号 is null ");
            }
            else
            {
                dr = dt.Select("父编号=" + pid);
            }

            var cl = dr.Length - 1;
            var co = dt.Columns.Count - 1;
            for (int i = 0; i <= cl; i++)
            {
                for (int j = 2; j <= co; j++)
                {
                    try
                    {
                        sheet.Cells[rows, i + 1] = dr[i][j].ToString();
                    }
                    catch (Exception)
                    {

                        throw;
                    }
                }
                shendu++;
                pid = int.Parse(dr[i]["编号"].ToString());
                sheet = ChildrenBale(pid, dt, rows++, shendu, sheet);
                shendu--;
            }
            return sheet;

        }
阿哈太阳的主页 阿哈太阳 | 初学一级 | 园豆:23
提问于:2015-12-11 18:12
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册