private void ExportExcel(DataTable dt, int[] headerWidth) { if (dt != null && dt.Rows.Count > 0) { string title = this.GetExportTitle(); string path = FileDialogHelper.SaveExcel(title + ".xls"); if (path != null && path != string.Empty) { if (File.Exists(path)) { File.Delete(path); } ListExcel ls = new ListExcel(path, dt); ls.Title = title; ls.HeaderWidth = headerWidth; ls.GetExcelReport(); this.Cursor = Cursors.WaitCursor; MessageBoxHelper.Show("导出成功!"); this.Cursor = Cursors.Default; } } else { MessageBoxHelper.Show("没有找到可导出的数据!"); } }
/// <summary> /// ListExcel导出名叫reportname的excel报表 /// </summary> /// <param name="reportname">报表名字带xls后缀</param> /// <param name="listExcel">ListExcel</param> public static void ExportExcelReport(string reportname, ListExcel listExcel) { System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(reportname)); System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel"; string filename = listExcel.GetExcelReport(); FileInfo file = new FileInfo(filename); System.Web.HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString()); //添加头文件,指定文件的大小,让浏览器显示文件下载的速度 System.Web.HttpContext.Current.Response.WriteFile(file.FullName); // 把文件流发送到客户端 System.Web.HttpContext.Current.Response.End(); }
private void Export() { DataTable dt = this.GetTableFromGrid(); string path = FileDialogHelper.SaveExcel(this.GetTitle() + ".xls"); if (path != null && path != string.Empty) { if (File.Exists(path)) { File.Delete(path); } ListExcel ls = new ListExcel(path, dt); ls.Title = this.GetTitle(); ls.GetExcelReport(); this.Cursor = Cursors.WaitCursor; MessageBoxHelper.Show("导出成功!"); this.Cursor = Cursors.Default; } }