public class NPOIExcel
{
private string _title;
private string _sheetName;
private string _filePath; /// <summary>
/// 导出到Excel
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
public bool ToExcel(DataTable table)
{
FileStream fs = new FileStream(this._filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
IWorkbook workBook = new HSSFWorkbook();
this._sheetName = this._sheetName.IsEmpty() ? "sheet1" : this._sheetName;
ISheet sheet = workBook.CreateSheet(this._sheetName); //处理表格标题
IRow row = sheet.CreateRow();
row.CreateCell().SetCellValue(this._title);
sheet.AddMergedRegion(new CellRangeAddress(, , , table.Columns.Count - ));
row.Height = ; ICellStyle cellStyle = workBook.CreateCellStyle();
IFont font = workBook.CreateFont();
font.FontName = "微软雅黑";
font.FontHeightInPoints = ;
cellStyle.SetFont(font);
cellStyle.VerticalAlignment = VerticalAlignment.Center;
cellStyle.Alignment = HorizontalAlignment.Center;
row.Cells[].CellStyle = cellStyle; //处理表格列头
row = sheet.CreateRow();
for (int i = ; i < table.Columns.Count; i++)
{
row.CreateCell(i).SetCellValue(table.Columns[i].ColumnName);
row.Height = ;
sheet.AutoSizeColumn(i);
} //处理数据内容
for (int i = ; i < table.Rows.Count; i++)
{
row = sheet.CreateRow( + i);
row.Height = ;
for (int j = ; j < table.Columns.Count; j++)
{
row.CreateCell(j).SetCellValue(table.Rows[i][j].ToString());
sheet.SetColumnWidth(j, * );
}
} //写入数据流
workBook.Write(fs);
fs.Flush();
fs.Close(); return true;
} /// <summary>
/// 导出到Excel
/// </summary>
/// <param name="table"></param>
/// <param name="title"></param>
/// <param name="sheetName"></param>
/// <returns></returns>
public bool ToExcel(DataTable table, string title, string sheetName, string filePath)
{
this._title = title;
this._sheetName = sheetName;
this._filePath = filePath;
return ToExcel(table);
}
}

NPOIExcel的更多相关文章

  1. 封装对NPOIExcel的操作,方便的设置导出Excel的样式

    下载: http://pan.baidu.com/s/1boTpT5l 使用方法: 导入: 使用 ReadToDataTable方法 导出: NPOIExcel.ExcelManager manger ...

  2. NPOI-Excel系列-1002.创建带有Document Summary Information和Summary Information的Excel文件

    1. using NPOI.HSSF.UserModel; using NPOI.HPSF; using NPOI.POIFS.FileSystem; using Microsoft.VisualSt ...

  3. NPOI-Excel系列-1000.创建一个标准的Excel文件

    using NPOI.HSSF.UserModel; using Microsoft.VisualStudio.TestTools.UnitTesting; using System.IO; name ...

  4. 关于NPOIExcel导出excel

    1.支持导出多个sheet文件 /// <summary> /// 导出到Excel并下载(html) /// </summary> /// <param name=&q ...

  5. Asp.Net 使用Npoi导出Excel

    引言 使用Npoi导出Excel 服务器可以不装任何office组件,昨天在做一个导出时用到Npoi导出Excel,而且所导Excel也符合规范,打开时不会有任何文件损坏之类的提示.但是在做导入时还是 ...

  6. NPOI 导出Excel

    NPOIExcel npoiexcel = new NPOIExcel(); string filename = DateTime.Now.ToString("yyyyMMddHHmmssf ...

  7. git日志的查看与修改

    1.命令行中查看日志 git log 默认是显示所有的日志信息,之前出来的界面显示的日志,很少. 最后发现,只需要使用键盘上向下键↓,就可以继续浏览更多的日志 空格键,可以翻页浏览日志. 向左←  向 ...

  8. Web C# 导出Excel 方法总结

    方法1:微软推荐服务器需安装Excel型 依赖: 软件:Office Excel 2007-2013 引用:Microsoft Office 14.0 Object Library 1.1 数据准备 ...

  9. asp.net mvc4 easyui datagrid 增删改查分页 导出 先上传后导入 NPOI批量导入 导出EXCEL

    效果图 数据库代码 create database CardManage use CardManage create table CardManage ( ID ,) primary key, use ...

随机推荐

  1. MapReduce实现手机上网日志分析(排序)

    一.背景 1.1 流程 实现排序,分组拍上一篇通过Partitioner实现了. 实现接口,自动产生接口方法,写属性,产生getter和setter,序列化和反序列化属性,写比较方法,重写toStri ...

  2. eclipse发布web项目到生产环境的方式汇总(tomcat)

    参考: http://www.cnblogs.com/xiohao/p/3689832.html http://www.111cn.net/jsp/J2EE-EJB/90337.htm 我收集了几点: ...

  3. 关于line-height

    line-height不允许负值,给定值之后会根据font-size计算行高,line-height指定的行高是行高最小值,设置height可以增大行高 line-height的计算:以px为例,li ...

  4. oracle---jdbctest--laobai

    import java.sql.CallableStatement; import java.sql.Connection; import java.sql.ResultSet; import ora ...

  5. C#------判断btye[]是否为空

    public byte[] PhotoByte; //= new byte[byte.MaxValue]; if(PhotoByte == null) { MessageBox.Show(" ...

  6. Java学习资料

    微信扫码:http://v.dxsbb.com/jisuanji/Java之家:http://www.javazhijia.com/bs/biye/137.html一些 http://www.ibei ...

  7. 树莓派笔记之使用netselect选择最快Raspbian软件源

    背景: 之前在葉難大大的部落格里看到有讲可以使用netselect查找最快软件源,今天正好看到, 特此记下来,因为之前一直使用中国科学技术大学的源,结果发现不是我这里最快的. 注意: 以下仅对Rasp ...

  8. Eclipse DDT

    http://www.eclipse.org/downloads/ https://github.com/DDT-IDE/DDT/blob/latest/documentation/UserGuide ...

  9. SQL SERVER批量修改表名前缀

    比如前缀由mms_修改为 ets_ exec   sp_msforeachtable     @command1='  declare   @o   sysname,@n   sysname     ...

  10. Robots.txt - 禁止爬虫(转)

    Robots.txt - 禁止爬虫 robots.txt用于禁止网络爬虫访问网站指定目录.robots.txt的格式采用面向行的语法:空行.注释行(以#打头).规则行.规则行的格式为:Field: v ...