C# 导出Excel 多个Sheet
以下代码中最关键的代码是
Worksheet mSheet = (Microsoft.Office.Interop.Excel.Worksheet)mBook.Worksheets.Add(miss, miss, 1, Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);
网上很资料中都没有提到,我也是试了很久才搞出来的,上面这一行是新建了2个Sheet,想建多个就修改Add的第3个参数,数量为参数值+1
xlapp.Visible = false; //是否显示导出过程 ,建议关闭,否则在导出过程中鼠标点击Excel文件时会出错。
引用:COM --》 Microsoft Office xx.x Object Library 再引用 Microsoft.Office.Interop.Excel (可以到网上搜索或以下地址下载:http://files.cnblogs.com/fan0136/Microsoft.Office.Interop.Excel.rar)
完整的代码台下,实际使用中数据表参数可以修改成List<object>:
public class ToExcel{/// <summary>/// 可以自定义导出Excel的格式,传的参数为GridView/// </summary>/// <param name="gridView"></param>/// <param name="filename"></param>public static void ExportGridViewToExcel(DevExpress.XtraGrid.Views.Grid.GridView gridView, DevExpress.XtraGrid.Views.Grid.GridView gridView1, string filename){//System.Data.DataTable dt = (System.Data.DataTable)gridView.DataSource;SaveFileDialog sfd = new SaveFileDialog();filename += DateTime.Now.ToString("yyyyMMdd") + "-" + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString();sfd.FileName = filename;sfd.Filter = "Excel files (*xls) | *.xls";sfd.RestoreDirectory = true;if (sfd.ShowDialog() == DialogResult.OK && sfd.FileName.Trim() != null){int rowIndex = 1;int rowIndex1 = 1;int colIndex = 0;int colNum = gridView.Columns.Count;System.Reflection.Missing miss = System.Reflection.Missing.Value;Microsoft.Office.Interop.Excel.Application xlapp = new Microsoft.Office.Interop.Excel.Application();xlapp.Visible = true;Microsoft.Office.Interop.Excel.Workbooks mBooks = (Microsoft.Office.Interop.Excel.Workbooks)xlapp.Workbooks;Microsoft.Office.Interop.Excel.Workbook mBook = (Microsoft.Office.Interop.Excel.Workbook)mBooks.Add(miss);Worksheet mSheet = (Microsoft.Office.Interop.Excel.Worksheet)mBook.Worksheets.Add(miss, miss, 1, Microsoft.Office.Interop.Excel.XlSheetType.xlWorksheet);Worksheet sm = mBook.Worksheets[1];sm.Name = "q";Worksheet ws = mBook.Worksheets[2];ws.Name = "B";//设置对齐方式mSheet.Cells.HorizontalAlignment = XlHAlign.xlHAlignCenter;//设置文字自动换行 //mSheet.Cells.WrapText = true;//设置第一行高度,即标题栏((Microsoft.Office.Interop.Excel.Range)mSheet.Rows["1:1", System.Type.Missing]).RowHeight = 20;//设置数据行行高度((Microsoft.Office.Interop.Excel.Range)mSheet.Rows["2:" + gridView.RowCount + 1, System.Type.Missing]).RowHeight = 16;//设置字体大小(10号字体)mSheet.Range[mSheet.Cells[1, 1], mSheet.Cells[gridView.RowCount + 1, gridView.Columns.Count]].Font.Size = 10;//设置单元格边框Microsoft.Office.Interop.Excel.Range range1 = mSheet.Range[mSheet.Cells[1, 1], mSheet.Cells[gridView.RowCount + 1, gridView.Columns.Count]];range1.Borders.LineStyle = 1;//写标题for (int row = 1; row <= gridView.Columns.Count; row++){sm.Cells[1, row] = gridView.Columns[row - 1].GetTextCaption();} //写标题for (int row = 1; row <= gridView1.Columns.Count; row++){ws.Cells[1, row] = gridView1.Columns[row - 1].GetTextCaption();}try{for (int i = 0; i < gridView.RowCount; i++){rowIndex++;colIndex = 0;for (int j = 0; j < gridView.Columns.Count; j++){colIndex++;sm.Cells[rowIndex, colIndex] = gridView.GetRowCellValue(i, gridView.Columns[j]);}} for (int i = 0; i < gridView1.RowCount; i++){rowIndex1++;colIndex = 0;for (int j = 0; j < gridView1.Columns.Count; j++){colIndex++;ws.Cells[rowIndex1, colIndex] = gridView1.GetRowCellValue(i, gridView1.Columns[j]);}}mBook.SaveAs(sfd.FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel7, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,miss, miss, miss, miss, miss);}catch (Exception ex){throw new Exception(ex.Message);}finally{mBooks.Close();xlapp.Quit();System.Runtime.InteropServices.Marshal.ReleaseComObject(mSheet);System.Runtime.InteropServices.Marshal.ReleaseComObject(mBook); System.Runtime.InteropServices.Marshal.ReleaseComObject(mBooks);System.Runtime.InteropServices.Marshal.ReleaseComObject(xlapp);GC.Collect();}}else{//return false;}}}
源码下载地址:http://download.csdn.net/detail/jn40105/7124495
转载于:https://www.cnblogs.com/Ares-blog/p/3632897.html
C# 导出Excel 多个Sheet相关推荐
- Poi 导出excel多个sheet的内容
Poi 导出excel多个sheet的内容 目录 Poi 导出excel多个sheet的内容 代码: Contrller Service: 导出引用:ExcelExportUtil 导出类:PoiMu ...
- 编码技巧——使用Easypoi导出Excel、多sheet
本文主要介绍easypoi导出Excel的代码示例:自己之前手动实现过导出工具类<编码技巧--导出工具类>,基于实体和注解,通过反射来映射实体字段和exce列的关系:在部分工程里面看到了e ...
- 前端vue导出Excel(多sheet)
场景: 当导出文件的时候遇到结构复杂的表格(如:表头合并):或者数据分页的时候,后端就很难操作了,难以保持表格原有结构.所以就需要前端导出. 步骤如下: 1.组装数据 2.标记表头合并项,计算合并项 ...
- JAVA-动态导出Excel(自定义sheet,head)
vue前端代码:file-saver - npm index.js: export function exportFile(data) {return request({url: '/url/expo ...
- java 导出excel到多个sheet
java 导出excel 多个sheet 废话不多说了,直接上代码,有需要的盆友,可以看看.@TOC @RequestMapping("/exportAllyScoreCount" ...
- java代码实现导出Excel表格、工具ssm框架、maven、idea
第一步.导入依赖 <!--生成excel文件--><dependency><groupId>org.apache.poi</groupId><ar ...
- java统计数据并导出Excel
业务代码: @RequestMapping(value = "/exportDepartStatistics.html")@ResponseBodypublic HashMap&l ...
- Java导出excel文件
需求 将每个xmpp机房的在线/离线用户信息导出到Excel表格中(定时任务+网页按钮),并在网页上提供下载按钮进行下载. 效果预览 导出文件效果 点击下载弹出框效果 代码总览 /** ". ...
- java导出excel文件(标题为中文乱码)
1 Java导出Excel文件 function excelOut(){ window.location.href="../getFileToExcel/"+taskId; } ...
- vue使用js-xlsx、xlsx、xlsxStyle导出excel,可合并表格、修改格子样式,例如背景颜色、字体大小、列宽等
开始之前先上效果图,觉得不错的可以点个赞(有合并表格的需求可看文章底部链接) 1.需要用到的js (1)js-xlsx.js https://github.com/Ctrl-Ling/XLSX-Sty ...
最新文章
- 使用matlab进行mex编译时的路径问题mexopts
- 日本語のマナーを学びましょう
- 使用Mybatis Generator自动生成代码
- Elasticsearch Metric Aggregation指标聚合详解
- Python创建单例模式的5种方法
- AssertJ的SoftAssertions –我们需要它们吗?
- IBM JVM调整– gencon GC策略
- 富文本编辑器、日期选择器、软件天堂、防止XSS攻击、字体icon、转pdf
- 一些社会运行的底层规律,和你的利益息息相关
- POJ3261 Milk Patterns 【后缀数组】
- php mysql zend linux,Linux系统安装配置PHP+MySQL+zend+PHPadmin开发环境
- python 调用C语言DLL的方法
- java多线程volatile_java多线程——volatile
- python商业爬虫教程_python商业爬虫课程
- 用路由器打印机显示服务器不存在,打印机连路由器怎么搜索不到
- 有关USGS下载landsat 8影像的方法
- 贪心——国际象棋锦标赛
- 单片机学习笔记(五)—— 键盘
- C语言:goto循环语句
- 一个用js写的沙漏程序 hourglass
热门文章
- pycharm添加conda虚拟环境
- Tensorspace一款神奇的神经网络可视化应用
- 《小欢喜》欢喜人生,人生如画,画里有话,话说人生
- python求解对给定字符串,求解所有子序列是否为回文序列
- python实现猴子排序(Monkey_sort)闲聊睡眠排序
- python实现Longest Common Subsequence最长公共子序列算法
- pytorch加载模型报错RuntimeError: Error(s) in loading state_dict for ResNet:Missing key(s) in stat
- 区块链 xuperchain io.grpc.StatusRuntimeException: UNKNOWN: error execution reverted: execution reverted
- ubuntu定时任务cron 访问网址php
- 安装nuxt_一天上手Nuxt基于vue服务端渲染