用NPOI操作Excel,将DataTable和List写入Excel并下载类
封装了一个NPOI操作类 WriteToDownLoad方法对DataTable和List写入Excel
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Web;
6
using System.IO;
7
using NPOI.HSSF.UserModel;
8
using NPOI.HPSF;
9
using NPOI.POIFS.FileSystem;
10
using System.Data;
11
using System.Reflection;
12
13
public class ExcelOperate
14
{
15
public ExcelOperate()
16
{
17
18
}
19
20
static HSSFWorkbook hssfworkbook;
21
22
/// <summary>
23
/// 初始化
24
/// </summary>
25
static void InitializeWorkbook()
26
{
27
hssfworkbook = new HSSFWorkbook();
28
29
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
30
dsi.Company = "";
31
hssfworkbook.DocumentSummaryInformation = dsi;
32
33
SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
34
si.Subject = "";
35
hssfworkbook.SummaryInformation = si;
36
}
37
38
/// <summary>
39
/// DataTable写入Excel
40
/// </summary>
41
/// <param name="FileName">要保存的文件名称 eg:test.xls</param>
42
/// <param name="SheetName">工作薄名称</param>
43
/// <param name="dt">要写入的DataTable </param>
44
public static void WriteToDownLoad(string FileName, string SheetName, DataTable dt)
45
{
46
string filename = FileName;
47
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
48
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename));
49
HttpContext.Current.Response.Clear();
50
51
//初始化Excel信息
52
InitializeWorkbook();
53
54
//填充数据
55
DTExcel(SheetName, dt, null);
56
57
HttpContext.Current.Response.BinaryWrite(WriteToStream().GetBuffer());
58
HttpContext.Current.Response.End();
59
}
60
61
62
/// <summary>
63
/// List写入Excel
64
/// </summary>
65
/// <typeparam name="T">实体</typeparam>
66
/// <param name="FileName">要保存的文件名称 eg:test.xls</param>
67
/// <param name="SheetName">工作薄名称</param>
68
/// <param name="lst">要写入的List</param>
69
public static void WriteToDownLoad<T>(string FileName, string SheetName, List<T> lst)
70
{
71
string filename = FileName;
72
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
73
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename));
74
HttpContext.Current.Response.Clear();
75
76
//初始化Excel信息
77
InitializeWorkbook();
78
79
//填充数据
80
ListExcel<T>(SheetName, lst, null);
81
82
HttpContext.Current.Response.BinaryWrite(WriteToStream().GetBuffer());
83
HttpContext.Current.Response.End();
84
}
85
86
87
static MemoryStream WriteToStream()
88
{
89
MemoryStream file = new MemoryStream();
90
hssfworkbook.Write(file);
91
return file;
92
}
93
94
95
96
数据填充部分
179
}
180
181
using System;2
using System.Collections.Generic;3
using System.Linq;4
using System.Text;5
using System.Web;6
using System.IO;7
using NPOI.HSSF.UserModel;8
using NPOI.HPSF;9
using NPOI.POIFS.FileSystem;10
using System.Data;11
using System.Reflection;12

13
public class ExcelOperate14
{15
public ExcelOperate()16
{17

18
}19

20
static HSSFWorkbook hssfworkbook;21

22
/// <summary>23
/// 初始化24
/// </summary>25
static void InitializeWorkbook()26
{27
hssfworkbook = new HSSFWorkbook();28

29
DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();30
dsi.Company = "";31
hssfworkbook.DocumentSummaryInformation = dsi;32

33
SummaryInformation si = PropertySetFactory.CreateSummaryInformation();34
si.Subject = "";35
hssfworkbook.SummaryInformation = si;36
}37

38
/// <summary>39
/// DataTable写入Excel40
/// </summary>41
/// <param name="FileName">要保存的文件名称 eg:test.xls</param>42
/// <param name="SheetName">工作薄名称</param>43
/// <param name="dt">要写入的DataTable </param>44
public static void WriteToDownLoad(string FileName, string SheetName, DataTable dt)45
{46
string filename = FileName;47
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";48
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename));49
HttpContext.Current.Response.Clear();50

51
//初始化Excel信息52
InitializeWorkbook();53

54
//填充数据55
DTExcel(SheetName, dt, null);56

57
HttpContext.Current.Response.BinaryWrite(WriteToStream().GetBuffer());58
HttpContext.Current.Response.End();59
}60

61

62
/// <summary>63
/// List写入Excel64
/// </summary>65
/// <typeparam name="T">实体</typeparam>66
/// <param name="FileName">要保存的文件名称 eg:test.xls</param>67
/// <param name="SheetName">工作薄名称</param>68
/// <param name="lst">要写入的List</param>69
public static void WriteToDownLoad<T>(string FileName, string SheetName, List<T> lst)70
{71
string filename = FileName;72
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";73
HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename));74
HttpContext.Current.Response.Clear();75

76
//初始化Excel信息77
InitializeWorkbook();78

79
//填充数据80
ListExcel<T>(SheetName, lst, null);81

82
HttpContext.Current.Response.BinaryWrite(WriteToStream().GetBuffer());83
HttpContext.Current.Response.End();84
}85

86

87
static MemoryStream WriteToStream()88
{89
MemoryStream file = new MemoryStream();90
hssfworkbook.Write(file);91
return file;92
}93

94
95

96
数据填充部分179
}180

181


浙公网安备 33010602011771号