博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# excel xls保存
阅读量:7097 次
发布时间:2019-06-28

本文共 4661 字,大约阅读时间需要 15 分钟。

public HSSFWorkbook Excel_Export(DataTable query,string title,int[] rowweight,string[] rowtitle)        {            HSSFWorkbook workbook = new HSSFWorkbook();            HSSFSheet sheet = workbook.CreateSheet("Sheet1") as HSSFSheet;            IRow row = sheet.CreateRow(0);            int ii = 0;            Color c = Color.FromArgb(215, 228, 188);            HSSFPalette palette = workbook.GetCustomPalette();            palette.SetColorAtIndex((short)63, c.R, c.G, c.B);            HSSFColor cellColor = palette.FindColor(c.R, c.G, c.B);            ICellStyle style = workbook.CreateCellStyle();            style.Alignment = HorizontalAlignment.Center;            style.WrapText = true;            style.BorderLeft = BorderStyle.Thin;            style.BorderRight = BorderStyle.Thin;            style.BorderTop = BorderStyle.Thin;            style.BorderBottom = BorderStyle.Thin;            style.VerticalAlignment = VerticalAlignment.Center;            style.FillPattern = FillPattern.SolidForeground;            style.FillForegroundColor = cellColor.Indexed;            IFont font = workbook.CreateFont();            font.FontHeightInPoints = 11;            font.FontName = "微软雅黑";            font.IsBold = true;            style.SetFont(font);            ICell cell = row.CreateCell(ii);            if(!string.IsNullOrEmpty(title))            {                row.Height = 50 * 20;                cell.SetCellValue(title);                cell.CellStyle = style;                sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, query.Columns.Count-1));                ii += 1;            }            style = workbook.CreateCellStyle();            style.Alignment = HorizontalAlignment.Left;            style.WrapText = true;            style.VerticalAlignment = VerticalAlignment.Center;            style.BorderLeft = BorderStyle.Thin;            style.BorderRight = BorderStyle.Thin;            style.BorderTop = BorderStyle.Thin;            style.BorderBottom = BorderStyle.Thin;            style.FillPattern = FillPattern.SolidForeground;            style.FillForegroundColor = cellColor.Indexed;            font = workbook.CreateFont();            font.FontHeightInPoints = 11;            font.FontName = "微软雅黑";            style.SetFont(font);            IRow row1 = sheet.CreateRow(ii);            row1.Height = 60 * 20;            int i = 0;            if(rowtitle.Length>0)                foreach (var item in rowtitle)                {                    cell = row1.CreateCell(i);                    cell.SetCellValue(item.ToString());                    cell.CellStyle = style;                    i += 1;                }            else                foreach (var item in  query.Columns)                {                    cell = row1.CreateCell(i);                    cell.SetCellValue(item.ToString());                    cell.CellStyle = style;                    i += 1;                }                  for ( i = 0; i < rowweight.Length; i++)            {                if (rowweight[i] > 0)                {                    row1.Cells[i].CellStyle = style; //把样式赋给单元格                    sheet.SetColumnWidth(i, rowweight[i] * 256);//设置列宽                }            }            i = 0;            row.Height = 30 * 20;            style = workbook.CreateCellStyle();            style.WrapText = true;            style.Alignment = HorizontalAlignment.Left;            style.VerticalAlignment = VerticalAlignment.Center;            style.BorderLeft = BorderStyle.Thin;            style.BorderRight = BorderStyle.Thin;            style.BorderTop = BorderStyle.Thin;            style.BorderBottom = BorderStyle.Thin;            font = workbook.CreateFont();            font.FontHeightInPoints = 10;            font.FontName = "微软雅黑";            style.SetFont(font);            for (int n = 0; n < query.Rows.Count; n++)            {                ii += 1;                IRow rowtemp = sheet.CreateRow(ii);                for (int j = 0; j < query.Columns.Count; j++)                {                    cell = rowtemp.CreateCell(j);                    cell.CellStyle = style;                    cell.SetCellValue(query.Rows[n][j]?.ToString() ?? "");                                 }            }            return workbook;        }

 

//写文件            MemoryStream ms = new MemoryStream();            workbook.Write(ms);            ms.Flush();            ms.Seek(0, SeekOrigin.Begin); //ms.Position = 0;            string fileName = string.Concat(string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now), ".xls");            return File(ms, "application/vnd.ms-excel", fileName);

 

转载于:https://www.cnblogs.com/LiuFengH/p/10576955.html

你可能感兴趣的文章
Spring 执行 sql 脚本(文件)
查看>>
canvas绘制多张图其中有空白或绘制不成功的问题
查看>>
quicklink解析
查看>>
webpack4系列教程(九):开发环境和生产环境
查看>>
GraphQL 科普 前端向
查看>>
前端基本功-常见概念(一)
查看>>
史上最全SQL优化方案
查看>>
阿里云时空数据库引擎HBase Ganos上线,场景、功能、优势全解析
查看>>
Promise实现思路的个人理解
查看>>
基于文件系统实现可追加的数据集市
查看>>
Python2.x与3.x版本区别
查看>>
pipenv与virtualenv
查看>>
Python 送你一棵圣诞树
查看>>
Express 文档(express())
查看>>
算法复杂度分析
查看>>
WEB站点性能优化实践
查看>>
C++ 学习笔记之——输入和输出
查看>>
前端调用ocx将ocx封装为cab包整体流程
查看>>
接口异常状态统一处理方案:优先业务端处理,再按需统一处理。
查看>>
Go模块简明教程(Go语言依赖包管理工具)
查看>>