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. MySQL常用的锁机制 ----------顾名思义

    悲观锁与乐观锁: 悲观锁:顾名思义,就是很悲观,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会block直到它拿到锁.传统的关系型数据库里边就用到了很多这 ...

  2. oracle数据库 查看被锁定表及解锁方法

    最近做项目,遇到了更新超时的问题,反复检查数据都正常,纳闷完了忽然想到是不是表被锁定了,一看果然,解决办法如下: --锁表查询语句 SELECT object_name, machine, s.sid ...

  3. 第四周博客之一---Linux的基本命令(前5个)

    一.Linux的系统结构 "/"根目录部分有以下子目录: 1./bin:系统启动时需要的执行文件(二进制),这些文件可以被普通用户使用. 2./boot:用于自举加载程序(LILO ...

  4. Ubuntu下Caffe实现物体分类

    参考链接: ubuntu下配置Caffe:https://blog.csdn.net/a_z666666/article/details/72853346 https://www.cnblogs.co ...

  5. 初学者--oracle安装完后出现的一些问题

    对于初学者安装数据库后,立马回运行数据库,看是否安装成功,但oracle安装完成,其中有些都是默认状态,所以需要手动修改和添加.例如这个问题 1.pl/sql developer was not te ...

  6. java基础知识—字符串

    1.声明字符串的方法: String s ="你好" String s=new String("你好"); 2.字符串的长度: 字符串.length(); 3. ...

  7. ubuntu 下redis的安装简介

    Linux公社:https://www.linuxidc.com/topicnews.aspx?page=2&tid=2 简单介绍下ubuntu下redis的安装方式: 第一种: 1:进入re ...

  8. 三星Galaxy S8 刷机经验记录

    这段时间用上了三星S8,由于原生系统太耗电,所以萌生了root的想法.写这篇博客记录下这段时间的各种尝试. Root过程说明: 友情提示,道路千万条,安全第一条.开始捣鼓手机之前请一定准备好官方的救砖 ...

  9. bottle 0.5中的key-value数据库

    import anydbm as dbm import cPickle as pickle class BottleBucket(object): '''Memory-caching wrapper ...

  10. vuex数据持久化插件--指定持久化特定的值

    指定需要持久化的state,配置如下 import createPersistedState from "vuex-persistedstate" conststore = new ...