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. Saiku去掉License验证信息以及数据备份(二十一)

    Saiku去掉License验证信息 终于还是走到了这一步,老早就在说要去掉这个License验证了,一直没做因为忙着别的.但是因为这个License还和可定义的用户数相关,限制了我们的使用,所以这里 ...

  2. 总结AWS使用要点

    最近做毕业设计,要用AWS跑代码,以前只用过一次,这次踩了很多坑,记录一下.. AWS命令行管理S3 bucket AWS官方文档: http://docs.amazonaws.cn/cli/late ...

  3. 【webpack学习笔记】a08-缓存

    一般要优化网站的加载速度可以从缓存入手,利用缓存可以降低网络流量,减少访问请求,从而可以达到加快访问速度的目的. 但是,如果一旦将文件缓存,那么有更新的时候就不能及时更新,若要看到最新内容,那么就需要 ...

  4. 记录配置xshell显示中文乱码的一次巧合

    linux系统设置了中文UTF8,xshell终端也设置了中文UTF8,但xshell仍然无法显示中文,于是想起可能是字体显示问题. 设置楷体字体后,虽然文字方向还有问题,但乱码显示的问题解决了,方向 ...

  5. 关于基于LinphoneSDK通话项目开发中遇到的相关问题

    在之前小学期的项目开发当中,我们小组进行的是使用网上开源的LinphoneSDK来开发一款Android端的VOIP电话APP. 因为网上关于这个SDK在安卓端的开发文档相当少,所以我们只能根据少量的 ...

  6. 003dayPython学习初始模块和字节码

    一.注释: 1.单行注释 # 被注释的内容 2.多行注释 """ 被注释的内容 """ 二.模块 我们在编程的时候,往往是一个主.py文件, ...

  7. 20145338 《网络对抗》逆向及Bof基础实验

    逆向及Bof基础实验 实践目标 ·本次实践的对象是一个名为pwn1的linux可执行文件. ·该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. ·该程序同时包含 ...

  8. P1403 [AHOI2005]约数研究 题解

    转载luogu某位神犇的题解QAQ 这题重点在于一个公式: f(i)=n/i 至于公式是怎么推出来的,看我解释: 1-n的因子个数,可以看成共含有2因子的数的个数+含有3因子的数的个数……+含有n因子 ...

  9. python笔记27-time模块

    import datetime, time#一种是时间戳.一种是格式化时间.一种是时间元组# print(time.timezone) # 和标准时间相差的时间,单位是sprint(int(time. ...

  10. complex类的定义和实现

    #include<iostream> #include<cmath> using namespace std; class complex { public: complex( ...