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;
}
哪段代码是判断是否用户保存成功了的?不是导出成功哦,是保存成功
你得把你是怎么保存的说一下啊,要不别人怎么知道要怎么判断。
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小窗体啊,