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();
int ii = ; Color c = Color.FromArgb(, , );
HSSFPalette palette = workbook.GetCustomPalette();
palette.SetColorAtIndex((short), 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 = ;
font.FontName = "微软雅黑";
font.IsBold = true;
style.SetFont(font); ICell cell = row.CreateCell(ii); if(!string.IsNullOrEmpty(title))
{
row.Height = * ;
cell.SetCellValue(title);
cell.CellStyle = style;
sheet.AddMergedRegion(new CellRangeAddress(, , , query.Columns.Count-));
ii += ;
} 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 = ;
font.FontName = "微软雅黑";
style.SetFont(font); IRow row1 = sheet.CreateRow(ii);
row1.Height = * ; int i = ;
if(rowtitle.Length>)
foreach (var item in rowtitle)
{
cell = row1.CreateCell(i);
cell.SetCellValue(item.ToString());
cell.CellStyle = style;
i += ;
}
else
foreach (var item in query.Columns)
{
cell = row1.CreateCell(i);
cell.SetCellValue(item.ToString());
cell.CellStyle = style;
i += ;
} for ( i = ; i < rowweight.Length; i++)
{
if (rowweight[i] > )
{
row1.Cells[i].CellStyle = style; //把样式赋给单元格
sheet.SetColumnWidth(i, rowweight[i] * );//设置列宽
}
}
i = ;
row.Height = * ;
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 = ;
font.FontName = "微软雅黑";
style.SetFont(font);
for (int n = ; n < query.Rows.Count; n++)
{
ii += ;
IRow rowtemp = sheet.CreateRow(ii);
for (int j = ; 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(, SeekOrigin.Begin); //ms.Position = 0; string fileName = string.Concat(string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now), ".xls");
return File(ms, "application/vnd.ms-excel", fileName);

c# excel xls保存的更多相关文章

  1. python pandas合并多个excel(xls和xlsx)文件(弹窗选择文件夹和保存文件)

    # python pandas合并多个excel(xls和xlsx)文件(弹窗选择文件夹和保存文件) import tkinter as tk from tkinter import filedial ...

  2. Python 文本(txt) 转换成 EXCEL(xls)

    #!/bin/env python # -*- encoding: utf-8 -*- #------------------------------------------------------- ...

  3. Excel导入保存附件和解析数据

    Excel导入保存附件和解析数据 一,前端上传附件的组件 1.先给一个下载模板的按钮 // 下载Excel模板 downLoadExcel: function () { window.open(GLO ...

  4. C# EXCEL(.xls和.xlsx)导入到数据库

    C# EXCEL(.xls和.xlsx)导入到数据库  转(http://www.cnblogs.com/bart-cai/articles/2716555.html) 原理:1.判断是否是Excel ...

  5. POI导出Excel(xls、xlsx均可以,也支持图片)——(三)

    Jar包

  6. 将Excel表格保存为图片

    如何将Excel表格保存为图片,可参见以下几种方法: 1.借助其它办法软件,例如Word或PPT. 步骤:选中Excel中需要被保存成图片的内容,Ctrl+C进行复制,打开Word或PPT办公软件,鼠 ...

  7. excel 永久保存宏命令

      excel 永久保存宏命令 CreateTime--2018年5月31日10:03:44 Author:Marydon 情形一:下次编辑excel时,仍可使用 Ctrl+s-->选择否,选择 ...

  8. 微软白板Excel xls列号数字转字母

    Excel xls列号数字转字母 https://blog.csdn.net/lf124/article/details/53432817?utm_source=itdadao&utm_med ...

  9. C# 操作 Excel(.xls和.xlsx)文件

    C#创建Excel(.xls和.xlsx)文件的三种方法 .NET 使用NPOI导入导出标准Excel C# 使用NPOI 实现Excel的简单导入导出 NET使用NPOI组件将数据导出Excel-通 ...

随机推荐

  1. svn 删除svn项目命令

    svn delete svn://127.0.0.1:3690/project -m delete

  2. Mybatis逆向工程自动生成代码(Ubuntu18.04-idea环境)

    最近在学习taotao商城项目,有一节是关于mybatis逆向工程的,参考了这个博文,https://blog.csdn.net/yerenyuan_pku/article/details/71909 ...

  3. cvte春招测试面试记录

    cvte春招测试面试记录,挂在了综合面试(hr面)...尽量回忆面试的问题(可能不完全). 技术面一面: 1.自我介绍 2.根据实习项目问,智能客服怎么测正确率之类的. 3.测试人脸解锁 4.测试微信 ...

  4. nginx常用模块

    Nginx模块介绍 核心模块:core module 标准模块:stand modules HTTP modules: Standard HTTP modules Optional HTTP modu ...

  5. css的position

    1.标准流2.浮动3.定位块级元素:div.H1-H6.有序及无序列表(ol.ul.li).p内联元素:a.span.img 1. 介绍 1.1 说明 Position 属性:规定元素的定位类型.即元 ...

  6. Java面试2018常考题目汇总

    一.JAVA基础篇-概念 1.简述你所知道的Linux: Linux起源于1991年,1995年流行起来的免费操作系统,目前, Linux是主流的服务器操作系统, 广泛应用于互联网.云计算.智能手机( ...

  7. 1. nginx添加自定义http模块(简单)

    步骤 1. 新建模块目录2. 添加模块配置文件3. 编写模块源码文件4. 在主配置文件中配置访问location5. 编译加入模块文件6. 测试 新建模块目录 mkdir /opt/nginx/ext ...

  8. Flutter错误集合

    一.Waiting for another flutter command to release the startup lock... 运行flutter命令 flutter upgrade 运行 ...

  9. poj3162(树形dp+优先队列)

    Walking Race Time Limit: 10000MS   Memory Limit: 131072K Total Submissions: 5409   Accepted: 1371 Ca ...

  10. 最近在研究syslog日志,就说一下syslog格式吧

    syslog格式:<PRI>HEADER MESSAGE syslog的消息长度:不超过1024.syslog格式举例:<15>Jul 10 12:00:00 192.168. ...