DataTable 导出Excel 之 NPOI

NPOI 强大的地方就是Excel里面90%功能,都能实现。坑爹的是需要引用5个DLL

?

官方例子:

static void Main(string[] args)

{

IWorkbook workbook = new XSSFWorkbook();

ISheet worksheet = workbook.CreateSheet("Sheet1");

?

for (int rownum = 0; rownum < 10000; rownum++)

{

IRow row = worksheet.CreateRow(rownum);

for (int celnum = 0; celnum < 20; celnum++)

{

ICell Cell = row.CreateCell(celnum);

Cell.SetCellValue("Cell: Row-" + rownum + ";CellNo:" + celnum);

}

}

?

FileStream sw = File.Create("test.xlsx");

workbook.Write(sw);

sw.Close();

}

?

自定义导出数据,设置字体,列宽,表头

?

?

DataTable dt =//

IWorkbook
workbook
=
new
XSSFWorkbook();

ISheet
worksheet
=
workbook.CreateSheet("fulu");

?

//字体设置

IFont
Font
=
workbook.CreateFont();

Font.FontName = "华文细黑";

ICellStyle
CellStyle
=
workbook.CreateCellStyle();

CellStyle.SetFont(Font);

?

IFont
FontHead
=
workbook.CreateFont();

FontHead.FontName = "华文细黑";

FontHead.Boldweight =
(short)NPOI.SS.UserModel.FontBoldWeight.Bold;

ICellStyle
CellHeadStyle
=
workbook.CreateCellStyle();

CellHeadStyle.SetFont(FontHead);

?

#region
定义DataTable要显示的数据(如果全部显示,忽略这种方法)

?

List<ExcelFormat> formatList = new
List<ExcelFormat>();

int
n
= 0;

ExcelFormat
item
=
new
ExcelFormat();

item.ID = n;

item.ColTitle = "订单编号";

item.ColName = "OrderNo";

item.ColWidth = 20;

formatList.Add(item);

?

item
=
new
ExcelFormat();

item.ID =
++n;

item.ColTitle = "购买时间";

item.ColName = "BuyTime";

item.ColWidth = 20;

formatList.Add(item);

?

item
=
new
ExcelFormat();

item.ID =
++n;

item.ColTitle = "购买数量";

item.ColName = "BuyNum";

item.ColWidth = 10;

formatList.Add(item);

?

item
=
new
ExcelFormat();

item.ID =
++n;

item.ColTitle = "订单金额";

item.ColName = "Payment";

item.ColWidth = 10;

formatList.Add(item);

?

item
=
new
ExcelFormat();

item.ID =
++n;

item.ColTitle = "商品编号";

item.ColName = "ProductId";

item.ColWidth = 15;

formatList.Add(item);

?

item
=
new
ExcelFormat();

item.ID =
++n;

item.ColTitle = "商品名称";

item.ColName = "ProductName";

item.ColWidth = 40;

formatList.Add(item);

?

item
=
new
ExcelFormat();

item.ID =
++n;

item.ColTitle = "充值账号";

item.ColName = "ChargeAccount";

item.ColWidth = 20;

formatList.Add(item);

?

item
=
new
ExcelFormat();

item.ID = n++;

item.ColTitle = "FL订单编号";

item.ColName = "FLOrderNo";

item.ColWidth = 20;

formatList.Add(item);

?

item
=
new
ExcelFormat();

item.ID = n++;

item.ColTitle = "订单状态";

item.ColName = "OrderStatus";

item.ColWidth = 15;

formatList.Add(item);

?

item
=
new
ExcelFormat();

item.ID = n++;

item.ColTitle = "买家地区";

item.ColName = "UserIP";

item.ColWidth = 20;

formatList.Add(item);

?

item
=
new
ExcelFormat();

item.ID = n++;

item.ColTitle = "备注";

item.ColName = "Remark";

item.ColWidth = 40;

formatList.Add(item);

?

#endregion

?

//设置每一列的宽度

foreach
(ExcelFormat
format in
formatList)

{

worksheet.SetColumnWidth(format.ID, format.ColWidth * 256);

}

?

//写入表头,从第行开始, 华文细黑+加粗

IRow
rowhead
=
worksheet.CreateRow(0);

foreach
(ExcelFormat
format in
formatList)

{

ICell
Cell
=
rowhead.CreateCell(format.ID);

Cell.SetCellValue(format.ColTitle);

Cell.CellStyle = CellHeadStyle;

}

?

//写入行,从第行开始,华文细黑

