(原創)C#使用QueryTables導出到Excel

简介: private void ExortToExcel1(string strSql)                      {                                 string ExportPath = Request.
 

private void ExortToExcel1(string strSql)

                     {

                                string ExportPath = Request.PhysicalApplicationPath + "Temp\\";

                                string strFileName = Session.SessionID+DateTime.Now.ToString("yyyyMMdd hhmmss")+".xls";

                                //新建一Excel應用程式

                                Missing missing = Missing.Value;

                                Excel.ApplicationClass objExcel = null;

                                Excel.Workbooks objBooks = null;

                                Excel.Workbook objBook = null;

                                Excel.Worksheet objSheet = null;

                                try

                                {

                                           objExcel = new Excel.ApplicationClass();

                                           objExcel.Visible = false;

                                           objBooks = (Excel.Workbooks)objExcel.Workbooks;

                                           objBook = (Excel.Workbook)(objBooks.Add(missing));

                                           objSheet = (Excel.Worksheet)objBook.ActiveSheet;

 

                                           string conn = "ODBC;DRIVER={Oracle in OraHome92};SERVER=MCM;UID=SFC;PWD=SFC;DBQ=MCM;";

                                           Excel.QueryTable tb = objSheet.QueryTables.Add(conn,objSheet.get_Range("A1", missing),strSql);

                                           //tb.Name = "來自 FCNP 的查詢";

                                           tb.FieldNames = true;

                                           tb.RowNumbers = false;

                                           tb.FillAdjacentFormulas = false;

                                           tb.PreserveFormatting = true;

                                           tb.RefreshOnFileOpen = false;

                                           tb.BackgroundQuery = true;

                                           tb.RefreshStyle = Excel.XlCellInsertionMode.xlInsertDeleteCells;

                                           tb.SavePassword = false;

                                           tb.SaveData = true;

                                           tb.AdjustColumnWidth = true;

                                           tb.RefreshPeriod = 0;

                                           tb.PreserveColumnInfo = true;

                                           tb.BackgroundQuery = false;

                                           tb.Refresh(tb.BackgroundQuery);

                                                    

 

                                           try

                                           {

                                                     objSheet.PageSetup.LeftMargin = 20;

                                                     objSheet.PageSetup.RightMargin = 20;

                                                     objSheet.PageSetup.TopMargin = 35;

                                                     objSheet.PageSetup.BottomMargin = 15;

                                                     objSheet.PageSetup.HeaderMargin = 7;

                                                     objSheet.PageSetup.FooterMargin = 10;

                                                     objSheet.PageSetup.CenterHorizontally = true;

                                                     objSheet.PageSetup.CenterVertically = false;

                                                     objSheet.PageSetup.Orientation = Excel.XlPageOrientation.xlPortrait;

                                                     objSheet.PageSetup.PaperSize = Excel.XlPaperSize.xlPaperA4;

                                                     objSheet.PageSetup.Zoom = false;

                                                     objSheet.PageSetup.FitToPagesWide = 1;

                                                     objSheet.PageSetup.FitToPagesTall = false;

                                           }

                                           catch

                                           {

                                           }

                                           //關閉Excel

                                           objBook.SaveAs(ExportPath+strFileName, missing, missing, missing, missing, missing,Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing,missing);

                                           objBook.Close(false,missing,missing);

                                           objBooks.Close();

                                           objExcel.Quit();

                                }

                                finally

                                {                                        

                                           //System.Runtime.InteropServices.Marshal.ReleaseComObject(objRange);

                                           if (!objSheet.Equals(null))

                                                     System.Runtime.InteropServices.Marshal.ReleaseComObject(objSheet);

                                           if (objBook!=null)

                                                     System.Runtime.InteropServices.Marshal.ReleaseComObject(objBook);

                                           if (objBooks!=null)

                                                     System.Runtime.InteropServices.Marshal.ReleaseComObject(objBooks);

                                           if (objExcel!=null)

                                                     System.Runtime.InteropServices.Marshal.ReleaseComObject(objExcel);

                                           GC.Collect();

                                }

                                //保存或打開報表

                                Response.Clear();

                                Response.Buffer= true;

                                Response.ContentType = "application/vnd.ms-excel";

                                Response.ContentEncoding = System.Text.Encoding.UTF8;                               

                                Response.AppendHeader("content-disposition","attachment;filename="+strFileName+".xls");

                                Response.Charset = "";

                                this.EnableViewState = false;

                                Response.WriteFile(ExportPath+strFileName);

                                Response.End();     

                     }


註:必須安裝odbc for oracle 

目录
相关文章
|
4月前
|
关系型数据库 MySQL Shell
不通过navicat工具怎么把查询数据导出到excel表中
不通过navicat工具怎么把查询数据导出到excel表中
47 0
|
22天前
|
前端开发
实现Excel文件和其他文件导出为压缩包,并导入
实现Excel文件和其他文件导出为压缩包,并导入
25 1
|
24天前
|
数据格式 UED
记录一次NPOI库导出Excel遇到的小问题解决方案
【11月更文挑战第16天】本文记录了使用 NPOI 库导出 Excel 过程中遇到的三个主要问题及其解决方案:单元格数据格式错误、日期格式不正确以及合并单元格边框缺失。通过自定义单元格样式、设置数据格式和手动添加边框,有效解决了这些问题,提升了导出文件的质量和用户体验。
148 3
|
1月前
|
Java API Apache
|
1月前
|
存储 Java API
Java实现导出多个excel表打包到zip文件中,供客户端另存为窗口下载
Java实现导出多个excel表打包到zip文件中,供客户端另存为窗口下载
45 4
|
2月前
|
JavaScript 前端开发 数据处理
Vue导出el-table表格为Excel文件的两种方式
Vue导出el-table表格为Excel文件的两种方式
83 6
|
3月前
|
SQL C# 数据库
EPPlus库的安装和使用 C# 中 Excel的导入和导出
本文介绍了如何使用EPPlus库在C#中实现Excel的导入和导出功能。首先,通过NuGet包管理器安装EPPlus库,然后提供了将DataGridView数据导出到Excel的步骤和代码示例,包括将DataGridView转换为DataTable和使用EPPlus将DataTable导出为Excel文件。接着,介绍了如何将Excel数据导入到数据库中,包括读取Excel文件、解析数据、执行SQL插入操作。
EPPlus库的安装和使用 C# 中 Excel的导入和导出
|
2月前
|
easyexcel Java UED
SpringBoot中大量数据导出方案:使用EasyExcel并行导出多个excel文件并压缩zip后下载
在SpringBoot环境中,为了优化大量数据的Excel导出体验,可采用异步方式处理。具体做法是将数据拆分后利用`CompletableFuture`与`ThreadPoolTaskExecutor`并行导出,并使用EasyExcel生成多个Excel文件,最终将其压缩成ZIP文件供下载。此方案提升了导出效率,改善了用户体验。代码示例展示了如何实现这一过程,包括多线程处理、模板导出及资源清理等关键步骤。
|
2月前
|
前端开发 JavaScript
💥【exceljs】纯前端如何实现Excel导出下载和上传解析?
本文介绍了用于处理Excel文件的库——ExcelJS,相较于SheetJS,ExcelJS支持更高级的样式自定义且易于使用。表格对比显示,ExcelJS在样式设置、内存效率及流式操作方面更具优势。主要适用于Node.js环境,也支持浏览器端使用。文中详细展示了如何利用ExcelJS实现前端的Excel导出下载和上传解析功能,并提供了示例代码。此外,还提供了在线调试的仓库链接和运行命令,方便读者实践。
391 5
|
2月前
|
前端开发 JavaScript Java
导出excel的两个方式:前端vue+XLSX 导出excel,vue+后端POI 导出excel,并进行分析、比较
这篇文章介绍了使用前端Vue框架结合XLSX库和后端结合Apache POI库导出Excel文件的两种方法,并对比分析了它们的优缺点。
663 0