项目中的一个功能是将数据导入到Excel文件中,这里使用NPOI操作Excel,代码如下:
复制代码
public class Excel : IDataTransfer
{
public Stream Export(string[] titles, List<string>[] dataSource)
{
return ExportData(titles, dataSource);
}
protected virtual Stream ExportData(string[] titles, List<string>[] dataSource)
{
if (dataSource == null)
{
throw new ArgumentNullException();
}
HSSFWorkbook book = new HSSFWorkbook();
ISheet sheet = book.CreateSheet("sheet1");
int rowCount = dataSource.Length;
int cellCount = dataSource[0].Count;
var titleRow = sheet.CreateRow(0);
#region header style
// 该行的高度
titleRow.HeightInPoints = 18;
// 列的样式
ICellStyle headStyle = book.CreateCellStyle();
// 单元格内容居中显示