首页 新闻 会员 周边 捐助

怎么判断导出excel完毕

0
悬赏园豆:10 [已解决问题] 解决于 2012-01-17 16:48

.NET导出excel,怎么判断是否保存成功了,也就是说保存成功了,我要更新数据库的一个字段的,然后页面就要重新刷新一次,读取数据,问题是我怎么判断是否保存完毕了

华崽崽的主页 华崽崽 | 初学一级 | 园豆:19
提问于:2012-01-06 22:54
< >
分享
最佳答案
0

 private void expExl()
        {
            this.rButton2.Enabled = false;
            ApplicationClass _excel = new ApplicationClass();
            _excel.Application.Workbooks.Add(true);
            // 若是true,则在导出的时候会显示EXcel界面。
            _excel.Visible = false;
            Workbooks workbooks = (Workbooks)_excel.Workbooks;
            Workbook workbook = (Workbook)(workbooks.Add(Type.Missing));
            Worksheet worksheet = (Worksheet)workbook.ActiveSheet;
            worksheet.Name = sheetName;

            if (_excel == null)
            {
                MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            List<Item> exportFields= GetExportFields();

            // 标题
            InitExcelHeader(_excel,exportFields);
            int row = 3;

            //InitExcelHeader(_excel, exportFields, row);
            int exportRows = 0;
            // 1按照dataGridView导出
            if (this.expTable != null)
            {
                exportRows=ExportGridToExcel(_excel, exportFields, row);

            }else{
                exportRows=ExportDataToExcel(_excel, exportFields, row);
            }


            //保存到文件
            worksheet.SaveAs(fullPathName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
              Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing);
            workbook.Close(false, Type.Missing, Type.Missing);
            workbooks.Close();
            _excel.Quit();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(_excel);

            GC.Collect();

            this.result_label.Visible = true;
            this.result_label.Text = "数据成功导出" + exportRows+ "条数据。 ";
            this.rButton2.Enabled = true;

        }

收获园豆:10
volin | 初学一级 |园豆:39 | 2012-01-07 09:33

哪段代码是判断是否用户保存成功了的?不是导出成功哦,是保存成功

华崽崽 | 园豆:19 (初学一级) | 2012-01-07 09:34
其他回答(1)
0

你得把你是怎么保存的说一下啊,要不别人怎么知道要怎么判断。

LCM | 园豆:6876 (大侠五级) | 2012-01-06 23:15

Response.Clear();
                    Response.AddHeader("Content-Disposition",
                                       "attachment;filename=" +
                                      excelName);
                    Response.AddHeader("Content-Length", MyFileInfo.Length.ToString());
                    Response.ContentType = "application/ms-excel";
                    Response.WriteFile(MyFileName);
                    HttpContext.Current.ApplicationInstance.CompleteRequest();

//这是导出的方法,导出也是普通的导出,保存就是浏览器弹出的那个保存excel小窗体啊,

支持(0) 反对(0) 华崽崽 | 园豆:19 (初学一级) | 2012-01-06 23:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册