for (int
rownum = 1;
rownum
<=
dt.Rows.Count;
rownum++)

{

IRow
row
=
worksheet.CreateRow(rownum);

foreach
(ExcelFormat
format in
formatList)

{

ICell
Cell
=
row.CreateCell(format.ID);

Cell.SetCellValue(dt.Rows[rownum - 1][format.ColName].ToString());

Cell.CellStyle = CellStyle;

}

}

?

//保存到Excel

string
filename
=
string.Format("fulu{0}.xlsx",
DateTime.Now.ToLongDateString());

FileStream
sw
=
File.Create(filename);

workbook.Write(sw);

sw.Close();

?

MessageBox.Show("导出成功");

?

?

?

class
ExcelFormat

{

public
int ID { get; set; }

public
string ColTitle { get; set; }

public
string ColName { get; set; }

public
int ColWidth { get; set; }

}

?

时间: 2024-12-15 04:27:28

DataTable 导出Excel 之 NPOI的相关文章

Excel导入DataTable兼容2003-2012(请细心查看注释)以及 DataTable导出Excel(导出格式2003.xls)注释:需要引用NPOI

1.#region Excel导入DataTable兼容2003-2012(请细心查看注释)/// <summary> /// 读取Excel文件到DataSet中/// 注释1:2012导出如报错“ System.InvalidOperationException: 未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序.”解决:下载2007 Office system 驱动程序:数据连接组件安装http://download.microsoft.com/downl

ASP.NET导出Excel(利用NPOI和EPPlus库,无需安装Office)

网上提供了很多Asp.net中操作Excel的方法,其中大部分是调用微软的Office组件,下面提供三个无须安装Office即可从Asp.net输出Excel的方法. 1 简单方法 //下面代码输出的Excel有三列(姓名.年龄.性别)//列之间用\t隔开StringWriter sw = new StringWriter();sw.WriteLine("姓名\t年龄\t性别"); //Excel表格的列标题 sw.WriteLine("张三\t29\t男"); /

asp.net DataTable导出 excel的方法记录(第三方)

官网:http://npoi.codeplex.com/ 简单应用,主要是可以实现我们想要的简单效果,呵呵 需要引入dll,可以在官网下载,也可在下面下载 C#代码   protected void getExcel(DataTable dt) { NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook(); NPOI.SS.UserModel.Sheet sheet = book.CreateSh

C# Datatable导出Excel方法

C# 导出Excel方法  先引用下System.IO;System.data; 具体函数如下: 1 public static bool ExportCSV(DataTable dt, string fileNmae) 2 { 3 bool Msg = false; 4 string con = ""; 5 foreach (DataColumn dc in dt.Columns) 6 { 7 con += dc.ColumnName + ","; 8 } 9 c

【转】C# DataTable 导出 Excel 进阶 多行表头、合并单元格、中文文件名乱码

本文原创地址:http://blog.csdn.net/ranbolwb/article/details/8083983 ,转载请保留本行. 本例子是上一篇 DataTable 导出 Excel 的进阶,除了上一篇提到的处理乱码问题,本例还添加了处理多行表头.合并单元格的功能及处理中文文件名乱码问题,应该可以满足日常开发的需要了. 废话不多说了,直接上代码: [C#] 可以写单独类 1 using System; 2 using System.Collections.Generic; 3 usi

DataTable 导出 excel , 可以自定义列名,没有繁琐的引用

/// <summary> /// 由DataTable导出Excel /// </summary> /// <param name="p"></param> /// <param name="dt"></param> /// <param name="fileName"></param> /// <param name="b"

C#中datatable导出excel(三种方法)

方法一:(拷贝直接可以使用,适合大批量资料, 上万笔)Microsoft.Office.Interop.Excel.Application appexcel = new Microsoft.Office.Interop.Excel.Application();SaveFileDialog savefiledialog = new SaveFileDialog();System.Reflection.Missing miss = System.Reflection.Missing.Value;ap

NPOI DataTable导出excel

/// <summary> /// DataTable导出到Excel文件 /// </summary> /// <param name="dtSource">源DataTable</param> /// <param name="strHeaderText">表头文本</param> /// <param name="strFileName">保存位置</

DataTable ---导出Excel

//mscorlib.dll, v4.0.0.0&&Microsoft.Office.Interop.Excel.dll, v14.0.0.0 /// <summary> /// 导出Excel /// </summary> /// <param name="dt_data">要导出的数据表</param> public void ExcelPort(DataTable dt_data) { try { //表格控件对象(