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. K2百家讲坛 | 越秀地产:K2为房企数字化转型带来更多可能

    随着数字化经济时代的到来,房地产行业逐渐形成了新的竞争和市场格局,房企要在此背景下实现稳步发展,需要由原本的粗放式管理逐渐向集团性管理.精细化管控转变,这对房企的经营发展战略和业务管理方式都提出了不小 ...

  2. Spring _day02_IoC注解开发入门

    1.Spring IoC注解开发入门 1.1 注解开发案例: 创建项目所需要的jar,四个基本的包(beans core context expression ),以及两个日志记录的包,还要AOP的包 ...

  3. 电脑小白和ta的小白电脑——Tomcat服务器

    配置web服务器tomcat,这里默认了已经配置JAVA开发环境↓ https://www.cnblogs.com/gifted35/p/9775112.html (一)下载tomcat 我安装的服务 ...

  4. FTP 代理服务器实现

    本文将在Linux环境下实现一个简单的FTP代理服务器,主要内容涉及FTP主动/被动模式和简单的Socket编程. 1. 主动模式和被动模式 FTP有两种模式,即主动模式(Active Mode)和被 ...

  5. 域名排序 sort uniq awk

    [root@web01 ~]# sort [-fbMnrtuk] [file or stdin] 选项与参数:-f :忽略大小写的差异,例如 A 与 a 视为编码相同:-b :忽略最前面的空格符部分: ...

  6. java Reference

    相关讲解,参考: Java Reference 源码分析 Java Reference详解 Reference: // 名称说明下:Reference指代引用对象本身,Referent指代被引用对象 ...

  7. for ,foreach ,map 循环的区别

    一.for循环 1.for - 循环代码块一定的次数 遍历数组最常用到的for循环,是最为熟知的一种方法 for (var i=0; i<5; i++) { x=x + "The nu ...

  8. C# FTP操作报550错误

    最近在做FTP创建文件夹和上传文件的功能,测试之后一直提示“远程服务器返回错误: (550) 文件不可用(例如,未找到文件,无法访问文件)”,我在网上找了很久的解决方案也没有解决掉这个问题,网上找到的 ...

  9. 运维脚本while语法

    循环的意思就是让程序重复地执行某些语句; whiler循环就是循环结构的一种,当事先不知道循环该执行多少次,就要用到while循环; while循环语句的运行过程 使用while循环语句时,可以根据特 ...

  10. Graphics Class

    System.Drawing 封装一个 GDI+ 绘图图面. 此类不能被继承. https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.g ...